1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* load_file_position(), save_file_position() */
51 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/widget.h"
56 #include "lib/charsets.h" /* get_codepage_id */
59 #include "src/filemanager/cmd.h" /* view_other_cmd() */
60 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
62 #include "src/main.h" /* source_codepage */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
73 int option_typewriter_wrap
= 0;
74 int option_auto_para_formatting
= 0;
75 int option_fill_tabs_with_spaces
= 0;
76 int option_return_does_auto_indent
= 1;
77 int option_backspace_through_tabs
= 0;
78 int option_fake_half_tabs
= 1;
79 int option_save_mode
= EDIT_QUICK_SAVE
;
80 int option_save_position
= 1;
81 int option_max_undo
= 32768;
82 int option_persistent_selections
= 1;
83 int option_cursor_beyond_eol
= 0;
84 int option_line_state
= 0;
85 int option_line_state_width
= 0;
87 int option_edit_right_extreme
= 0;
88 int option_edit_left_extreme
= 0;
89 int option_edit_top_extreme
= 0;
90 int option_edit_bottom_extreme
= 0;
91 int enable_show_tabs_tws
= 1;
92 int option_check_nl_at_eof
= 0;
93 int show_right_margin
= 0;
95 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
96 char *option_backup_ext
= NULL
;
98 int edit_stack_iterator
= 0;
99 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
100 /* magic sequense for say than block is vertical */
101 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
103 /*** file scope macro definitions ****************************************************************/
105 #define TEMP_BUF_LEN 1024
107 #define space_width 1
109 /*** file scope type declarations ****************************************************************/
111 /*** file scope variables ************************************************************************/
113 /* detecting an error on save is easy: just check if every byte has been written. */
114 /* detecting an error on read, is not so easy 'cos there is not way to tell
115 whether you read everything or not. */
116 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
117 static const struct edit_filters
119 const char *read
, *write
, *extension
;
123 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
124 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
125 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
126 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
131 static long last_bracket
= -1;
133 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
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 void user_menu (WEdit
* edit
);
169 static int left_of_four_spaces (WEdit
* edit
);
170 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
172 /* --------------------------------------------------------------------------------------------- */
177 const char *header
= N_("About");
178 const char *button_name
= N_("&OK");
179 const char *const version
= "MCEdit " VERSION
;
180 char text
[BUF_LARGE
];
182 int win_len
, version_len
, button_len
;
189 button_name
= _(button_name
);
192 button_len
= str_term_width1 (button_name
) + 5;
193 version_len
= str_term_width1 (version
);
195 g_snprintf (text
, sizeof (text
),
196 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
197 " A user friendly text editor\n"
198 " written for the Midnight Commander"));
200 win_len
= str_term_width1 (header
);
201 win_len
= max (win_len
, version_len
);
202 win_len
= max (win_len
, button_len
);
204 /* count width and height of text */
205 str_msg_term_size (text
, &lines
, &cols
);
207 cols
= max (win_len
, cols
) + 6;
210 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
211 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
213 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
214 add_widget (about_dlg
, label_new (5, 3, text
));
215 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
216 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
219 destroy_dlg (about_dlg
);
222 /* --------------------------------------------------------------------------------------------- */
224 * Initialize the buffers for an empty files.
228 edit_init_buffers (WEdit
* edit
)
232 for (j
= 0; j
<= MAXBUFF
; j
++)
234 edit
->buffers1
[j
] = NULL
;
235 edit
->buffers2
[j
] = NULL
;
240 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
243 /* --------------------------------------------------------------------------------------------- */
245 * Load file OR text into buffers. Set cursor to the beginning of file.
246 * @returns 1 on error.
250 edit_load_file_fast (WEdit
* edit
, const char *filename
)
256 edit
->curs2
= edit
->last_byte
;
257 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
259 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
264 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
265 edit_error_dialog (_("Error"), errmsg
);
270 if (!edit
->buffers2
[buf2
])
271 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
276 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
277 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
280 for (buf
= buf2
- 1; buf
>= 0; buf
--)
282 /* edit->buffers2[0] is already allocated */
283 if (!edit
->buffers2
[buf
])
284 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
285 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
293 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
294 edit_error_dialog (_("Error"), err_str
);
301 /* --------------------------------------------------------------------------------------------- */
302 /** Return index of the filter or -1 is there is no appropriate filter */
305 edit_find_filter (const char *filename
)
309 if (filename
== NULL
)
312 l
= strlen (filename
);
313 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
315 e
= strlen (all_filters
[i
].extension
);
317 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
323 /* --------------------------------------------------------------------------------------------- */
326 edit_get_filter (const char *filename
)
329 char *p
, *quoted_name
;
331 i
= edit_find_filter (filename
);
335 quoted_name
= name_quote (filename
, 0);
336 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
337 g_free (quoted_name
);
341 /* --------------------------------------------------------------------------------------------- */
344 edit_insert_stream (WEdit
* edit
, FILE * f
)
348 while ((c
= fgetc (f
)) >= 0)
350 edit_insert (edit
, c
);
356 /* --------------------------------------------------------------------------------------------- */
357 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
360 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
363 gchar
*errmsg
= NULL
;
365 /* Try opening an existing file */
366 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
371 * Try creating the file. O_EXCL prevents following broken links
372 * and opening existing files.
374 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
377 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
382 /* New file, delete it if it's not modified or saved */
383 edit
->delete_file
= 1;
387 /* Check what we have opened */
388 if (mc_fstat (file
, st
) < 0)
390 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
394 /* We want to open regular files only */
395 if (!S_ISREG (st
->st_mode
))
397 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
402 * Don't delete non-empty files.
403 * O_EXCL should prevent it, but let's be on the safe side.
406 edit
->delete_file
= 0;
408 if (st
->st_size
>= SIZE_LIMIT
)
409 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
412 (void) mc_close (file
);
416 edit_error_dialog (_("Error"), errmsg
);
423 /* --------------------------------------------------------------------------------------------- */
425 * Open the file and load it into the buffers, either directly or using
426 * a filter. Return 0 on success, 1 on error.
428 * Fast loading (edit_load_file_fast) is used when the file size is
429 * known. In this case the data is read into the buffers by blocks.
430 * If the file size is not known, the data is loaded byte by byte in
435 edit_load_file (WEdit
* edit
)
439 /* Cannot do fast load if a filter is used */
440 if (edit_find_filter (edit
->filename
) >= 0)
444 * VFS may report file size incorrectly, and slow load is not a big
445 * deal considering overhead in VFS.
447 if (!vfs_file_is_local (edit
->filename
))
451 * FIXME: line end translation should disable fast loading as well
452 * Consider doing fseek() to the end and ftell() for the real size.
457 /* If we are dealing with a real file, check that it exists */
458 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
463 /* nothing to load */
467 edit_init_buffers (edit
);
471 edit
->last_byte
= edit
->stat1
.st_size
;
472 edit_load_file_fast (edit
, edit
->filename
);
473 /* If fast load was used, the number of lines wasn't calculated */
474 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
481 edit
->stack_disable
= 1;
482 if (!edit_insert_file (edit
, edit
->filename
))
487 edit
->stack_disable
= 0;
494 /* --------------------------------------------------------------------------------------------- */
495 /** Restore saved cursor position in the file */
498 edit_load_position (WEdit
* edit
)
504 if (!edit
->filename
|| !*edit
->filename
)
507 filename
= vfs_canon (edit
->filename
);
508 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
513 edit_move_to_line (edit
, line
- 1);
514 edit
->prev_col
= column
;
518 edit_cursor_move (edit
, offset
);
519 line
= edit
->curs_line
;
520 edit
->search_start
= edit
->curs1
;
523 book_mark_restore (edit
, BOOK_MARK_COLOR
);
525 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
526 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
529 /* --------------------------------------------------------------------------------------------- */
530 /** Save cursor position in the file */
533 edit_save_position (WEdit
* edit
)
537 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
540 filename
= vfs_canon (edit
->filename
);
542 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
543 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
544 edit
->serialized_bookmarks
);
545 edit
->serialized_bookmarks
= NULL
;
550 /* --------------------------------------------------------------------------------------------- */
551 /** Clean the WEdit stricture except the widget part */
554 edit_purge_widget (WEdit
* edit
)
556 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
557 char *start
= (char *) edit
+ sizeof (Widget
);
558 memset (start
, 0, len
);
559 edit
->macro_i
= -1; /* not recording a macro */
562 /* --------------------------------------------------------------------------------------------- */
565 edit_set_keymap (void)
567 editor_map
= default_editor_keymap
;
568 if (editor_keymap
&& editor_keymap
->len
> 0)
569 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
571 editor_x_map
= default_editor_x_keymap
;
572 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
573 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
576 /* --------------------------------------------------------------------------------------------- */
578 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
579 then the file should be as it was when he loaded up. Then set edit->modified to 0.
583 pop_action (WEdit
* edit
)
586 unsigned long sp
= edit
->stack_pointer
;
588 if (sp
== edit
->stack_bottom
)
591 sp
= (sp
- 1) & edit
->stack_size_mask
;
592 c
= edit
->undo_stack
[sp
];
595 /* edit->undo_stack[sp] = '@'; */
596 edit
->stack_pointer
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
600 if (sp
== edit
->stack_bottom
)
603 c
= edit
->undo_stack
[(sp
- 1) & edit
->stack_size_mask
];
604 if (edit
->undo_stack
[sp
] == -2)
606 /* edit->undo_stack[sp] = '@'; */
607 edit
->stack_pointer
= sp
;
610 edit
->undo_stack
[sp
]++;
615 /* --------------------------------------------------------------------------------------------- */
616 /** is called whenever a modification is made by one of the four routines below */
619 edit_modification (WEdit
* edit
)
621 edit
->caches_valid
= 0;
622 edit
->screen_modified
= 1;
624 /* raise lock when file modified */
625 if (!edit
->modified
&& !edit
->delete_file
)
626 edit
->locked
= edit_lock_file (edit
);
630 /* --------------------------------------------------------------------------------------------- */
633 edit_insert_over (WEdit
* edit
)
637 for (i
= 0; i
< edit
->over_col
; i
++)
639 edit_insert (edit
, ' ');
644 /* --------------------------------------------------------------------------------------------- */
647 edit_backspace (WEdit
* edit
, const int byte_delete
)
658 if (edit
->utf8
&& byte_delete
== 0)
660 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
664 for (i
= 1; i
<= cw
; i
++)
666 if (edit
->mark1
>= edit
->curs1
)
669 edit
->end_mark_curs
--;
671 if (edit
->mark2
>= edit
->curs1
)
673 if (edit
->last_get_rule
>= edit
->curs1
)
674 edit
->last_get_rule
--;
676 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
677 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
678 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
680 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
681 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
685 edit_push_action (edit
, p
);
687 edit_modification (edit
);
691 book_mark_dec (edit
, edit
->curs_line
);
694 edit
->force
|= REDRAW_AFTER_CURSOR
;
697 if (edit
->curs1
< edit
->start_display
)
699 edit
->start_display
--;
708 /* --------------------------------------------------------------------------------------------- */
710 #ifdef FAST_MOVE_CURSOR
712 memqcpy (WEdit
* edit
, unsigned char *dest
, unsigned char *src
, int n
)
715 while ((next
= (unsigned long) memccpy (dest
, src
, '\n', n
)))
718 next
-= (unsigned long) dest
;
724 #endif /* FAST_MOVE_CURSOR */
726 /* --------------------------------------------------------------------------------------------- */
727 /* high level cursor movement commands */
728 /* --------------------------------------------------------------------------------------------- */
731 is_in_indent (WEdit
* edit
)
733 long p
= edit_bol (edit
, edit
->curs1
);
734 while (p
< edit
->curs1
)
735 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
740 /* --------------------------------------------------------------------------------------------- */
743 is_blank (WEdit
* edit
, long offset
)
747 s
= edit_bol (edit
, offset
);
748 f
= edit_eol (edit
, offset
) - 1;
751 c
= edit_get_byte (edit
, s
++);
759 /* --------------------------------------------------------------------------------------------- */
760 /** returns the offset of line i */
763 edit_find_line (WEdit
* edit
, int line
)
767 if (!edit
->caches_valid
)
769 for (i
= 0; i
< N_LINE_CACHES
; i
++)
770 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
771 /* three offsets that we *know* are line 0 at 0 and these two: */
772 edit
->line_numbers
[1] = edit
->curs_line
;
773 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
774 edit
->line_numbers
[2] = edit
->total_lines
;
775 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
776 edit
->caches_valid
= 1;
778 if (line
>= edit
->total_lines
)
779 return edit
->line_offsets
[2];
782 /* find the closest known point */
783 for (i
= 0; i
< N_LINE_CACHES
; i
++)
786 n
= abs (edit
->line_numbers
[i
] - line
);
794 return edit
->line_offsets
[j
]; /* know the offset exactly */
795 if (m
== 1 && j
>= 3)
796 i
= j
; /* one line different - caller might be looping, so stay in this cache */
798 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
799 if (line
> edit
->line_numbers
[j
])
800 edit
->line_offsets
[i
] =
801 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
803 edit
->line_offsets
[i
] =
804 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
805 edit
->line_numbers
[i
] = line
;
806 return edit
->line_offsets
[i
];
809 /* --------------------------------------------------------------------------------------------- */
810 /** moves up until a blank line is reached, or until just
811 before a non-blank line is reached */
814 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
817 if (edit
->curs_line
> 1)
819 if (line_is_blank (edit
, edit
->curs_line
))
821 if (line_is_blank (edit
, edit
->curs_line
- 1))
823 for (i
= edit
->curs_line
- 1; i
; i
--)
824 if (!line_is_blank (edit
, i
))
832 for (i
= edit
->curs_line
- 1; i
; i
--)
833 if (line_is_blank (edit
, i
))
839 for (i
= edit
->curs_line
- 1; i
; i
--)
840 if (line_is_blank (edit
, i
))
844 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
847 /* --------------------------------------------------------------------------------------------- */
848 /** moves down until a blank line is reached, or until just
849 before a non-blank line is reached */
852 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
855 if (edit
->curs_line
>= edit
->total_lines
- 1)
857 i
= edit
->total_lines
;
861 if (line_is_blank (edit
, edit
->curs_line
))
863 if (line_is_blank (edit
, edit
->curs_line
+ 1))
865 for (i
= edit
->curs_line
+ 1; i
; i
++)
866 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
874 for (i
= edit
->curs_line
+ 1; i
; i
++)
875 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
881 for (i
= edit
->curs_line
+ 1; i
; i
++)
882 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
886 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
889 /* --------------------------------------------------------------------------------------------- */
892 edit_begin_page (WEdit
* edit
)
894 edit_update_curs_row (edit
);
895 edit_move_up (edit
, edit
->curs_row
, 0);
898 /* --------------------------------------------------------------------------------------------- */
901 edit_end_page (WEdit
* edit
)
903 edit_update_curs_row (edit
);
904 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
908 /* --------------------------------------------------------------------------------------------- */
909 /** goto beginning of text */
912 edit_move_to_top (WEdit
* edit
)
916 edit_cursor_move (edit
, -edit
->curs1
);
917 edit_move_to_prev_col (edit
, 0);
918 edit
->force
|= REDRAW_PAGE
;
919 edit
->search_start
= 0;
920 edit_update_curs_row (edit
);
925 /* --------------------------------------------------------------------------------------------- */
926 /** goto end of text */
929 edit_move_to_bottom (WEdit
* edit
)
931 if (edit
->curs_line
< edit
->total_lines
)
933 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
934 edit
->start_display
= edit
->last_byte
;
935 edit
->start_line
= edit
->total_lines
;
936 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
937 edit
->force
|= REDRAW_PAGE
;
941 /* --------------------------------------------------------------------------------------------- */
942 /** goto beginning of line */
945 edit_cursor_to_bol (WEdit
* edit
)
947 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
948 edit
->search_start
= edit
->curs1
;
949 edit
->prev_col
= edit_get_col (edit
);
953 /* --------------------------------------------------------------------------------------------- */
954 /** goto end of line */
957 edit_cursor_to_eol (WEdit
* edit
)
959 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
960 edit
->search_start
= edit
->curs1
;
961 edit
->prev_col
= edit_get_col (edit
);
965 /* --------------------------------------------------------------------------------------------- */
972 const char option_chars_move_whole_word
[] =
973 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !Aa0 !+-*/= |<> ![ !] !\\#! ";
979 if (*option_chars_move_whole_word
== '!')
983 if (g_ascii_isupper ((gchar
) c
))
985 else if (g_ascii_islower ((gchar
) c
))
987 else if (g_ascii_isalpha (c
))
989 else if (isdigit (c
))
991 else if (isspace (c
))
993 q
= strchr (option_chars_move_whole_word
, c
);
998 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1003 while ((q
= strchr (q
+ 1, c
)));
1007 /* --------------------------------------------------------------------------------------------- */
1010 edit_left_word_move (WEdit
* edit
, int s
)
1015 if (edit
->column_highlight
1016 && edit
->mark1
!= edit
->mark2
1017 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1019 edit_cursor_move (edit
, -1);
1022 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1023 c2
= edit_get_byte (edit
, edit
->curs1
);
1024 if (!(my_type_of (c1
) & my_type_of (c2
)))
1026 if (isspace (c1
) && !isspace (c2
))
1029 if (!isspace (c1
) && isspace (c2
))
1034 /* --------------------------------------------------------------------------------------------- */
1037 edit_left_word_move_cmd (WEdit
* edit
)
1039 edit_left_word_move (edit
, 0);
1040 edit
->force
|= REDRAW_PAGE
;
1043 /* --------------------------------------------------------------------------------------------- */
1046 edit_right_word_move (WEdit
* edit
, int s
)
1051 if (edit
->column_highlight
1052 && edit
->mark1
!= edit
->mark2
1053 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1055 edit_cursor_move (edit
, 1);
1056 if (edit
->curs1
>= edit
->last_byte
)
1058 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1059 c2
= edit_get_byte (edit
, edit
->curs1
);
1060 if (!(my_type_of (c1
) & my_type_of (c2
)))
1062 if (isspace (c1
) && !isspace (c2
))
1065 if (!isspace (c1
) && isspace (c2
))
1070 /* --------------------------------------------------------------------------------------------- */
1073 edit_right_word_move_cmd (WEdit
* edit
)
1075 edit_right_word_move (edit
, 0);
1076 edit
->force
|= REDRAW_PAGE
;
1079 /* --------------------------------------------------------------------------------------------- */
1082 edit_right_char_move_cmd (WEdit
* edit
)
1088 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1094 c
= edit_get_byte (edit
, edit
->curs1
);
1096 if (option_cursor_beyond_eol
&& c
== '\n')
1102 edit_cursor_move (edit
, cw
);
1106 /* --------------------------------------------------------------------------------------------- */
1109 edit_left_char_move_cmd (WEdit
* edit
)
1112 if (edit
->column_highlight
1113 && option_cursor_beyond_eol
1114 && edit
->mark1
!= edit
->mark2
1115 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1119 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1123 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1129 edit_cursor_move (edit
, -cw
);
1133 /* --------------------------------------------------------------------------------------------- */
1134 /** Up or down cursor moving.
1135 direction = TRUE - move up
1140 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1143 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1152 edit
->force
|= REDRAW_PAGE
;
1156 edit_scroll_upward (edit
, i
);
1158 edit_scroll_downward (edit
, i
);
1160 p
= edit_bol (edit
, edit
->curs1
);
1162 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1164 edit_cursor_move (edit
, p
- edit
->curs1
);
1166 edit_move_to_prev_col (edit
, p
);
1168 /* search start of current multibyte char (like CJK) */
1169 if (edit
->curs1
+ 1 < edit
->last_byte
)
1171 edit_right_char_move_cmd (edit
);
1172 edit_left_char_move_cmd (edit
);
1175 edit
->search_start
= edit
->curs1
;
1176 edit
->found_len
= 0;
1179 /* --------------------------------------------------------------------------------------------- */
1182 edit_right_delete_word (WEdit
* edit
)
1187 if (edit
->curs1
>= edit
->last_byte
)
1189 c1
= edit_delete (edit
, 1);
1190 c2
= edit_get_byte (edit
, edit
->curs1
);
1191 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1193 if (!(my_type_of (c1
) & my_type_of (c2
)))
1198 /* --------------------------------------------------------------------------------------------- */
1201 edit_left_delete_word (WEdit
* edit
)
1206 if (edit
->curs1
<= 0)
1208 c1
= edit_backspace (edit
, 1);
1209 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1210 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1212 if (!(my_type_of (c1
) & my_type_of (c2
)))
1217 /* --------------------------------------------------------------------------------------------- */
1219 the start column position is not recorded, and hence does not
1220 undo as it happed. But who would notice.
1224 edit_do_undo (WEdit
* edit
)
1229 edit
->stack_disable
= 1; /* don't record undo's onto undo stack! */
1231 while ((ac
= pop_action (edit
)) < KEY_PRESS
)
1238 edit_cursor_move (edit
, 1);
1241 edit_cursor_move (edit
, -1);
1244 edit_backspace (edit
, 1);
1247 edit_delete (edit
, 1);
1250 edit
->column_highlight
= 1;
1253 edit
->column_highlight
= 0;
1256 if (ac
>= 256 && ac
< 512)
1257 edit_insert_ahead (edit
, ac
- 256);
1258 if (ac
>= 0 && ac
< 256)
1259 edit_insert (edit
, ac
);
1261 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1263 edit
->mark1
= ac
- MARK_1
;
1264 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1266 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1268 edit
->mark2
= ac
- MARK_2
;
1269 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1272 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1275 if (edit
->start_display
> ac
- KEY_PRESS
)
1277 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1278 edit
->force
|= REDRAW_PAGE
;
1280 else if (edit
->start_display
< ac
- KEY_PRESS
)
1282 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1283 edit
->force
|= REDRAW_PAGE
;
1285 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1286 edit_update_curs_row (edit
);
1289 edit
->stack_disable
= 0;
1292 /* --------------------------------------------------------------------------------------------- */
1295 edit_delete_to_line_end (WEdit
* edit
)
1297 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1301 edit_delete (edit
, 1);
1305 /* --------------------------------------------------------------------------------------------- */
1308 edit_delete_to_line_begin (WEdit
* edit
)
1310 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1314 edit_backspace (edit
, 1);
1318 /* --------------------------------------------------------------------------------------------- */
1321 is_aligned_on_a_tab (WEdit
* edit
)
1323 edit_update_curs_col (edit
);
1324 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1325 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1328 /* --------------------------------------------------------------------------------------------- */
1331 right_of_four_spaces (WEdit
* edit
)
1334 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1335 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1337 return is_aligned_on_a_tab (edit
);
1341 /* --------------------------------------------------------------------------------------------- */
1344 left_of_four_spaces (WEdit
* edit
)
1347 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1348 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1350 return is_aligned_on_a_tab (edit
);
1354 /* --------------------------------------------------------------------------------------------- */
1357 edit_auto_indent (WEdit
* edit
)
1362 /* use the previous line as a template */
1363 p
= edit_move_backward (edit
, p
, 1);
1364 /* copy the leading whitespace of the line */
1366 { /* no range check - the line _is_ \n-terminated */
1367 c
= edit_get_byte (edit
, p
++);
1368 if (c
!= ' ' && c
!= '\t')
1370 edit_insert (edit
, c
);
1374 /* --------------------------------------------------------------------------------------------- */
1377 edit_double_newline (WEdit
* edit
)
1379 edit_insert (edit
, '\n');
1380 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1382 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1384 edit
->force
|= REDRAW_PAGE
;
1385 edit_insert (edit
, '\n');
1388 /* --------------------------------------------------------------------------------------------- */
1391 edit_tab_cmd (WEdit
* edit
)
1395 if (option_fake_half_tabs
)
1397 if (is_in_indent (edit
))
1399 /*insert a half tab (usually four spaces) unless there is a
1400 half tab already behind, then delete it and insert a
1402 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1404 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1405 edit_backspace (edit
, 1);
1406 edit_insert (edit
, '\t');
1410 insert_spaces_tab (edit
, 1);
1415 if (option_fill_tabs_with_spaces
)
1417 insert_spaces_tab (edit
, 0);
1421 edit_insert (edit
, '\t');
1425 /* --------------------------------------------------------------------------------------------- */
1428 check_and_wrap_line (WEdit
* edit
)
1431 if (!option_typewriter_wrap
)
1433 edit_update_curs_col (edit
);
1434 if (edit
->curs_col
< option_word_wrap_line_length
)
1440 c
= edit_get_byte (edit
, curs
);
1441 if (c
== '\n' || curs
<= 0)
1443 edit_insert (edit
, '\n');
1446 if (c
== ' ' || c
== '\t')
1448 int current
= edit
->curs1
;
1449 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1450 edit_insert (edit
, '\n');
1451 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1457 /* --------------------------------------------------------------------------------------------- */
1458 /** this find the matching bracket in either direction, and sets edit->bracket */
1461 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1463 const char *const b
= "{}{[][()(", *p
;
1464 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1465 unsigned long j
= 0;
1467 edit_update_curs_row (edit
);
1468 c
= edit_get_byte (edit
, edit
->curs1
);
1471 if (!furthest_bracket_search
)
1472 furthest_bracket_search
--;
1473 /* not on a bracket at all */
1476 /* the matching bracket */
1478 /* going left or right? */
1479 if (strchr ("{[(", c
))
1481 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1483 /* out of buffer? */
1484 if (q
>= edit
->last_byte
|| q
< 0)
1486 a
= edit_get_byte (edit
, q
);
1487 /* don't want to eat CPU */
1488 if (j
++ > furthest_bracket_search
)
1490 /* out of screen? */
1493 if (q
< edit
->start_display
)
1495 /* count lines if searching downward */
1496 if (inc
> 0 && a
== '\n')
1497 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
1500 /* count bracket depth */
1501 i
+= (a
== c
) - (a
== d
);
1502 /* return if bracket depth is zero */
1510 /* --------------------------------------------------------------------------------------------- */
1513 edit_goto_matching_bracket (WEdit
* edit
)
1517 q
= edit_get_bracket (edit
, 0, 0);
1520 edit
->bracket
= edit
->curs1
;
1521 edit
->force
|= REDRAW_PAGE
;
1522 edit_cursor_move (edit
, q
- edit
->curs1
);
1526 /* --------------------------------------------------------------------------------------------- */
1529 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
1533 if (edit
->macro_depth
++ > 256)
1535 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1536 edit
->macro_depth
--;
1539 edit
->force
|= REDRAW_PAGE
;
1542 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
1544 edit_update_screen (edit
);
1545 edit
->macro_depth
--;
1548 /* --------------------------------------------------------------------------------------------- */
1549 /** User edit menu, like user menu (F2) but only in editor. */
1552 user_menu (WEdit
* edit
)
1556 long start_mark
, end_mark
;
1559 block_file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
1561 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1563 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1565 /* run shell scripts from menu */
1566 user_menu_cmd (edit
);
1568 if ((mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1575 /* i.e. we have marked block */
1576 rc
= edit_block_delete_cmd (edit
);
1580 edit_insert_file (edit
, block_file
);
1582 /* truncate block file */
1583 fd
= fopen (block_file
, "w");
1587 edit_refresh_cmd (edit
);
1588 edit
->force
|= REDRAW_COMPLETELY
;
1590 g_free (block_file
);
1593 /* --------------------------------------------------------------------------------------------- */
1594 /*** public functions ****************************************************************************/
1595 /* --------------------------------------------------------------------------------------------- */
1598 edit_get_byte (WEdit
* edit
, long byte_index
)
1601 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1604 if (byte_index
>= edit
->curs1
)
1606 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1607 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1611 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1615 /* --------------------------------------------------------------------------------------------- */
1618 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1621 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1624 if (byte_index
>= edit
->curs1
)
1626 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1627 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1628 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1632 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1633 (byte_index
& M_EDIT_BUF_SIZE
));
1637 /* --------------------------------------------------------------------------------------------- */
1640 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1644 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1650 if (byte_index
>= edit
->curs1
)
1652 p
= edit
->curs1
+ edit
->curs2
- 1;
1653 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1654 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1658 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1662 /* --------------------------------------------------------------------------------------------- */
1665 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1670 gchar
*next_ch
= NULL
;
1673 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1679 str
= edit_get_byte_ptr (edit
, byte_index
);
1687 res
= g_utf8_get_char_validated (str
, -1);
1697 /* Calculate UTF-8 char width */
1698 next_ch
= g_utf8_next_char (str
);
1701 width
= next_ch
- str
;
1709 *char_width
= width
;
1713 /* --------------------------------------------------------------------------------------------- */
1716 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1718 gchar
*str
, *buf
= NULL
;
1721 gchar
*next_ch
= NULL
;
1727 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1733 ch
= edit_get_utf (edit
, byte_index
, &width
);
1737 *char_width
= width
;
1741 str
= edit_get_byte_ptr (edit
, byte_index
);
1742 buf
= edit_get_buf_ptr (edit
, byte_index
);
1743 if (str
== NULL
|| buf
== NULL
)
1748 /* get prev utf8 char */
1750 str
= g_utf8_find_prev_char (buf
, str
);
1752 res
= g_utf8_get_char_validated (str
, -1);
1762 /* Calculate UTF-8 char width */
1763 next_ch
= g_utf8_next_char (str
);
1766 width
= next_ch
- str
;
1774 *char_width
= width
;
1778 /* --------------------------------------------------------------------------------------------- */
1781 edit_get_write_filter (const char *write_name
, const char *filename
)
1784 char *p
, *writename
;
1786 i
= edit_find_filter (filename
);
1790 writename
= name_quote (write_name
, 0);
1791 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1796 /* --------------------------------------------------------------------------------------------- */
1799 edit_write_stream (WEdit
* edit
, FILE * f
)
1803 if (edit
->lb
== LB_ASIS
)
1805 for (i
= 0; i
< edit
->last_byte
; i
++)
1806 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1811 /* change line breaks */
1812 for (i
= 0; i
< edit
->last_byte
; i
++)
1814 unsigned char c
= edit_get_byte (edit
, i
);
1816 if (!(c
== '\n' || c
== '\r'))
1818 /* not line break */
1819 if (fputc (c
, f
) < 0)
1823 { /* (c == '\n' || c == '\r') */
1824 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1828 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1829 /* put one line break unconditionally */
1830 if (fputc ('\n', f
) < 0)
1833 i
++; /* 2 chars are processed */
1835 if (c
== '\r' && c1
== '\n')
1836 /* Windows line break; go to the next char */
1839 if (c
== '\r' && c1
== '\r')
1841 /* two Macintosh line breaks; put second line break */
1842 if (fputc ('\n', f
) < 0)
1847 if (fputc (c1
, f
) < 0)
1851 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1852 /* put one line break unconditionally */
1853 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1856 if (c
== '\r' && c1
== '\n')
1857 /* Windows line break; go to the next char */
1861 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1862 /* put one line break unconditionally */
1863 if (fputc ('\r', f
) < 0)
1866 i
++; /* 2 chars are processed */
1868 if (c
== '\r' && c1
== '\n')
1869 /* Windows line break; go to the next char */
1872 if (c
== '\n' && c1
== '\n')
1874 /* two Windows line breaks; put second line break */
1875 if (fputc ('\r', f
) < 0)
1880 if (fputc (c1
, f
) < 0)
1883 case LB_ASIS
: /* default without changes */
1889 return edit
->last_byte
;
1892 /* --------------------------------------------------------------------------------------------- */
1893 /** inserts a file at the cursor, returns 1 on success */
1895 edit_insert_file (WEdit
* edit
, const char *filename
)
1899 p
= edit_get_filter (filename
);
1903 long current
= edit
->curs1
;
1904 f
= (FILE *) popen (p
, "r");
1907 edit_insert_stream (edit
, f
);
1908 edit_cursor_move (edit
, current
- edit
->curs1
);
1912 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1913 edit_error_dialog (_("Error"), errmsg
);
1922 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1923 edit_error_dialog (_("Error"), errmsg
);
1932 int i
, file
, blocklen
;
1933 long current
= edit
->curs1
;
1934 int vertical_insertion
= 0;
1936 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
1939 buf
= g_malloc0 (TEMP_BUF_LEN
);
1940 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
1943 /* if contain signature VERTICAL_MAGIC tnen it vertical block */
1944 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
1945 vertical_insertion
= 1;
1947 mc_lseek (file
, 0, SEEK_SET
);
1949 if (vertical_insertion
)
1950 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
1952 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
1953 for (i
= 0; i
< blocklen
; i
++)
1954 edit_insert (edit
, buf
[i
]);
1955 edit_cursor_move (edit
, current
- edit
->curs1
);
1964 /* --------------------------------------------------------------------------------------------- */
1966 * Fill in the edit structure. Return NULL on failure. Pass edit as
1967 * NULL to allocate a new structure.
1969 * If line is 0, try to restore saved position. Otherwise put the
1970 * cursor on that line and show it in the middle of the screen.
1974 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
1977 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
1978 if (option_line_state
)
1980 option_line_state_width
= LINE_STATE_WIDTH
;
1984 option_line_state_width
= 0;
1990 * Expand option_whole_chars_search by national letters using
1994 static char option_whole_chars_search_buf
[256];
1996 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
1999 size_t len
= str_term_width1 (option_whole_chars_search
);
2001 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2003 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2005 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2007 option_whole_chars_search_buf
[len
++] = i
;
2011 option_whole_chars_search_buf
[len
] = 0;
2012 option_whole_chars_search
= option_whole_chars_search_buf
;
2014 #endif /* ENABLE_NLS */
2015 edit
= g_malloc0 (sizeof (WEdit
));
2016 edit
->search
= NULL
;
2019 edit_purge_widget (edit
);
2020 edit
->num_widget_lines
= lines
;
2022 edit
->num_widget_columns
= columns
;
2023 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2024 edit
->stat1
.st_uid
= getuid ();
2025 edit
->stat1
.st_gid
= getgid ();
2026 edit
->stat1
.st_mtime
= 0;
2028 edit
->force
|= REDRAW_PAGE
;
2029 edit_set_filename (edit
, filename
);
2030 edit
->stack_size
= START_STACK_SIZE
;
2031 edit
->stack_size_mask
= START_STACK_SIZE
- 1;
2032 edit
->undo_stack
= g_malloc0 ((edit
->stack_size
+ 10) * sizeof (long));
2035 edit
->converter
= str_cnv_from_term
;
2036 edit_set_codeset (edit
);
2038 if (edit_load_file (edit
))
2040 /* edit_load_file already gives an error message */
2046 edit
->loading_done
= 1;
2049 edit_load_syntax (edit
, NULL
, NULL
);
2052 edit_get_syntax_color (edit
, -1, &color
);
2055 /* load saved cursor position */
2056 if ((line
== 0) && option_save_position
)
2058 edit_load_position (edit
);
2064 edit_move_display (edit
, line
- 1);
2065 edit_move_to_line (edit
, line
- 1);
2073 /* --------------------------------------------------------------------------------------------- */
2074 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2077 edit_clean (WEdit
* edit
)
2084 /* a stale lock, remove it */
2086 edit
->locked
= edit_unlock_file (edit
);
2088 /* save cursor position */
2089 if (option_save_position
)
2090 edit_save_position (edit
);
2091 else if (edit
->serialized_bookmarks
!= NULL
)
2092 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2094 /* File specified on the mcedit command line and never saved */
2095 if (edit
->delete_file
)
2096 unlink (edit
->filename
);
2098 edit_free_syntax_rules (edit
);
2099 book_mark_flush (edit
, -1);
2100 for (; j
<= MAXBUFF
; j
++)
2102 g_free (edit
->buffers1
[j
]);
2103 g_free (edit
->buffers2
[j
]);
2106 g_free (edit
->undo_stack
);
2107 g_free (edit
->filename
);
2110 mc_search_free (edit
->search
);
2111 edit
->search
= NULL
;
2113 if (edit
->converter
!= str_cnv_from_term
)
2114 str_close_conv (edit
->converter
);
2116 edit_purge_widget (edit
);
2121 /* --------------------------------------------------------------------------------------------- */
2122 /** returns 1 on success */
2125 edit_renew (WEdit
* edit
)
2127 int lines
= edit
->num_widget_lines
;
2128 int columns
= edit
->num_widget_columns
;
2131 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
2134 /* --------------------------------------------------------------------------------------------- */
2136 * Load a new file into the editor. If it fails, preserve the old file.
2137 * To do it, allocate a new widget, initialize it and, if the new file
2138 * was loaded, copy the data to the old widget.
2139 * Return 1 on success, 0 on failure.
2143 edit_reload (WEdit
* edit
, const char *filename
)
2146 int lines
= edit
->num_widget_lines
;
2147 int columns
= edit
->num_widget_columns
;
2149 e
= g_malloc0 (sizeof (WEdit
));
2150 e
->widget
= edit
->widget
;
2151 if (!edit_init (e
, lines
, columns
, filename
, 0))
2157 memcpy (edit
, e
, sizeof (WEdit
));
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Load a new file into the editor and set line. If it fails, preserve the old file.
2165 * To do it, allocate a new widget, initialize it and, if the new file
2166 * was loaded, copy the data to the old widget.
2167 * Return 1 on success, 0 on failure.
2171 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2174 int lines
= edit
->num_widget_lines
;
2175 int columns
= edit
->num_widget_columns
;
2177 e
= g_malloc0 (sizeof (WEdit
));
2178 e
->widget
= edit
->widget
;
2179 if (!edit_init (e
, lines
, columns
, filename
, line
))
2185 memcpy (edit
, e
, sizeof (WEdit
));
2190 /* --------------------------------------------------------------------------------------------- */
2193 edit_set_codeset (WEdit
* edit
)
2198 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
2203 conv
= str_crt_conv_from (cp_id
);
2204 if (conv
!= INVALID_CONV
)
2206 if (edit
->converter
!= str_cnv_from_term
)
2207 str_close_conv (edit
->converter
);
2208 edit
->converter
= conv
;
2213 edit
->utf8
= str_isutf8 (cp_id
);
2220 /* --------------------------------------------------------------------------------------------- */
2222 Recording stack for undo:
2223 The following is an implementation of a compressed stack. Identical
2224 pushes are recorded by a negative prefix indicating the number of times the
2225 same char was pushed. This saves space for repeated curs-left or curs-right
2242 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2243 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2244 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2245 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2248 The only way the cursor moves or the buffer is changed is through the routines:
2249 insert, backspace, insert_ahead, delete, and cursor_move.
2250 These record the reverse undo movements onto the stack each time they are
2253 Each key press results in a set of actions (insert; delete ...). So each time
2254 a key is pressed the current position of start_display is pushed as
2255 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2256 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2257 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2262 edit_push_action (WEdit
* edit
, long c
, ...)
2264 unsigned long sp
= edit
->stack_pointer
;
2268 /* first enlarge the stack if necessary */
2269 if (sp
> edit
->stack_size
- 10)
2271 if (option_max_undo
< 256)
2272 option_max_undo
= 256;
2273 if (edit
->stack_size
< (unsigned long) option_max_undo
)
2275 t
= g_realloc (edit
->undo_stack
, (edit
->stack_size
* 2 + 10) * sizeof (long));
2278 edit
->undo_stack
= t
;
2279 edit
->stack_size
<<= 1;
2280 edit
->stack_size_mask
= edit
->stack_size
- 1;
2284 spm1
= (edit
->stack_pointer
- 1) & edit
->stack_size_mask
;
2285 if (edit
->stack_disable
)
2288 #ifdef FAST_MOVE_CURSOR
2289 if (c
== CURS_LEFT_LOTS
|| c
== CURS_RIGHT_LOTS
)
2292 edit
->undo_stack
[sp
] = (c
== CURS_LEFT_LOTS
) ? CURS_LEFT
: CURS_RIGHT
;
2293 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
2295 c
= -(va_arg (ap
, int));
2299 #endif /* ! FAST_MOVE_CURSOR */
2300 if (edit
->stack_bottom
!= sp
2301 && spm1
!= edit
->stack_bottom
2302 && ((sp
- 2) & edit
->stack_size_mask
) != edit
->stack_bottom
)
2305 if (edit
->undo_stack
[spm1
] < 0)
2307 d
= edit
->undo_stack
[(sp
- 2) & edit
->stack_size_mask
];
2310 if (edit
->undo_stack
[spm1
] > -1000000000)
2312 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2313 edit
->undo_stack
[spm1
]--;
2317 /* #define NO_STACK_CURSMOVE_ANIHILATION */
2318 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2319 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
2320 { /* a left then a right anihilate each other */
2321 if (edit
->undo_stack
[spm1
] == -2)
2322 edit
->stack_pointer
= spm1
;
2324 edit
->undo_stack
[spm1
]++;
2331 d
= edit
->undo_stack
[spm1
];
2335 return; /* --> no need to push multiple do-nothings */
2336 edit
->undo_stack
[sp
] = -2;
2339 #ifndef NO_STACK_CURSMOVE_ANIHILATION
2340 else if ((c
== CURS_LEFT
&& d
== CURS_RIGHT
) || (c
== CURS_RIGHT
&& d
== CURS_LEFT
))
2341 { /* a left then a right anihilate each other */
2342 edit
->stack_pointer
= spm1
;
2348 edit
->undo_stack
[sp
] = c
;
2351 edit
->stack_pointer
= (edit
->stack_pointer
+ 1) & edit
->stack_size_mask
;
2353 /* if the sp wraps round and catches the stack_bottom then erase
2354 * the first set of actions on the stack to make space - by moving
2355 * stack_bottom forward one "key press" */
2356 c
= (edit
->stack_pointer
+ 2) & edit
->stack_size_mask
;
2357 if ((unsigned long) c
== edit
->stack_bottom
||
2358 (((unsigned long) c
+ 1) & edit
->stack_size_mask
) == edit
->stack_bottom
)
2361 edit
->stack_bottom
= (edit
->stack_bottom
+ 1) & edit
->stack_size_mask
;
2363 while (edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
2364 && edit
->stack_bottom
!= edit
->stack_pointer
);
2366 /*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
2367 if (edit
->stack_pointer
!= edit
->stack_bottom
2368 && edit
->undo_stack
[edit
->stack_bottom
] < KEY_PRESS
)
2369 edit
->stack_bottom
= edit
->stack_pointer
= 0;
2372 /* --------------------------------------------------------------------------------------------- */
2374 Basic low level single character buffer alterations and movements at the cursor.
2375 Returns char passed over, inserted or removed.
2379 edit_insert (WEdit
* edit
, int c
)
2381 /* check if file has grown to large */
2382 if (edit
->last_byte
>= SIZE_LIMIT
)
2385 /* first we must update the position of the display window */
2386 if (edit
->curs1
< edit
->start_display
)
2388 edit
->start_display
++;
2393 /* Mark file as modified, unless the file hasn't been fully loaded */
2394 if (edit
->loading_done
)
2396 edit_modification (edit
);
2399 /* now we must update some info on the file and check if a redraw is required */
2402 if (edit
->book_mark
)
2403 book_mark_inc (edit
, edit
->curs_line
);
2405 edit
->total_lines
++;
2406 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2409 /* save the reverse command onto the undo stack */
2410 edit_push_action (edit
, BACKSPACE
);
2412 /* update markers */
2413 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2414 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2415 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2417 /* add a new buffer if we've reached the end of the last one */
2418 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2419 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2421 /* perform the insertion */
2422 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2423 = (unsigned char) c
;
2425 /* update file length */
2428 /* update cursor position */
2432 /* --------------------------------------------------------------------------------------------- */
2433 /** same as edit_insert and move left */
2436 edit_insert_ahead (WEdit
* edit
, int c
)
2438 if (edit
->last_byte
>= SIZE_LIMIT
)
2441 if (edit
->curs1
< edit
->start_display
)
2443 edit
->start_display
++;
2447 edit_modification (edit
);
2450 if (edit
->book_mark
)
2451 book_mark_inc (edit
, edit
->curs_line
);
2452 edit
->total_lines
++;
2453 edit
->force
|= REDRAW_AFTER_CURSOR
;
2455 edit_push_action (edit
, DELCHAR
);
2457 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2458 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2459 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2461 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2462 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2463 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2464 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2471 /* --------------------------------------------------------------------------------------------- */
2474 edit_delete (WEdit
* edit
, const int byte_delete
)
2484 /* if byte_delete = 1 then delete only one byte not multibyte char */
2485 if (edit
->utf8
&& byte_delete
== 0)
2487 edit_get_utf (edit
, edit
->curs1
, &cw
);
2491 for (i
= 1; i
<= cw
; i
++)
2493 if (edit
->mark1
> edit
->curs1
)
2496 edit
->end_mark_curs
--;
2498 if (edit
->mark2
> edit
->curs1
)
2500 if (edit
->last_get_rule
> edit
->curs1
)
2501 edit
->last_get_rule
--;
2503 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2505 1) & M_EDIT_BUF_SIZE
) - 1];
2507 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2509 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2510 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2514 edit_push_action (edit
, p
+ 256);
2517 edit_modification (edit
);
2520 if (edit
->book_mark
)
2521 book_mark_dec (edit
, edit
->curs_line
);
2522 edit
->total_lines
--;
2523 edit
->force
|= REDRAW_AFTER_CURSOR
;
2525 if (edit
->curs1
< edit
->start_display
)
2527 edit
->start_display
--;
2535 /* --------------------------------------------------------------------------------------------- */
2537 #ifdef FAST_MOVE_CURSOR
2539 edit_move_backward_lots (WEdit
* edit
, long increment
)
2542 unsigned char *p
= NULL
;
2544 if (increment
> edit
->curs1
)
2545 increment
= edit
->curs1
;
2548 edit_push_action (edit
, CURS_RIGHT_LOTS
, increment
);
2550 t
= r
= EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
);
2553 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
2558 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
2559 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- r
, r
);
2566 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
-
2567 s
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
], s
);
2568 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
2569 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
2572 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] + t
- r
,
2573 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
2574 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
2579 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2582 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
2584 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2591 s
= edit
->curs1
& M_EDIT_BUF_SIZE
;
2602 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
2603 EDIT_BUF_SIZE
- t
, edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] + s
- t
, t
);
2608 p
= edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
];
2609 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = 0;
2612 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] +
2614 edit
->buffers1
[(edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1] +
2615 EDIT_BUF_SIZE
- (r
- s
), r
- s
);
2620 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2623 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = p
;
2625 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2630 return edit_get_byte (edit
, edit
->curs1
);
2632 #endif /* ! FAST_MOVE_CURSOR */
2634 /* --------------------------------------------------------------------------------------------- */
2635 /** moves the cursor right or left: increment positive or negative respectively */
2638 edit_cursor_move (WEdit
* edit
, long increment
)
2640 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2642 #ifdef FAST_MOVE_CURSOR
2643 if (increment
< -256)
2645 edit
->force
|= REDRAW_PAGE
;
2646 edit_move_backward_lots (edit
, -increment
);
2649 #endif /* ! FAST_MOVE_CURSOR */
2653 for (; increment
< 0; increment
++)
2658 edit_push_action (edit
, CURS_RIGHT
);
2660 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2661 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2662 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2663 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2664 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2666 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2667 1) & M_EDIT_BUF_SIZE
];
2668 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2670 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2671 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2677 edit
->force
|= REDRAW_LINE_BELOW
;
2682 else if (increment
> 0)
2684 for (; increment
> 0; increment
--)
2689 edit_push_action (edit
, CURS_LEFT
);
2691 c
= edit_get_byte (edit
, edit
->curs1
);
2692 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2693 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2694 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2696 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2698 1) & M_EDIT_BUF_SIZE
) - 1];
2699 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2701 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2702 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2708 edit
->force
|= REDRAW_LINE_ABOVE
;
2714 /* These functions return positions relative to lines */
2716 /* --------------------------------------------------------------------------------------------- */
2717 /** returns index of last char on line + 1 */
2720 edit_eol (WEdit
* edit
, long current
)
2722 if (current
>= edit
->last_byte
)
2723 return edit
->last_byte
;
2726 if (edit_get_byte (edit
, current
) == '\n')
2731 /* --------------------------------------------------------------------------------------------- */
2732 /** returns index of first char on line */
2735 edit_bol (WEdit
* edit
, long current
)
2741 if (edit_get_byte (edit
, current
- 1) == '\n')
2746 /* --------------------------------------------------------------------------------------------- */
2749 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2752 if (upto
> edit
->last_byte
)
2753 upto
= edit
->last_byte
;
2756 while (current
< upto
)
2757 if (edit_get_byte (edit
, current
++) == '\n')
2762 /* --------------------------------------------------------------------------------------------- */
2763 /* If lines is zero this returns the count of lines from current to upto. */
2764 /* If upto is zero returns index of lines forward current. */
2767 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2771 return edit_count_lines (edit
, current
, upto
);
2780 next
= edit_eol (edit
, current
) + 1;
2781 if (next
> edit
->last_byte
)
2790 /* --------------------------------------------------------------------------------------------- */
2791 /** Returns offset of 'lines' lines up from current */
2794 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2798 current
= edit_bol (edit
, current
);
2799 while ((lines
--) && current
!= 0)
2800 current
= edit_bol (edit
, current
- 1);
2804 /* --------------------------------------------------------------------------------------------- */
2805 /* If cols is zero this returns the count of columns from current to upto. */
2806 /* If upto is zero returns index of cols across from current. */
2809 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2820 q
= edit
->last_byte
+ 2;
2822 for (col
= 0, p
= current
; p
< q
; p
++)
2836 orig_c
= c
= edit_get_byte (edit
, p
);
2840 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2845 if (g_unichar_iswide (utf_ch
))
2848 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2852 c
= convert_to_display_c (c
);
2854 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2862 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
2863 /* '\r' is shown as ^M, so we must advance 2 characters */
2864 /* Caret notation for control characters */
2872 /* --------------------------------------------------------------------------------------------- */
2873 /** returns the current column position of the cursor */
2876 edit_get_col (WEdit
* edit
)
2878 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2881 /* --------------------------------------------------------------------------------------------- */
2882 /* Scrolling functions */
2883 /* --------------------------------------------------------------------------------------------- */
2886 edit_update_curs_row (WEdit
* edit
)
2888 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
2891 /* --------------------------------------------------------------------------------------------- */
2894 edit_update_curs_col (WEdit
* edit
)
2896 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2899 /* --------------------------------------------------------------------------------------------- */
2902 edit_get_curs_col (const WEdit
* edit
)
2904 return edit
->curs_col
;
2907 /* --------------------------------------------------------------------------------------------- */
2908 /** moves the display start position up by i lines */
2911 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
2913 unsigned long lines_above
= edit
->start_line
;
2914 if (i
> lines_above
)
2918 edit
->start_line
-= i
;
2919 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
2920 edit
->force
|= REDRAW_PAGE
;
2921 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2923 edit_update_curs_row (edit
);
2927 /* --------------------------------------------------------------------------------------------- */
2928 /** returns 1 if could scroll, 0 otherwise */
2931 edit_scroll_downward (WEdit
* edit
, int i
)
2934 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
2935 if (lines_below
> 0)
2937 if (i
> lines_below
)
2939 edit
->start_line
+= i
;
2940 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
2941 edit
->force
|= REDRAW_PAGE
;
2942 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2944 edit_update_curs_row (edit
);
2947 /* --------------------------------------------------------------------------------------------- */
2950 edit_scroll_right (WEdit
* edit
, int i
)
2952 edit
->force
|= REDRAW_PAGE
;
2953 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2954 edit
->start_col
-= i
;
2957 /* --------------------------------------------------------------------------------------------- */
2960 edit_scroll_left (WEdit
* edit
, int i
)
2962 if (edit
->start_col
)
2964 edit
->start_col
+= i
;
2965 if (edit
->start_col
> 0)
2966 edit
->start_col
= 0;
2967 edit
->force
|= REDRAW_PAGE
;
2968 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2972 /* --------------------------------------------------------------------------------------------- */
2973 /* high level cursor movement commands */
2974 /* --------------------------------------------------------------------------------------------- */
2977 edit_move_to_prev_col (WEdit
* edit
, long p
)
2979 int prev
= edit
->prev_col
;
2980 int over
= edit
->over_col
;
2981 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
2983 if (option_cursor_beyond_eol
)
2985 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
2986 edit_eol (edit
, edit
->curs1
));
2988 if (line_len
< prev
+ edit
->over_col
)
2990 edit
->over_col
= prev
+ over
- line_len
;
2991 edit
->prev_col
= line_len
;
2992 edit
->curs_col
= line_len
;
2996 edit
->curs_col
= prev
+ over
;
2997 edit
->prev_col
= edit
->curs_col
;
3004 if (is_in_indent (edit
) && option_fake_half_tabs
)
3006 edit_update_curs_col (edit
);
3008 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3010 int q
= edit
->curs_col
;
3011 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3012 p
= edit_bol (edit
, edit
->curs1
);
3013 edit_cursor_move (edit
,
3014 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3016 if (!left_of_four_spaces (edit
))
3017 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3023 /* --------------------------------------------------------------------------------------------- */
3026 line_is_blank (WEdit
* edit
, long line
)
3028 return is_blank (edit
, edit_find_line (edit
, line
));
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** move cursor to line 'line' */
3035 edit_move_to_line (WEdit
* e
, long line
)
3037 if (line
< e
->curs_line
)
3038 edit_move_up (e
, e
->curs_line
- line
, 0);
3040 edit_move_down (e
, line
- e
->curs_line
, 0);
3041 edit_scroll_screen_over_cursor (e
);
3044 /* --------------------------------------------------------------------------------------------- */
3045 /** scroll window so that first visible line is 'line' */
3048 edit_move_display (WEdit
* e
, long line
)
3050 if (line
< e
->start_line
)
3051 edit_scroll_upward (e
, e
->start_line
- line
);
3053 edit_scroll_downward (e
, line
- e
->start_line
);
3056 /* --------------------------------------------------------------------------------------------- */
3057 /** save markers onto undo stack */
3060 edit_push_markers (WEdit
* edit
)
3062 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
3063 edit_push_action (edit
, MARK_2
+ edit
->mark2
);
3066 /* --------------------------------------------------------------------------------------------- */
3069 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3078 /* --------------------------------------------------------------------------------------------- */
3079 /** highlight marker toggle */
3082 edit_mark_cmd (WEdit
* edit
, int unmark
)
3084 edit_push_markers (edit
);
3087 edit_set_markers (edit
, 0, 0, 0, 0);
3088 edit
->force
|= REDRAW_PAGE
;
3092 if (edit
->mark2
>= 0)
3094 edit
->end_mark_curs
= -1;
3095 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3096 edit
->curs_col
+ edit
->over_col
);
3097 edit
->force
|= REDRAW_PAGE
;
3101 edit
->end_mark_curs
= edit
->curs1
;
3102 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3103 edit
->curs_col
+ edit
->over_col
);
3108 /* --------------------------------------------------------------------------------------------- */
3111 edit_delete_line (WEdit
* edit
)
3114 * Delete right part of the line.
3115 * Note that edit_get_byte() returns '\n' when byte position is
3118 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3120 (void) edit_delete (edit
, 1);
3125 * Note that edit_delete() will not corrupt anything if called while
3126 * cursor position is EOF.
3128 (void) edit_delete (edit
, 1);
3131 * Delete left part of the line.
3132 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3134 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3136 (void) edit_backspace (edit
, 1);
3140 /* --------------------------------------------------------------------------------------------- */
3143 insert_spaces_tab (WEdit
* edit
, int half
)
3146 edit_update_curs_col (edit
);
3147 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3148 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3151 edit_insert (edit
, ' ');
3156 /* --------------------------------------------------------------------------------------------- */
3159 edit_indent_width (WEdit
* edit
, long p
)
3162 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3164 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3167 /* --------------------------------------------------------------------------------------------- */
3170 edit_insert_indent (WEdit
* edit
, int indent
)
3172 if (!option_fill_tabs_with_spaces
)
3174 while (indent
>= TAB_SIZE
)
3176 edit_insert (edit
, '\t');
3180 while (indent
-- > 0)
3181 edit_insert (edit
, ' ');
3184 /* --------------------------------------------------------------------------------------------- */
3187 edit_push_key_press (WEdit
* edit
)
3189 edit_push_action (edit
, KEY_PRESS
+ edit
->start_display
);
3190 if (edit
->mark2
== -1)
3191 edit_push_action (edit
, MARK_1
+ edit
->mark1
);
3194 /* --------------------------------------------------------------------------------------------- */
3197 edit_find_bracket (WEdit
* edit
)
3199 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3200 if (last_bracket
!= edit
->bracket
)
3201 edit
->force
|= REDRAW_PAGE
;
3202 last_bracket
= edit
->bracket
;
3205 /* --------------------------------------------------------------------------------------------- */
3207 * This executes a command as though the user initiated it through a key
3208 * press. Callback with WIDGET_KEY as a message calls this after
3209 * translating the key press. This function can be used to pass any
3210 * command to the editor. Note that the screen wouldn't update
3211 * automatically. Either of command or char_for_insertion must be
3212 * passed as -1. Commands are executed, and char_for_insertion is
3213 * inserted at the cursor.
3217 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3219 if (command
== CK_Begin_Record_Macro
)
3222 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3225 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
3227 edit
->force
|= REDRAW_COMPLETELY
;
3228 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
3232 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
3234 edit
->macro
[edit
->macro_i
].command
= command
;
3235 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
3237 /* record the beginning of a set of editing actions initiated by a key press */
3238 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
3239 edit_push_key_press (edit
);
3241 edit_execute_cmd (edit
, command
, char_for_insertion
);
3242 if (edit
->column_highlight
)
3243 edit
->force
|= REDRAW_PAGE
;
3246 /* --------------------------------------------------------------------------------------------- */
3248 This executes a command at a lower level than macro recording.
3249 It also does not push a key_press onto the undo stack. This means
3250 that if it is called many times, a single undo command will undo
3251 all of them. It also does not check for the Undo command.
3254 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3256 edit
->force
|= REDRAW_LINE
;
3258 /* The next key press will unhighlight the found string, so update
3260 if (edit
->found_len
|| edit
->column_highlight
)
3261 edit
->force
|= REDRAW_PAGE
;
3263 if (command
/ 100 == 6)
3264 { /* a highlight command like shift-arrow */
3265 edit
->column_highlight
= 0;
3266 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3268 edit_mark_cmd (edit
, 1); /* clear */
3269 edit_mark_cmd (edit
, 0); /* marking on */
3271 edit
->highlight
= 1;
3274 { /* any other command */
3275 if (edit
->highlight
)
3276 edit_mark_cmd (edit
, 0); /* clear */
3277 edit
->highlight
= 0;
3280 /* first check for undo */
3281 if (command
== CK_Undo
)
3283 edit_do_undo (edit
);
3284 edit
->found_len
= 0;
3285 edit
->prev_col
= edit_get_col (edit
);
3286 edit
->search_start
= edit
->curs1
;
3290 /* An ordinary key press */
3291 if (char_for_insertion
>= 0)
3293 /* if non persistent selection and text selected */
3294 if (!option_persistent_selections
)
3296 if (edit
->mark1
!= edit
->mark2
)
3297 edit_block_delete_cmd (edit
);
3299 if (edit
->overwrite
)
3301 /* remove char only one time, after input first byte, multibyte chars */
3302 if ((!utf8_display
|| edit
->charpoint
== 0)
3303 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3304 edit_delete (edit
, 0);
3306 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3307 edit_insert_over (edit
);
3309 if (char_for_insertion
> 255 && utf8_display
== 0)
3311 unsigned char str
[6 + 1];
3313 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3323 while (str
[i
] != 0 && i
<= 6)
3325 char_for_insertion
= str
[i
];
3326 edit_insert (edit
, char_for_insertion
);
3332 edit_insert (edit
, char_for_insertion
);
3334 if (option_auto_para_formatting
)
3336 format_paragraph (edit
, 0);
3337 edit
->force
|= REDRAW_PAGE
;
3340 check_and_wrap_line (edit
);
3341 edit
->found_len
= 0;
3342 edit
->prev_col
= edit_get_col (edit
);
3343 edit
->search_start
= edit
->curs1
;
3344 edit_find_bracket (edit
);
3352 case CK_Begin_Page_Highlight
:
3353 case CK_End_Page_Highlight
:
3360 if (edit
->mark2
>= 0)
3362 if (!option_persistent_selections
)
3364 if (edit
->column_highlight
)
3365 edit_push_action (edit
, COLUMN_ON
);
3366 edit
->column_highlight
= 0;
3367 edit_mark_cmd (edit
, 1);
3376 case CK_Begin_Page_Highlight
:
3377 case CK_End_Page_Highlight
:
3382 case CK_Word_Left_Highlight
:
3383 case CK_Word_Right_Highlight
:
3384 case CK_Up_Highlight
:
3385 case CK_Down_Highlight
:
3386 case CK_Up_Alt_Highlight
:
3387 case CK_Down_Alt_Highlight
:
3388 if (edit
->mark2
== -1)
3389 break; /*marking is following the cursor: may need to highlight a whole line */
3392 case CK_Left_Highlight
:
3393 case CK_Right_Highlight
:
3394 edit
->force
|= REDRAW_CHAR_ONLY
;
3397 /* basic cursor key commands */
3401 /* if non persistent selection and text selected */
3402 if (!option_persistent_selections
)
3404 if (edit
->mark1
!= edit
->mark2
)
3406 edit_block_delete_cmd (edit
);
3410 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3415 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3417 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3418 edit_backspace (edit
, 1);
3423 if (option_fake_half_tabs
)
3426 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3428 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3429 edit_backspace (edit
, 1);
3434 edit_backspace (edit
, 0);
3437 /* if non persistent selection and text selected */
3438 if (!option_persistent_selections
)
3440 if (edit
->mark1
!= edit
->mark2
)
3442 edit_block_delete_cmd (edit
);
3447 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3448 edit_insert_over (edit
);
3450 if (option_fake_half_tabs
)
3453 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3455 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3456 edit_delete (edit
, 1);
3460 edit_delete (edit
, 0);
3462 case CK_Delete_Word_Left
:
3464 edit_left_delete_word (edit
);
3466 case CK_Delete_Word_Right
:
3467 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3468 edit_insert_over (edit
);
3470 edit_right_delete_word (edit
);
3472 case CK_Delete_Line
:
3473 edit_delete_line (edit
);
3475 case CK_Delete_To_Line_End
:
3476 edit_delete_to_line_end (edit
);
3478 case CK_Delete_To_Line_Begin
:
3479 edit_delete_to_line_begin (edit
);
3483 if (option_auto_para_formatting
)
3485 edit_double_newline (edit
);
3486 if (option_return_does_auto_indent
)
3487 edit_auto_indent (edit
);
3488 format_paragraph (edit
, 0);
3492 edit_insert (edit
, '\n');
3493 if (option_return_does_auto_indent
)
3495 edit_auto_indent (edit
);
3500 edit_insert (edit
, '\n');
3503 case CK_Page_Up_Alt_Highlight
:
3504 edit
->column_highlight
= 1;
3506 case CK_Page_Up_Highlight
:
3507 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3509 case CK_Page_Down_Alt_Highlight
:
3510 edit
->column_highlight
= 1;
3512 case CK_Page_Down_Highlight
:
3513 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3515 case CK_Left_Alt_Highlight
:
3516 edit
->column_highlight
= 1;
3518 case CK_Left_Highlight
:
3519 if (option_fake_half_tabs
)
3521 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3523 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3526 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3527 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3531 edit_left_char_move_cmd (edit
);
3533 case CK_Right_Alt_Highlight
:
3534 edit
->column_highlight
= 1;
3536 case CK_Right_Highlight
:
3537 if (option_fake_half_tabs
)
3539 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3541 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3542 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3546 edit_right_char_move_cmd (edit
);
3549 case CK_Begin_Page_Highlight
:
3550 edit_begin_page (edit
);
3553 case CK_End_Page_Highlight
:
3554 edit_end_page (edit
);
3557 case CK_Word_Left_Highlight
:
3559 edit_left_word_move_cmd (edit
);
3562 case CK_Word_Right_Highlight
:
3564 edit_right_word_move_cmd (edit
);
3566 case CK_Up_Alt_Highlight
:
3567 edit
->column_highlight
= 1;
3569 case CK_Up_Highlight
:
3570 edit_move_up (edit
, 1, 0);
3572 case CK_Down_Alt_Highlight
:
3573 edit
->column_highlight
= 1;
3575 case CK_Down_Highlight
:
3576 edit_move_down (edit
, 1, 0);
3578 case CK_Paragraph_Up_Alt_Highlight
:
3579 edit
->column_highlight
= 1;
3580 case CK_Paragraph_Up
:
3581 case CK_Paragraph_Up_Highlight
:
3582 edit_move_up_paragraph (edit
, 0);
3584 case CK_Paragraph_Down_Alt_Highlight
:
3585 edit
->column_highlight
= 1;
3586 case CK_Paragraph_Down
:
3587 case CK_Paragraph_Down_Highlight
:
3588 edit_move_down_paragraph (edit
, 0);
3590 case CK_Scroll_Up_Alt_Highlight
:
3591 edit
->column_highlight
= 1;
3593 case CK_Scroll_Up_Highlight
:
3594 edit_move_up (edit
, 1, 1);
3596 case CK_Scroll_Down_Alt_Highlight
:
3597 edit
->column_highlight
= 1;
3598 case CK_Scroll_Down
:
3599 case CK_Scroll_Down_Highlight
:
3600 edit_move_down (edit
, 1, 1);
3603 case CK_Home_Highlight
:
3604 edit_cursor_to_bol (edit
);
3607 case CK_End_Highlight
:
3608 edit_cursor_to_eol (edit
);
3611 /* if text marked shift block */
3612 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3614 if (edit
->mark2
< 0)
3615 edit_mark_cmd (edit
, 0);
3616 edit_move_block_to_right (edit
);
3620 if (option_cursor_beyond_eol
)
3621 edit_insert_over (edit
);
3622 edit_tab_cmd (edit
);
3623 if (option_auto_para_formatting
)
3625 format_paragraph (edit
, 0);
3626 edit
->force
|= REDRAW_PAGE
;
3630 check_and_wrap_line (edit
);
3635 case CK_Toggle_Insert
:
3636 edit
->overwrite
= (edit
->overwrite
== 0);
3640 if (edit
->mark2
>= 0)
3642 if (edit
->column_highlight
)
3643 edit_push_action (edit
, COLUMN_ON
);
3644 edit
->column_highlight
= 0;
3646 edit_mark_cmd (edit
, 0);
3648 case CK_Column_Mark
:
3649 if (!edit
->column_highlight
)
3650 edit_push_action (edit
, COLUMN_OFF
);
3651 edit
->column_highlight
= 1;
3652 edit_mark_cmd (edit
, 0);
3655 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3656 edit
->force
|= REDRAW_PAGE
;
3659 if (edit
->column_highlight
)
3660 edit_push_action (edit
, COLUMN_ON
);
3661 edit
->column_highlight
= 0;
3662 edit_mark_cmd (edit
, 1);
3665 case CK_Toggle_Line_State
:
3666 option_line_state
= !option_line_state
;
3667 if (option_line_state
)
3669 option_line_state_width
= LINE_STATE_WIDTH
;
3673 option_line_state_width
= 0;
3675 edit
->force
|= REDRAW_PAGE
;
3678 case CK_Toggle_Show_Margin
:
3679 show_right_margin
= !show_right_margin
;
3680 edit
->force
|= REDRAW_PAGE
;
3683 case CK_Toggle_Bookmark
:
3684 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3685 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3686 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3688 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3690 case CK_Flush_Bookmarks
:
3691 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3692 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3693 edit
->force
|= REDRAW_PAGE
;
3695 case CK_Next_Bookmark
:
3696 if (edit
->book_mark
)
3698 struct _book_mark
*p
;
3699 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3703 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3704 || p
->line
< edit
->start_line
)
3705 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3706 edit_move_to_line (edit
, p
->line
);
3710 case CK_Prev_Bookmark
:
3711 if (edit
->book_mark
)
3713 struct _book_mark
*p
;
3714 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3715 while (p
->line
== edit
->curs_line
)
3720 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3721 || p
->line
< edit
->start_line
)
3722 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3723 edit_move_to_line (edit
, p
->line
);
3728 case CK_Beginning_Of_Text
:
3729 case CK_Beginning_Of_Text_Highlight
:
3730 edit_move_to_top (edit
);
3732 case CK_End_Of_Text
:
3733 case CK_End_Of_Text_Highlight
:
3734 edit_move_to_bottom (edit
);
3738 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3739 edit_insert_over (edit
);
3740 edit_block_copy_cmd (edit
);
3743 edit_block_delete_cmd (edit
);
3746 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3747 edit_insert_over (edit
);
3748 edit_block_move_cmd (edit
);
3751 case CK_Shift_Block_Left
:
3752 if (edit
->mark1
!= edit
->mark2
)
3753 edit_move_block_to_left (edit
);
3755 case CK_Shift_Block_Right
:
3756 if (edit
->mark1
!= edit
->mark2
)
3757 edit_move_block_to_right (edit
);
3760 edit_copy_to_X_buf_cmd (edit
);
3763 edit_cut_to_X_buf_cmd (edit
);
3766 /* if non persistent selection and text selected */
3767 if (!option_persistent_selections
)
3769 if (edit
->mark1
!= edit
->mark2
)
3770 edit_block_delete_cmd (edit
);
3772 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3773 edit_insert_over (edit
);
3774 edit_paste_from_X_buf_cmd (edit
);
3776 case CK_Selection_History
:
3777 edit_paste_from_history (edit
);
3781 edit_save_as_cmd (edit
);
3784 edit_save_confirm_cmd (edit
);
3787 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3790 edit_save_block_cmd (edit
);
3792 case CK_Insert_File
:
3793 edit_insert_file_cmd (edit
);
3796 case CK_Load_Prev_File
:
3797 edit_load_back_cmd (edit
);
3799 case CK_Load_Next_File
:
3800 edit_load_forward_cmd (edit
);
3803 case CK_Load_Syntax_File
:
3804 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3806 case CK_Choose_Syntax
:
3807 edit_syntax_dialog (edit
);
3810 case CK_Load_Menu_File
:
3811 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3814 case CK_Toggle_Syntax
:
3815 option_syntax_highlighting
^= 1;
3816 if (option_syntax_highlighting
== 1)
3817 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3818 edit
->force
|= REDRAW_PAGE
;
3821 case CK_Toggle_Tab_TWS
:
3822 enable_show_tabs_tws
^= 1;
3823 edit
->force
|= REDRAW_PAGE
;
3827 edit_search_cmd (edit
, FALSE
);
3830 edit_search_cmd (edit
, TRUE
);
3833 edit_replace_cmd (edit
, 0);
3835 case CK_Replace_Again
:
3836 edit_replace_cmd (edit
, 1);
3838 case CK_Complete_Word
:
3839 /* if text marked shift block */
3840 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3842 edit_move_block_to_left (edit
);
3846 edit_complete_word_cmd (edit
);
3849 case CK_Find_Definition
:
3850 edit_get_match_keyword_cmd (edit
);
3853 dlg_stop (edit
->widget
.owner
);
3856 edit_new_cmd (edit
);
3859 edit_help_cmd (edit
);
3862 edit_refresh_cmd (edit
);
3864 case CK_SaveSetupCmd
:
3873 case CK_Edit_Options
:
3874 edit_options_dialog (edit
);
3876 case CK_Edit_Save_Mode
:
3877 menu_save_mode_cmd ();
3882 /* fool gcc to prevent a Y2K warning */
3883 char time_format
[] = "_c";
3884 time_format
[0] = '%';
3886 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3887 edit_print_string (edit
, s
);
3888 edit
->force
|= REDRAW_PAGE
;
3893 edit_goto_cmd (edit
);
3895 case CK_Paragraph_Format
:
3896 format_paragraph (edit
, 1);
3897 edit
->force
|= REDRAW_PAGE
;
3899 case CK_Delete_Macro
:
3900 edit_delete_macro_cmd (edit
);
3902 case CK_Match_Bracket
:
3903 edit_goto_matching_bracket (edit
);
3909 edit_sort_cmd (edit
);
3912 edit_ext_cmd (edit
);
3915 edit_mail_dialog (edit
);
3920 case CK_SelectCodepage
:
3921 edit_select_codepage_cmd (edit
);
3923 case CK_Insert_Literal
:
3924 edit_insert_literal_cmd (edit
);
3926 case CK_Execute_Macro
:
3927 edit_execute_macro_cmd (edit
);
3929 case CK_Begin_End_Macro
:
3930 edit_begin_end_macro_cmd (edit
);
3940 if ((command
/ 1000) == 1) /* a shell command */
3941 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
3942 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
3943 { /* a macro command */
3944 struct macro m
[MAX_MACRO_LENGTH
];
3946 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
3947 edit_execute_macro (edit
, m
, nm
);
3950 /* keys which must set the col position, and the search vars */
3956 case CK_Replace_Again
:
3957 case CK_Complete_Word
:
3958 edit
->prev_col
= edit_get_col (edit
);
3961 case CK_Up_Highlight
:
3962 case CK_Up_Alt_Highlight
:
3964 case CK_Down_Highlight
:
3965 case CK_Down_Alt_Highlight
:
3967 case CK_Page_Up_Highlight
:
3968 case CK_Page_Up_Alt_Highlight
:
3970 case CK_Page_Down_Highlight
:
3971 case CK_Page_Down_Alt_Highlight
:
3972 case CK_Beginning_Of_Text
:
3973 case CK_Beginning_Of_Text_Highlight
:
3974 case CK_End_Of_Text
:
3975 case CK_End_Of_Text_Highlight
:
3976 case CK_Paragraph_Up
:
3977 case CK_Paragraph_Up_Highlight
:
3978 case CK_Paragraph_Up_Alt_Highlight
:
3979 case CK_Paragraph_Down
:
3980 case CK_Paragraph_Down_Highlight
:
3981 case CK_Paragraph_Down_Alt_Highlight
:
3983 case CK_Scroll_Up_Highlight
:
3984 case CK_Scroll_Up_Alt_Highlight
:
3985 case CK_Scroll_Down
:
3986 case CK_Scroll_Down_Highlight
:
3987 case CK_Scroll_Down_Alt_Highlight
:
3988 edit
->search_start
= edit
->curs1
;
3989 edit
->found_len
= 0;
3992 edit
->found_len
= 0;
3993 edit
->prev_col
= edit_get_col (edit
);
3994 edit
->search_start
= edit
->curs1
;
3996 edit_find_bracket (edit
);
3998 if (option_auto_para_formatting
)
4004 case CK_Delete_Word_Left
:
4005 case CK_Delete_Word_Right
:
4006 case CK_Delete_To_Line_End
:
4007 case CK_Delete_To_Line_Begin
:
4008 format_paragraph (edit
, 0);
4009 edit
->force
|= REDRAW_PAGE
;
4014 /* --------------------------------------------------------------------------------------------- */
4017 edit_stack_init (void)
4019 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4021 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4022 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4025 edit_stack_iterator
= 0;
4028 /* --------------------------------------------------------------------------------------------- */
4031 edit_stack_free (void)
4033 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4034 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4037 /* --------------------------------------------------------------------------------------------- */
4041 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4043 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4046 /* --------------------------------------------------------------------------------------------- */
4050 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4052 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4055 /* --------------------------------------------------------------------------------------------- */
4058 edit_unlock_file (WEdit
* edit
)
4063 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4064 ret
= unlock_file (fullpath
);
4070 /* --------------------------------------------------------------------------------------------- */
4073 edit_lock_file (WEdit
* edit
)
4078 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4079 ret
= lock_file (fullpath
);
4085 /* --------------------------------------------------------------------------------------------- */