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 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
137 /*** file scope functions ************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
142 * here's a quick sketch of the layout: (don't run this through indent.)
144 * (b1 is buffers1 and b2 is buffers2)
147 * \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
148 * ______________________________________|______________________________________
150 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
151 * |-> |-> |-> |-> |-> |-> |
153 * _<------------------------->|<----------------->_
154 * WEdit->curs2 | WEdit->curs1
159 * file end|||file beginning
168 /* --------------------------------------------------------------------------------------------- */
170 static void user_menu (WEdit
* edit
);
171 static int left_of_four_spaces (WEdit
* edit
);
173 /* --------------------------------------------------------------------------------------------- */
178 const char *header
= N_("About");
179 const char *button_name
= N_("&OK");
180 const char *const version
= "MCEdit " VERSION
;
181 char text
[BUF_LARGE
];
183 int win_len
, version_len
, button_len
;
190 button_name
= _(button_name
);
193 button_len
= str_term_width1 (button_name
) + 5;
194 version_len
= str_term_width1 (version
);
196 g_snprintf (text
, sizeof (text
),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len
= str_term_width1 (header
);
202 win_len
= max (win_len
, version_len
);
203 win_len
= max (win_len
, button_len
);
205 /* count width and height of text */
206 str_msg_term_size (text
, &lines
, &cols
);
208 cols
= max (win_len
, cols
) + 6;
211 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
212 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
214 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
215 add_widget (about_dlg
, label_new (5, 3, text
));
216 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
217 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
220 destroy_dlg (about_dlg
);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
229 edit_init_buffers (WEdit
* edit
)
233 for (j
= 0; j
<= MAXBUFF
; j
++)
235 edit
->buffers1
[j
] = NULL
;
236 edit
->buffers2
[j
] = NULL
;
241 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
251 edit_load_file_fast (WEdit
* edit
, const char *filename
)
257 edit
->curs2
= edit
->last_byte
;
258 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
260 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
265 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
266 edit_error_dialog (_("Error"), errmsg
);
271 if (!edit
->buffers2
[buf2
])
272 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
277 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
278 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
281 for (buf
= buf2
- 1; buf
>= 0; buf
--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit
->buffers2
[buf
])
285 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
286 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
294 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
295 edit_error_dialog (_("Error"), err_str
);
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
306 edit_find_filter (const char *filename
)
310 if (filename
== NULL
)
313 l
= strlen (filename
);
314 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
316 e
= strlen (all_filters
[i
].extension
);
318 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
324 /* --------------------------------------------------------------------------------------------- */
327 edit_get_filter (const char *filename
)
330 char *p
, *quoted_name
;
332 i
= edit_find_filter (filename
);
336 quoted_name
= name_quote (filename
, 0);
337 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
338 g_free (quoted_name
);
342 /* --------------------------------------------------------------------------------------------- */
345 edit_insert_stream (WEdit
* edit
, FILE * f
)
349 while ((c
= fgetc (f
)) >= 0)
351 edit_insert (edit
, c
);
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
361 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
364 gchar
*errmsg
= NULL
;
366 /* Try opening an existing file */
367 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
378 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
383 /* New file, delete it if it's not modified or saved */
384 edit
->delete_file
= 1;
388 /* Check what we have opened */
389 if (mc_fstat (file
, st
) < 0)
391 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
395 /* We want to open regular files only */
396 if (!S_ISREG (st
->st_mode
))
398 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
407 edit
->delete_file
= 0;
409 if (st
->st_size
>= SIZE_LIMIT
)
410 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
413 (void) mc_close (file
);
417 edit_error_dialog (_("Error"), errmsg
);
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
436 edit_load_file (WEdit
* edit
)
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit
->filename
) >= 0)
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (edit
->filename
))
452 * FIXME: line end translation should disable fast loading as well
453 * Consider doing fseek() to the end and ftell() for the real size.
458 /* If we are dealing with a real file, check that it exists */
459 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
464 /* nothing to load */
468 edit_init_buffers (edit
);
472 edit
->last_byte
= edit
->stat1
.st_size
;
473 edit_load_file_fast (edit
, edit
->filename
);
474 /* If fast load was used, the number of lines wasn't calculated */
475 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
482 edit
->undo_stack_disable
= 1;
483 if (edit_insert_file (edit
, edit
->filename
) == 0)
488 edit
->undo_stack_disable
= 0;
495 /* --------------------------------------------------------------------------------------------- */
496 /** Restore saved cursor position in the file */
499 edit_load_position (WEdit
* edit
)
505 if (!edit
->filename
|| !*edit
->filename
)
508 filename
= vfs_canon (edit
->filename
);
509 load_file_position (filename
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
514 edit_move_to_line (edit
, line
- 1);
515 edit
->prev_col
= column
;
519 edit_cursor_move (edit
, offset
);
520 line
= edit
->curs_line
;
521 edit
->search_start
= edit
->curs1
;
524 book_mark_restore (edit
, BOOK_MARK_COLOR
);
526 edit_move_to_prev_col (edit
, edit_bol (edit
, edit
->curs1
));
527 edit_move_display (edit
, line
- (edit
->num_widget_lines
/ 2));
530 /* --------------------------------------------------------------------------------------------- */
531 /** Save cursor position in the file */
534 edit_save_position (WEdit
* edit
)
538 if (edit
->filename
== NULL
|| *edit
->filename
== '\0')
541 filename
= vfs_canon (edit
->filename
);
543 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
544 save_file_position (filename
, edit
->curs_line
+ 1, edit
->curs_col
, edit
->curs1
,
545 edit
->serialized_bookmarks
);
546 edit
->serialized_bookmarks
= NULL
;
551 /* --------------------------------------------------------------------------------------------- */
552 /** Clean the WEdit stricture except the widget part */
555 edit_purge_widget (WEdit
* edit
)
557 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
558 char *start
= (char *) edit
+ sizeof (Widget
);
559 memset (start
, 0, len
);
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 edit_pop_undo_action (WEdit
* edit
)
586 unsigned long sp
= edit
->undo_stack_pointer
;
588 if (sp
== edit
->undo_stack_bottom
)
591 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
592 c
= edit
->undo_stack
[sp
];
595 /* edit->undo_stack[sp] = '@'; */
596 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
600 if (sp
== edit
->undo_stack_bottom
)
603 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
604 if (edit
->undo_stack
[sp
] == -2)
606 /* edit->undo_stack[sp] = '@'; */
607 edit
->undo_stack_pointer
= sp
;
610 edit
->undo_stack
[sp
]++;
616 edit_pop_redo_action (WEdit
* edit
)
619 unsigned long sp
= edit
->redo_stack_pointer
;
621 if (sp
== edit
->redo_stack_bottom
)
624 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
625 c
= edit
->redo_stack
[sp
];
628 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
632 if (sp
== edit
->redo_stack_bottom
)
635 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
636 if (edit
->redo_stack
[sp
] == -2)
637 edit
->redo_stack_pointer
= sp
;
639 edit
->redo_stack
[sp
]++;
645 get_prev_undo_action (WEdit
* edit
)
648 unsigned long sp
= edit
->undo_stack_pointer
;
650 if (sp
== edit
->undo_stack_bottom
)
653 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
654 c
= edit
->undo_stack
[sp
];
658 if (sp
== edit
->undo_stack_bottom
)
661 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
665 /* --------------------------------------------------------------------------------------------- */
666 /** is called whenever a modification is made by one of the four routines below */
669 edit_modification (WEdit
* edit
)
671 edit
->caches_valid
= 0;
672 edit
->screen_modified
= 1;
674 /* raise lock when file modified */
675 if (!edit
->modified
&& !edit
->delete_file
)
676 edit
->locked
= edit_lock_file (edit
);
680 /* --------------------------------------------------------------------------------------------- */
683 edit_insert_over (WEdit
* edit
)
687 for (i
= 0; i
< edit
->over_col
; i
++)
689 edit_insert (edit
, ' ');
694 /* --------------------------------------------------------------------------------------------- */
697 edit_backspace (WEdit
* edit
, const int byte_delete
)
708 if (edit
->mark2
!= edit
->mark1
)
709 edit_push_markers (edit
);
711 if (edit
->utf8
&& byte_delete
== 0)
713 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
717 for (i
= 1; i
<= cw
; i
++)
719 if (edit
->mark1
>= edit
->curs1
)
722 edit
->end_mark_curs
--;
724 if (edit
->mark2
>= edit
->curs1
)
726 if (edit
->last_get_rule
>= edit
->curs1
)
727 edit
->last_get_rule
--;
729 p
= *(edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
] +
730 ((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
));
731 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
733 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
734 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
738 edit_push_undo_action (edit
, p
);
740 edit_modification (edit
);
744 book_mark_dec (edit
, edit
->curs_line
);
747 edit
->force
|= REDRAW_AFTER_CURSOR
;
750 if (edit
->curs1
< edit
->start_display
)
752 edit
->start_display
--;
760 /* --------------------------------------------------------------------------------------------- */
761 /* high level cursor movement commands */
762 /* --------------------------------------------------------------------------------------------- */
765 is_in_indent (WEdit
* edit
)
767 long p
= edit_bol (edit
, edit
->curs1
);
768 while (p
< edit
->curs1
)
769 if (!strchr (" \t", edit_get_byte (edit
, p
++)))
774 /* --------------------------------------------------------------------------------------------- */
777 is_blank (WEdit
* edit
, long offset
)
781 s
= edit_bol (edit
, offset
);
782 f
= edit_eol (edit
, offset
) - 1;
785 c
= edit_get_byte (edit
, s
++);
793 /* --------------------------------------------------------------------------------------------- */
794 /** returns the offset of line i */
797 edit_find_line (WEdit
* edit
, int line
)
801 if (!edit
->caches_valid
)
803 for (i
= 0; i
< N_LINE_CACHES
; i
++)
804 edit
->line_numbers
[i
] = edit
->line_offsets
[i
] = 0;
805 /* three offsets that we *know* are line 0 at 0 and these two: */
806 edit
->line_numbers
[1] = edit
->curs_line
;
807 edit
->line_offsets
[1] = edit_bol (edit
, edit
->curs1
);
808 edit
->line_numbers
[2] = edit
->total_lines
;
809 edit
->line_offsets
[2] = edit_bol (edit
, edit
->last_byte
);
810 edit
->caches_valid
= 1;
812 if (line
>= edit
->total_lines
)
813 return edit
->line_offsets
[2];
816 /* find the closest known point */
817 for (i
= 0; i
< N_LINE_CACHES
; i
++)
820 n
= abs (edit
->line_numbers
[i
] - line
);
828 return edit
->line_offsets
[j
]; /* know the offset exactly */
829 if (m
== 1 && j
>= 3)
830 i
= j
; /* one line different - caller might be looping, so stay in this cache */
832 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
833 if (line
> edit
->line_numbers
[j
])
834 edit
->line_offsets
[i
] =
835 edit_move_forward (edit
, edit
->line_offsets
[j
], line
- edit
->line_numbers
[j
], 0);
837 edit
->line_offsets
[i
] =
838 edit_move_backward (edit
, edit
->line_offsets
[j
], edit
->line_numbers
[j
] - line
);
839 edit
->line_numbers
[i
] = line
;
840 return edit
->line_offsets
[i
];
843 /* --------------------------------------------------------------------------------------------- */
844 /** moves up until a blank line is reached, or until just
845 before a non-blank line is reached */
848 edit_move_up_paragraph (WEdit
* edit
, int do_scroll
)
851 if (edit
->curs_line
> 1)
853 if (line_is_blank (edit
, edit
->curs_line
))
855 if (line_is_blank (edit
, edit
->curs_line
- 1))
857 for (i
= edit
->curs_line
- 1; i
; i
--)
858 if (!line_is_blank (edit
, i
))
866 for (i
= edit
->curs_line
- 1; i
; i
--)
867 if (line_is_blank (edit
, i
))
873 for (i
= edit
->curs_line
- 1; i
; i
--)
874 if (line_is_blank (edit
, i
))
878 edit_move_up (edit
, edit
->curs_line
- i
, do_scroll
);
881 /* --------------------------------------------------------------------------------------------- */
882 /** moves down until a blank line is reached, or until just
883 before a non-blank line is reached */
886 edit_move_down_paragraph (WEdit
* edit
, int do_scroll
)
889 if (edit
->curs_line
>= edit
->total_lines
- 1)
891 i
= edit
->total_lines
;
895 if (line_is_blank (edit
, edit
->curs_line
))
897 if (line_is_blank (edit
, edit
->curs_line
+ 1))
899 for (i
= edit
->curs_line
+ 1; i
; i
++)
900 if (!line_is_blank (edit
, i
) || i
> edit
->total_lines
)
908 for (i
= edit
->curs_line
+ 1; i
; i
++)
909 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
915 for (i
= edit
->curs_line
+ 1; i
; i
++)
916 if (line_is_blank (edit
, i
) || i
>= edit
->total_lines
)
920 edit_move_down (edit
, i
- edit
->curs_line
, do_scroll
);
923 /* --------------------------------------------------------------------------------------------- */
926 edit_begin_page (WEdit
* edit
)
928 edit_update_curs_row (edit
);
929 edit_move_up (edit
, edit
->curs_row
, 0);
932 /* --------------------------------------------------------------------------------------------- */
935 edit_end_page (WEdit
* edit
)
937 edit_update_curs_row (edit
);
938 edit_move_down (edit
, edit
->num_widget_lines
- edit
->curs_row
- 1, 0);
942 /* --------------------------------------------------------------------------------------------- */
943 /** goto beginning of text */
946 edit_move_to_top (WEdit
* edit
)
950 edit_cursor_move (edit
, -edit
->curs1
);
951 edit_move_to_prev_col (edit
, 0);
952 edit
->force
|= REDRAW_PAGE
;
953 edit
->search_start
= 0;
954 edit_update_curs_row (edit
);
959 /* --------------------------------------------------------------------------------------------- */
960 /** goto end of text */
963 edit_move_to_bottom (WEdit
* edit
)
965 if (edit
->curs_line
< edit
->total_lines
)
967 edit_move_down (edit
, edit
->total_lines
- edit
->curs_row
, 0);
968 edit
->start_display
= edit
->last_byte
;
969 edit
->start_line
= edit
->total_lines
;
970 edit_scroll_upward (edit
, edit
->num_widget_lines
- 1);
971 edit
->force
|= REDRAW_PAGE
;
975 /* --------------------------------------------------------------------------------------------- */
976 /** goto beginning of line */
979 edit_cursor_to_bol (WEdit
* edit
)
981 edit_cursor_move (edit
, edit_bol (edit
, edit
->curs1
) - edit
->curs1
);
982 edit
->search_start
= edit
->curs1
;
983 edit
->prev_col
= edit_get_col (edit
);
987 /* --------------------------------------------------------------------------------------------- */
988 /** goto end of line */
991 edit_cursor_to_eol (WEdit
* edit
)
993 edit_cursor_move (edit
, edit_eol (edit
, edit
->curs1
) - edit
->curs1
);
994 edit
->search_start
= edit
->curs1
;
995 edit
->prev_col
= edit_get_col (edit
);
999 /* --------------------------------------------------------------------------------------------- */
1001 static unsigned long
1006 const char option_chars_move_whole_word
[] =
1007 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
1013 if (*option_chars_move_whole_word
== '!')
1015 return 0x80000000UL
;
1017 if (g_ascii_isupper ((gchar
) c
))
1019 else if (g_ascii_islower ((gchar
) c
))
1021 else if (g_ascii_isalpha (c
))
1023 else if (isdigit (c
))
1025 else if (isspace (c
))
1027 q
= strchr (option_chars_move_whole_word
, c
);
1029 return 0xFFFFFFFFUL
;
1032 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
1037 while ((q
= strchr (q
+ 1, c
)));
1041 /* --------------------------------------------------------------------------------------------- */
1044 edit_left_word_move (WEdit
* edit
, int s
)
1049 if (edit
->column_highlight
1050 && edit
->mark1
!= edit
->mark2
1051 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1053 edit_cursor_move (edit
, -1);
1056 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1057 c2
= edit_get_byte (edit
, edit
->curs1
);
1058 if (c1
== '\n' || c2
== '\n')
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_left_word_move_cmd (WEdit
* edit
)
1075 edit_left_word_move (edit
, 0);
1076 edit
->force
|= REDRAW_PAGE
;
1079 /* --------------------------------------------------------------------------------------------- */
1082 edit_right_word_move (WEdit
* edit
, int s
)
1087 if (edit
->column_highlight
1088 && edit
->mark1
!= edit
->mark2
1089 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1091 edit_cursor_move (edit
, 1);
1092 if (edit
->curs1
>= edit
->last_byte
)
1094 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1095 c2
= edit_get_byte (edit
, edit
->curs1
);
1096 if (c1
== '\n' || c2
== '\n')
1098 if (!(my_type_of (c1
) & my_type_of (c2
)))
1100 if (isspace (c1
) && !isspace (c2
))
1103 if (!isspace (c1
) && isspace (c2
))
1108 /* --------------------------------------------------------------------------------------------- */
1111 edit_right_word_move_cmd (WEdit
* edit
)
1113 edit_right_word_move (edit
, 0);
1114 edit
->force
|= REDRAW_PAGE
;
1117 /* --------------------------------------------------------------------------------------------- */
1120 edit_right_char_move_cmd (WEdit
* edit
)
1126 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1132 c
= edit_get_byte (edit
, edit
->curs1
);
1134 if (option_cursor_beyond_eol
&& c
== '\n')
1140 edit_cursor_move (edit
, cw
);
1144 /* --------------------------------------------------------------------------------------------- */
1147 edit_left_char_move_cmd (WEdit
* edit
)
1150 if (edit
->column_highlight
1151 && option_cursor_beyond_eol
1152 && edit
->mark1
!= edit
->mark2
1153 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1157 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1161 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1167 edit_cursor_move (edit
, -cw
);
1171 /* --------------------------------------------------------------------------------------------- */
1172 /** Up or down cursor moving.
1173 direction = TRUE - move up
1178 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1181 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1190 edit
->force
|= REDRAW_PAGE
;
1194 edit_scroll_upward (edit
, i
);
1196 edit_scroll_downward (edit
, i
);
1198 p
= edit_bol (edit
, edit
->curs1
);
1200 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1202 edit_cursor_move (edit
, p
- edit
->curs1
);
1204 edit_move_to_prev_col (edit
, p
);
1206 /* search start of current multibyte char (like CJK) */
1207 if (edit
->curs1
+ 1 < edit
->last_byte
)
1209 edit_right_char_move_cmd (edit
);
1210 edit_left_char_move_cmd (edit
);
1213 edit
->search_start
= edit
->curs1
;
1214 edit
->found_len
= 0;
1217 /* --------------------------------------------------------------------------------------------- */
1220 edit_right_delete_word (WEdit
* edit
)
1225 if (edit
->curs1
>= edit
->last_byte
)
1227 c1
= edit_delete (edit
, 1);
1228 c2
= edit_get_byte (edit
, edit
->curs1
);
1229 if (c1
== '\n' || c2
== '\n')
1231 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1233 if (!(my_type_of (c1
) & my_type_of (c2
)))
1238 /* --------------------------------------------------------------------------------------------- */
1241 edit_left_delete_word (WEdit
* edit
)
1246 if (edit
->curs1
<= 0)
1248 c1
= edit_backspace (edit
, 1);
1249 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1250 if (c1
== '\n' || c2
== '\n')
1252 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1254 if (!(my_type_of (c1
) & my_type_of (c2
)))
1259 /* --------------------------------------------------------------------------------------------- */
1261 the start column position is not recorded, and hence does not
1262 undo as it happed. But who would notice.
1266 edit_do_undo (WEdit
* edit
)
1271 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1273 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1280 edit_cursor_move (edit
, 1);
1283 edit_cursor_move (edit
, -1);
1287 edit_backspace (edit
, 1);
1291 edit_delete (edit
, 1);
1294 edit
->column_highlight
= 1;
1297 edit
->column_highlight
= 0;
1300 if (ac
>= 256 && ac
< 512)
1301 edit_insert_ahead (edit
, ac
- 256);
1302 if (ac
>= 0 && ac
< 256)
1303 edit_insert (edit
, ac
);
1305 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1307 edit
->mark1
= ac
- MARK_1
;
1308 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1310 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1312 edit
->mark2
= ac
- MARK_2
;
1313 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1315 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1317 edit
->end_mark_curs
= ac
- MARK_CURS
;
1320 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1323 if (edit
->start_display
> ac
- KEY_PRESS
)
1325 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1326 edit
->force
|= REDRAW_PAGE
;
1328 else if (edit
->start_display
< ac
- KEY_PRESS
)
1330 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1331 edit
->force
|= REDRAW_PAGE
;
1333 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1334 edit_update_curs_row (edit
);
1337 edit
->undo_stack_disable
= 0;
1341 edit_do_redo (WEdit
* edit
)
1346 if (edit
->redo_stack_reset
)
1350 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1357 edit_cursor_move (edit
, 1);
1360 edit_cursor_move (edit
, -1);
1363 edit_backspace (edit
, 1);
1366 edit_delete (edit
, 1);
1369 edit
->column_highlight
= 1;
1372 edit
->column_highlight
= 0;
1375 if (ac
>= 256 && ac
< 512)
1376 edit_insert_ahead (edit
, ac
- 256);
1377 if (ac
>= 0 && ac
< 256)
1378 edit_insert (edit
, ac
);
1380 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1382 edit
->mark1
= ac
- MARK_1
;
1383 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1385 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1387 edit
->mark2
= ac
- MARK_2
;
1388 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1390 /* more than one pop usually means something big */
1392 edit
->force
|= REDRAW_PAGE
;
1395 if (edit
->start_display
> ac
- KEY_PRESS
)
1397 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1398 edit
->force
|= REDRAW_PAGE
;
1400 else if (edit
->start_display
< ac
- KEY_PRESS
)
1402 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1403 edit
->force
|= REDRAW_PAGE
;
1405 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1406 edit_update_curs_row (edit
);
1412 edit_group_undo (WEdit
* edit
)
1414 long ac
= KEY_PRESS
;
1415 long cur_ac
= KEY_PRESS
;
1416 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1418 cur_ac
= get_prev_undo_action (edit
);
1419 edit_do_undo (edit
);
1420 ac
= get_prev_undo_action (edit
);
1421 /* exit from cycle if option_group_undo is not set,
1422 * and make single UNDO operation
1424 if (!option_group_undo
)
1429 /* --------------------------------------------------------------------------------------------- */
1432 edit_delete_to_line_end (WEdit
* edit
)
1434 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1438 edit_delete (edit
, 1);
1442 /* --------------------------------------------------------------------------------------------- */
1445 edit_delete_to_line_begin (WEdit
* edit
)
1447 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1451 edit_backspace (edit
, 1);
1455 /* --------------------------------------------------------------------------------------------- */
1458 is_aligned_on_a_tab (WEdit
* edit
)
1460 edit_update_curs_col (edit
);
1461 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1462 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1465 /* --------------------------------------------------------------------------------------------- */
1468 right_of_four_spaces (WEdit
* edit
)
1471 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1472 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1474 return is_aligned_on_a_tab (edit
);
1478 /* --------------------------------------------------------------------------------------------- */
1481 left_of_four_spaces (WEdit
* edit
)
1484 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1485 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1487 return is_aligned_on_a_tab (edit
);
1491 /* --------------------------------------------------------------------------------------------- */
1494 edit_auto_indent (WEdit
* edit
)
1499 /* use the previous line as a template */
1500 p
= edit_move_backward (edit
, p
, 1);
1501 /* copy the leading whitespace of the line */
1503 { /* no range check - the line _is_ \n-terminated */
1504 c
= edit_get_byte (edit
, p
++);
1505 if (c
!= ' ' && c
!= '\t')
1507 edit_insert (edit
, c
);
1511 /* --------------------------------------------------------------------------------------------- */
1514 edit_double_newline (WEdit
* edit
)
1516 edit_insert (edit
, '\n');
1517 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1519 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1521 edit
->force
|= REDRAW_PAGE
;
1522 edit_insert (edit
, '\n');
1525 /* --------------------------------------------------------------------------------------------- */
1528 edit_tab_cmd (WEdit
* edit
)
1532 if (option_fake_half_tabs
)
1534 if (is_in_indent (edit
))
1536 /*insert a half tab (usually four spaces) unless there is a
1537 half tab already behind, then delete it and insert a
1539 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1541 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1542 edit_backspace (edit
, 1);
1543 edit_insert (edit
, '\t');
1547 insert_spaces_tab (edit
, 1);
1552 if (option_fill_tabs_with_spaces
)
1554 insert_spaces_tab (edit
, 0);
1558 edit_insert (edit
, '\t');
1562 /* --------------------------------------------------------------------------------------------- */
1565 check_and_wrap_line (WEdit
* edit
)
1568 if (!option_typewriter_wrap
)
1570 edit_update_curs_col (edit
);
1571 if (edit
->curs_col
< option_word_wrap_line_length
)
1577 c
= edit_get_byte (edit
, curs
);
1578 if (c
== '\n' || curs
<= 0)
1580 edit_insert (edit
, '\n');
1583 if (c
== ' ' || c
== '\t')
1585 int current
= edit
->curs1
;
1586 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1587 edit_insert (edit
, '\n');
1588 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1594 /* --------------------------------------------------------------------------------------------- */
1595 /** this find the matching bracket in either direction, and sets edit->bracket */
1598 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1600 const char *const b
= "{}{[][()(", *p
;
1601 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1602 unsigned long j
= 0;
1604 edit_update_curs_row (edit
);
1605 c
= edit_get_byte (edit
, edit
->curs1
);
1608 if (!furthest_bracket_search
)
1609 furthest_bracket_search
--;
1610 /* not on a bracket at all */
1613 /* the matching bracket */
1615 /* going left or right? */
1616 if (strchr ("{[(", c
))
1618 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1620 /* out of buffer? */
1621 if (q
>= edit
->last_byte
|| q
< 0)
1623 a
= edit_get_byte (edit
, q
);
1624 /* don't want to eat CPU */
1625 if (j
++ > furthest_bracket_search
)
1627 /* out of screen? */
1630 if (q
< edit
->start_display
)
1632 /* count lines if searching downward */
1633 if (inc
> 0 && a
== '\n')
1634 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
1637 /* count bracket depth */
1638 i
+= (a
== c
) - (a
== d
);
1639 /* return if bracket depth is zero */
1647 /* --------------------------------------------------------------------------------------------- */
1650 edit_goto_matching_bracket (WEdit
* edit
)
1654 q
= edit_get_bracket (edit
, 0, 0);
1657 edit
->bracket
= edit
->curs1
;
1658 edit
->force
|= REDRAW_PAGE
;
1659 edit_cursor_move (edit
, q
- edit
->curs1
);
1663 /* --------------------------------------------------------------------------------------------- */
1664 /** User edit menu, like user menu (F2) but only in editor. */
1667 user_menu (WEdit
* edit
)
1671 long start_mark
, end_mark
;
1674 block_file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
1676 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1678 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1680 /* run shell scripts from menu */
1681 if (user_menu_cmd (edit
) && (mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1686 /* i.e. we have marked block */
1688 rc
= edit_block_delete_cmd (edit
);
1694 ins_len
= edit_insert_file (edit
, block_file
);
1695 if (nomark
== 0 && ins_len
> 0)
1696 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1698 /* truncate block file */
1699 fd
= fopen (block_file
, "w");
1703 edit_refresh_cmd (edit
);
1704 edit
->force
|= REDRAW_COMPLETELY
;
1706 g_free (block_file
);
1709 /* --------------------------------------------------------------------------------------------- */
1710 /*** public functions ****************************************************************************/
1711 /* --------------------------------------------------------------------------------------------- */
1714 edit_get_byte (WEdit
* edit
, long byte_index
)
1717 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1720 if (byte_index
>= edit
->curs1
)
1722 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1723 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1727 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1731 /* --------------------------------------------------------------------------------------------- */
1734 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1737 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1740 if (byte_index
>= edit
->curs1
)
1742 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1743 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1744 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1748 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1749 (byte_index
& M_EDIT_BUF_SIZE
));
1753 /* --------------------------------------------------------------------------------------------- */
1756 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1760 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1766 if (byte_index
>= edit
->curs1
)
1768 p
= edit
->curs1
+ edit
->curs2
- 1;
1769 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1770 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1774 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1778 /* --------------------------------------------------------------------------------------------- */
1781 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1786 gchar
*next_ch
= NULL
;
1789 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1795 str
= edit_get_byte_ptr (edit
, byte_index
);
1803 res
= g_utf8_get_char_validated (str
, -1);
1813 /* Calculate UTF-8 char width */
1814 next_ch
= g_utf8_next_char (str
);
1817 width
= next_ch
- str
;
1825 *char_width
= width
;
1829 /* --------------------------------------------------------------------------------------------- */
1832 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1834 gchar
*str
, *buf
= NULL
;
1837 gchar
*next_ch
= NULL
;
1843 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1849 ch
= edit_get_utf (edit
, byte_index
, &width
);
1853 *char_width
= width
;
1857 str
= edit_get_byte_ptr (edit
, byte_index
);
1858 buf
= edit_get_buf_ptr (edit
, byte_index
);
1859 if (str
== NULL
|| buf
== NULL
)
1864 /* get prev utf8 char */
1866 str
= g_utf8_find_prev_char (buf
, str
);
1874 res
= g_utf8_get_char_validated (str
, -1);
1884 /* Calculate UTF-8 char width */
1885 next_ch
= g_utf8_next_char (str
);
1888 width
= next_ch
- str
;
1896 *char_width
= width
;
1900 /* --------------------------------------------------------------------------------------------- */
1903 edit_get_write_filter (const char *write_name
, const char *filename
)
1906 char *p
, *writename
;
1908 i
= edit_find_filter (filename
);
1912 writename
= name_quote (write_name
, 0);
1913 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1918 /* --------------------------------------------------------------------------------------------- */
1921 edit_write_stream (WEdit
* edit
, FILE * f
)
1925 if (edit
->lb
== LB_ASIS
)
1927 for (i
= 0; i
< edit
->last_byte
; i
++)
1928 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1933 /* change line breaks */
1934 for (i
= 0; i
< edit
->last_byte
; i
++)
1936 unsigned char c
= edit_get_byte (edit
, i
);
1938 if (!(c
== '\n' || c
== '\r'))
1940 /* not line break */
1941 if (fputc (c
, f
) < 0)
1945 { /* (c == '\n' || c == '\r') */
1946 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1950 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1951 /* put one line break unconditionally */
1952 if (fputc ('\n', f
) < 0)
1955 i
++; /* 2 chars are processed */
1957 if (c
== '\r' && c1
== '\n')
1958 /* Windows line break; go to the next char */
1961 if (c
== '\r' && c1
== '\r')
1963 /* two Macintosh line breaks; put second line break */
1964 if (fputc ('\n', f
) < 0)
1969 if (fputc (c1
, f
) < 0)
1973 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1974 /* put one line break unconditionally */
1975 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1978 if (c
== '\r' && c1
== '\n')
1979 /* Windows line break; go to the next char */
1983 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1984 /* put one line break unconditionally */
1985 if (fputc ('\r', f
) < 0)
1988 i
++; /* 2 chars are processed */
1990 if (c
== '\r' && c1
== '\n')
1991 /* Windows line break; go to the next char */
1994 if (c
== '\n' && c1
== '\n')
1996 /* two Windows line breaks; put second line break */
1997 if (fputc ('\r', f
) < 0)
2002 if (fputc (c1
, f
) < 0)
2005 case LB_ASIS
: /* default without changes */
2011 return edit
->last_byte
;
2014 /* --------------------------------------------------------------------------------------------- */
2015 /** inserts a file at the cursor, returns count of inserted bytes on success */
2017 edit_insert_file (WEdit
* edit
, const char *filename
)
2022 p
= edit_get_filter (filename
);
2026 long current
= edit
->curs1
;
2027 f
= (FILE *) popen (p
, "r");
2030 edit_insert_stream (edit
, f
);
2031 ins_len
= edit
->curs1
- current
;
2032 edit_cursor_move (edit
, current
- edit
->curs1
);
2036 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2037 edit_error_dialog (_("Error"), errmsg
);
2046 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2047 edit_error_dialog (_("Error"), errmsg
);
2056 int i
, file
, blocklen
;
2057 long current
= edit
->curs1
;
2058 int vertical_insertion
= 0;
2060 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2063 buf
= g_malloc0 (TEMP_BUF_LEN
);
2064 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2067 /* if contain signature VERTICAL_MAGIC then it vertical block */
2068 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2069 vertical_insertion
= 1;
2071 mc_lseek (file
, 0, SEEK_SET
);
2073 if (vertical_insertion
)
2075 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
2079 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2081 for (i
= 0; i
< blocklen
; i
++)
2082 edit_insert (edit
, buf
[i
]);
2085 ins_len
= edit
->curs1
- current
;
2086 edit_cursor_move (edit
, current
- edit
->curs1
);
2095 /* --------------------------------------------------------------------------------------------- */
2097 * Fill in the edit structure. Return NULL on failure. Pass edit as
2098 * NULL to allocate a new structure.
2100 * If line is 0, try to restore saved position. Otherwise put the
2101 * cursor on that line and show it in the middle of the screen.
2105 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
2108 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2109 if (option_line_state
)
2111 option_line_state_width
= LINE_STATE_WIDTH
;
2115 option_line_state_width
= 0;
2121 * Expand option_whole_chars_search by national letters using
2125 static char option_whole_chars_search_buf
[256];
2127 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2130 size_t len
= str_term_width1 (option_whole_chars_search
);
2132 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2134 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2136 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2138 option_whole_chars_search_buf
[len
++] = i
;
2142 option_whole_chars_search_buf
[len
] = 0;
2143 option_whole_chars_search
= option_whole_chars_search_buf
;
2145 #endif /* ENABLE_NLS */
2146 edit
= g_malloc0 (sizeof (WEdit
));
2147 edit
->search
= NULL
;
2150 edit_purge_widget (edit
);
2151 edit
->num_widget_lines
= lines
;
2153 edit
->num_widget_columns
= columns
;
2154 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2155 edit
->stat1
.st_uid
= getuid ();
2156 edit
->stat1
.st_gid
= getgid ();
2157 edit
->stat1
.st_mtime
= 0;
2159 edit
->force
|= REDRAW_PAGE
;
2160 edit_set_filename (edit
, filename
);
2161 edit
->undo_stack_size
= START_STACK_SIZE
;
2162 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2163 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2165 edit
->redo_stack_size
= START_STACK_SIZE
;
2166 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2167 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2170 edit
->converter
= str_cnv_from_term
;
2171 edit_set_codeset (edit
);
2173 if (edit_load_file (edit
))
2175 /* edit_load_file already gives an error message */
2181 edit
->loading_done
= 1;
2184 edit_load_syntax (edit
, NULL
, NULL
);
2187 edit_get_syntax_color (edit
, -1, &color
);
2190 /* load saved cursor position */
2191 if ((line
== 0) && option_save_position
)
2193 edit_load_position (edit
);
2199 edit_move_display (edit
, line
- 1);
2200 edit_move_to_line (edit
, line
- 1);
2204 edit_load_macro_cmd (edit
);
2208 /* --------------------------------------------------------------------------------------------- */
2209 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2212 edit_clean (WEdit
* edit
)
2219 /* a stale lock, remove it */
2221 edit
->locked
= edit_unlock_file (edit
);
2223 /* save cursor position */
2224 if (option_save_position
)
2225 edit_save_position (edit
);
2226 else if (edit
->serialized_bookmarks
!= NULL
)
2227 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2229 /* File specified on the mcedit command line and never saved */
2230 if (edit
->delete_file
)
2231 unlink (edit
->filename
);
2233 edit_free_syntax_rules (edit
);
2234 book_mark_flush (edit
, -1);
2235 for (; j
<= MAXBUFF
; j
++)
2237 g_free (edit
->buffers1
[j
]);
2238 g_free (edit
->buffers2
[j
]);
2241 g_free (edit
->undo_stack
);
2242 g_free (edit
->redo_stack
);
2243 g_free (edit
->filename
);
2245 mc_search_free (edit
->search
);
2246 edit
->search
= NULL
;
2248 if (edit
->converter
!= str_cnv_from_term
)
2249 str_close_conv (edit
->converter
);
2251 edit_purge_widget (edit
);
2256 /* --------------------------------------------------------------------------------------------- */
2257 /** returns 1 on success */
2260 edit_renew (WEdit
* edit
)
2262 int lines
= edit
->num_widget_lines
;
2263 int columns
= edit
->num_widget_columns
;
2266 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
2269 /* --------------------------------------------------------------------------------------------- */
2271 * Load a new file into the editor. If it fails, preserve the old file.
2272 * To do it, allocate a new widget, initialize it and, if the new file
2273 * was loaded, copy the data to the old widget.
2274 * Return 1 on success, 0 on failure.
2278 edit_reload (WEdit
* edit
, const char *filename
)
2281 int lines
= edit
->num_widget_lines
;
2282 int columns
= edit
->num_widget_columns
;
2284 e
= g_malloc0 (sizeof (WEdit
));
2285 e
->widget
= edit
->widget
;
2286 if (!edit_init (e
, lines
, columns
, filename
, 0))
2292 memcpy (edit
, e
, sizeof (WEdit
));
2297 /* --------------------------------------------------------------------------------------------- */
2299 * Load a new file into the editor and set line. If it fails, preserve the old file.
2300 * To do it, allocate a new widget, initialize it and, if the new file
2301 * was loaded, copy the data to the old widget.
2302 * Return 1 on success, 0 on failure.
2306 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2309 int lines
= edit
->num_widget_lines
;
2310 int columns
= edit
->num_widget_columns
;
2312 e
= g_malloc0 (sizeof (WEdit
));
2313 e
->widget
= edit
->widget
;
2314 if (!edit_init (e
, lines
, columns
, filename
, line
))
2320 memcpy (edit
, e
, sizeof (WEdit
));
2325 /* --------------------------------------------------------------------------------------------- */
2328 edit_set_codeset (WEdit
* edit
)
2333 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
2338 conv
= str_crt_conv_from (cp_id
);
2339 if (conv
!= INVALID_CONV
)
2341 if (edit
->converter
!= str_cnv_from_term
)
2342 str_close_conv (edit
->converter
);
2343 edit
->converter
= conv
;
2348 edit
->utf8
= str_isutf8 (cp_id
);
2355 /* --------------------------------------------------------------------------------------------- */
2357 Recording stack for undo:
2358 The following is an implementation of a compressed stack. Identical
2359 pushes are recorded by a negative prefix indicating the number of times the
2360 same char was pushed. This saves space for repeated curs-left or curs-right
2377 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2378 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2379 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2380 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2383 The only way the cursor moves or the buffer is changed is through the routines:
2384 insert, backspace, insert_ahead, delete, and cursor_move.
2385 These record the reverse undo movements onto the stack each time they are
2388 Each key press results in a set of actions (insert; delete ...). So each time
2389 a key is pressed the current position of start_display is pushed as
2390 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2391 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2392 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2397 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2399 unsigned long sp
= edit
->undo_stack_pointer
;
2403 /* first enlarge the stack if necessary */
2404 if (sp
> edit
->undo_stack_size
- 10)
2406 if (option_max_undo
< 256)
2407 option_max_undo
= 256;
2408 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2410 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2413 edit
->undo_stack
= t
;
2414 edit
->undo_stack_size
<<= 1;
2415 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2419 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2420 if (edit
->undo_stack_disable
)
2422 edit_push_redo_action (edit
, KEY_PRESS
);
2423 edit_push_redo_action (edit
, c
);
2426 else if (edit
->redo_stack_reset
)
2428 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2431 if (edit
->undo_stack_bottom
!= sp
2432 && spm1
!= edit
->undo_stack_bottom
2433 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2436 if (edit
->undo_stack
[spm1
] < 0)
2438 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2441 if (edit
->undo_stack
[spm1
] > -1000000000)
2443 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2445 edit
->undo_stack
[spm1
]--;
2453 d
= edit
->undo_stack
[spm1
];
2457 return; /* --> no need to push multiple do-nothings */
2458 edit
->undo_stack
[sp
] = -2;
2463 edit
->undo_stack
[sp
] = c
;
2466 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2468 /* if the sp wraps round and catches the undo_stack_bottom then erase
2469 * the first set of actions on the stack to make space - by moving
2470 * undo_stack_bottom forward one "key press" */
2471 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2472 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2473 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2476 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2478 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2479 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2481 /*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: */
2482 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2483 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2485 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2490 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2492 unsigned long sp
= edit
->redo_stack_pointer
;
2495 /* first enlarge the stack if necessary */
2496 if (sp
> edit
->redo_stack_size
- 10)
2498 if (option_max_undo
< 256)
2499 option_max_undo
= 256;
2500 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2502 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2505 edit
->redo_stack
= t
;
2506 edit
->redo_stack_size
<<= 1;
2507 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2511 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2513 if (edit
->redo_stack_bottom
!= sp
2514 && spm1
!= edit
->redo_stack_bottom
2515 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2518 if (edit
->redo_stack
[spm1
] < 0)
2520 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2523 if (edit
->redo_stack
[spm1
] > -1000000000)
2525 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2526 edit
->redo_stack
[spm1
]--;
2533 d
= edit
->redo_stack
[spm1
];
2537 return; /* --> no need to push multiple do-nothings */
2538 edit
->redo_stack
[sp
] = -2;
2539 goto redo_check_bottom
;
2543 edit
->redo_stack
[sp
] = c
;
2546 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2548 /* if the sp wraps round and catches the redo_stack_bottom then erase
2549 * the first set of actions on the stack to make space - by moving
2550 * redo_stack_bottom forward one "key press" */
2551 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2552 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2553 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2556 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2558 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2559 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2562 * If a single key produced enough pushes to wrap all the way round then
2563 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2564 * The stack is then initialised:
2567 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2568 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2569 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2573 /* --------------------------------------------------------------------------------------------- */
2575 Basic low level single character buffer alterations and movements at the cursor.
2576 Returns char passed over, inserted or removed.
2580 edit_insert (WEdit
* edit
, int c
)
2582 /* check if file has grown to large */
2583 if (edit
->last_byte
>= SIZE_LIMIT
)
2586 /* first we must update the position of the display window */
2587 if (edit
->curs1
< edit
->start_display
)
2589 edit
->start_display
++;
2594 /* Mark file as modified, unless the file hasn't been fully loaded */
2595 if (edit
->loading_done
)
2597 edit_modification (edit
);
2600 /* now we must update some info on the file and check if a redraw is required */
2603 if (edit
->book_mark
)
2604 book_mark_inc (edit
, edit
->curs_line
);
2606 edit
->total_lines
++;
2607 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2610 /* save the reverse command onto the undo stack */
2611 /* ordinary char and not space */
2613 edit_push_undo_action (edit
, BACKSPACE
);
2615 edit_push_undo_action (edit
, BACKSPACE_BR
);
2616 /* update markers */
2617 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2618 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2619 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2621 /* add a new buffer if we've reached the end of the last one */
2622 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2623 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2625 /* perform the insertion */
2626 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2627 = (unsigned char) c
;
2629 /* update file length */
2632 /* update cursor position */
2636 /* --------------------------------------------------------------------------------------------- */
2637 /** same as edit_insert and move left */
2640 edit_insert_ahead (WEdit
* edit
, int c
)
2642 if (edit
->last_byte
>= SIZE_LIMIT
)
2645 if (edit
->curs1
< edit
->start_display
)
2647 edit
->start_display
++;
2651 edit_modification (edit
);
2654 if (edit
->book_mark
)
2655 book_mark_inc (edit
, edit
->curs_line
);
2656 edit
->total_lines
++;
2657 edit
->force
|= REDRAW_AFTER_CURSOR
;
2659 /* ordinary char and not space */
2661 edit_push_undo_action (edit
, DELCHAR
);
2663 edit_push_undo_action (edit
, DELCHAR_BR
);
2665 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2666 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2667 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2669 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2670 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2671 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2672 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2679 /* --------------------------------------------------------------------------------------------- */
2682 edit_delete (WEdit
* edit
, const int byte_delete
)
2692 /* if byte_delete = 1 then delete only one byte not multibyte char */
2693 if (edit
->utf8
&& byte_delete
== 0)
2695 edit_get_utf (edit
, edit
->curs1
, &cw
);
2700 if (edit
->mark2
!= edit
->mark1
)
2701 edit_push_markers (edit
);
2703 for (i
= 1; i
<= cw
; i
++)
2705 if (edit
->mark1
> edit
->curs1
)
2708 edit
->end_mark_curs
--;
2710 if (edit
->mark2
> edit
->curs1
)
2712 if (edit
->last_get_rule
> edit
->curs1
)
2713 edit
->last_get_rule
--;
2715 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2717 1) & M_EDIT_BUF_SIZE
) - 1];
2719 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2721 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2722 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2726 edit_push_undo_action (edit
, p
+ 256);
2729 edit_modification (edit
);
2732 if (edit
->book_mark
)
2733 book_mark_dec (edit
, edit
->curs_line
);
2734 edit
->total_lines
--;
2735 edit
->force
|= REDRAW_AFTER_CURSOR
;
2737 if (edit
->curs1
< edit
->start_display
)
2739 edit
->start_display
--;
2747 /* --------------------------------------------------------------------------------------------- */
2748 /** moves the cursor right or left: increment positive or negative respectively */
2751 edit_cursor_move (WEdit
* edit
, long increment
)
2753 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2758 for (; increment
< 0; increment
++)
2763 edit_push_undo_action (edit
, CURS_RIGHT
);
2765 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2766 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2767 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2768 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2769 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2771 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2772 1) & M_EDIT_BUF_SIZE
];
2773 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2775 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2776 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2782 edit
->force
|= REDRAW_LINE_BELOW
;
2787 else if (increment
> 0)
2789 for (; increment
> 0; increment
--)
2794 edit_push_undo_action (edit
, CURS_LEFT
);
2796 c
= edit_get_byte (edit
, edit
->curs1
);
2797 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2798 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2799 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2801 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2803 1) & M_EDIT_BUF_SIZE
) - 1];
2804 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2806 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2807 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2813 edit
->force
|= REDRAW_LINE_ABOVE
;
2819 /* These functions return positions relative to lines */
2821 /* --------------------------------------------------------------------------------------------- */
2822 /** returns index of last char on line + 1 */
2825 edit_eol (WEdit
* edit
, long current
)
2827 if (current
>= edit
->last_byte
)
2828 return edit
->last_byte
;
2831 if (edit_get_byte (edit
, current
) == '\n')
2836 /* --------------------------------------------------------------------------------------------- */
2837 /** returns index of first char on line */
2840 edit_bol (WEdit
* edit
, long current
)
2846 if (edit_get_byte (edit
, current
- 1) == '\n')
2851 /* --------------------------------------------------------------------------------------------- */
2854 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2857 if (upto
> edit
->last_byte
)
2858 upto
= edit
->last_byte
;
2861 while (current
< upto
)
2862 if (edit_get_byte (edit
, current
++) == '\n')
2867 /* --------------------------------------------------------------------------------------------- */
2868 /* If lines is zero this returns the count of lines from current to upto. */
2869 /* If upto is zero returns index of lines forward current. */
2872 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2876 return edit_count_lines (edit
, current
, upto
);
2885 next
= edit_eol (edit
, current
) + 1;
2886 if (next
> edit
->last_byte
)
2895 /* --------------------------------------------------------------------------------------------- */
2896 /** Returns offset of 'lines' lines up from current */
2899 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2903 current
= edit_bol (edit
, current
);
2904 while ((lines
--) && current
!= 0)
2905 current
= edit_bol (edit
, current
- 1);
2909 /* --------------------------------------------------------------------------------------------- */
2910 /* If cols is zero this returns the count of columns from current to upto. */
2911 /* If upto is zero returns index of cols across from current. */
2914 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2925 q
= edit
->last_byte
+ 2;
2927 for (col
= 0, p
= current
; p
< q
; p
++)
2939 orig_c
= c
= edit_get_byte (edit
, p
);
2947 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2952 if (g_unichar_iswide (utf_ch
))
2955 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2959 c
= convert_to_display_c (c
);
2963 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2971 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
2972 /* '\r' is shown as ^M, so we must advance 2 characters */
2973 /* Caret notation for control characters */
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** returns the current column position of the cursor */
2985 edit_get_col (WEdit
* edit
)
2987 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
2990 /* --------------------------------------------------------------------------------------------- */
2991 /* Scrolling functions */
2992 /* --------------------------------------------------------------------------------------------- */
2995 edit_update_curs_row (WEdit
* edit
)
2997 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3000 /* --------------------------------------------------------------------------------------------- */
3003 edit_update_curs_col (WEdit
* edit
)
3005 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3008 /* --------------------------------------------------------------------------------------------- */
3011 edit_get_curs_col (const WEdit
* edit
)
3013 return edit
->curs_col
;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** moves the display start position up by i lines */
3020 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3022 unsigned long lines_above
= edit
->start_line
;
3023 if (i
> lines_above
)
3027 edit
->start_line
-= i
;
3028 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3029 edit
->force
|= REDRAW_PAGE
;
3030 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3032 edit_update_curs_row (edit
);
3036 /* --------------------------------------------------------------------------------------------- */
3037 /** returns 1 if could scroll, 0 otherwise */
3040 edit_scroll_downward (WEdit
* edit
, int i
)
3043 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
3044 if (lines_below
> 0)
3046 if (i
> lines_below
)
3048 edit
->start_line
+= i
;
3049 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3050 edit
->force
|= REDRAW_PAGE
;
3051 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3053 edit_update_curs_row (edit
);
3056 /* --------------------------------------------------------------------------------------------- */
3059 edit_scroll_right (WEdit
* edit
, int i
)
3061 edit
->force
|= REDRAW_PAGE
;
3062 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3063 edit
->start_col
-= i
;
3066 /* --------------------------------------------------------------------------------------------- */
3069 edit_scroll_left (WEdit
* edit
, int i
)
3071 if (edit
->start_col
)
3073 edit
->start_col
+= i
;
3074 if (edit
->start_col
> 0)
3075 edit
->start_col
= 0;
3076 edit
->force
|= REDRAW_PAGE
;
3077 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3081 /* --------------------------------------------------------------------------------------------- */
3082 /* high level cursor movement commands */
3083 /* --------------------------------------------------------------------------------------------- */
3086 edit_move_to_prev_col (WEdit
* edit
, long p
)
3088 int prev
= edit
->prev_col
;
3089 int over
= edit
->over_col
;
3090 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3092 if (option_cursor_beyond_eol
)
3094 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3095 edit_eol (edit
, edit
->curs1
));
3097 if (line_len
< prev
+ edit
->over_col
)
3099 edit
->over_col
= prev
+ over
- line_len
;
3100 edit
->prev_col
= line_len
;
3101 edit
->curs_col
= line_len
;
3105 edit
->curs_col
= prev
+ over
;
3106 edit
->prev_col
= edit
->curs_col
;
3113 if (is_in_indent (edit
) && option_fake_half_tabs
)
3115 edit_update_curs_col (edit
);
3117 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3119 int q
= edit
->curs_col
;
3120 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3121 p
= edit_bol (edit
, edit
->curs1
);
3122 edit_cursor_move (edit
,
3123 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3125 if (!left_of_four_spaces (edit
))
3126 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3132 /* --------------------------------------------------------------------------------------------- */
3135 line_is_blank (WEdit
* edit
, long line
)
3137 return is_blank (edit
, edit_find_line (edit
, line
));
3140 /* --------------------------------------------------------------------------------------------- */
3141 /** move cursor to line 'line' */
3144 edit_move_to_line (WEdit
* e
, long line
)
3146 if (line
< e
->curs_line
)
3147 edit_move_up (e
, e
->curs_line
- line
, 0);
3149 edit_move_down (e
, line
- e
->curs_line
, 0);
3150 edit_scroll_screen_over_cursor (e
);
3153 /* --------------------------------------------------------------------------------------------- */
3154 /** scroll window so that first visible line is 'line' */
3157 edit_move_display (WEdit
* e
, long line
)
3159 if (line
< e
->start_line
)
3160 edit_scroll_upward (e
, e
->start_line
- line
);
3162 edit_scroll_downward (e
, line
- e
->start_line
);
3165 /* --------------------------------------------------------------------------------------------- */
3166 /** save markers onto undo stack */
3169 edit_push_markers (WEdit
* edit
)
3171 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3172 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3173 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3176 /* --------------------------------------------------------------------------------------------- */
3179 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3188 /* --------------------------------------------------------------------------------------------- */
3189 /** highlight marker toggle */
3192 edit_mark_cmd (WEdit
* edit
, int unmark
)
3194 edit_push_markers (edit
);
3197 edit_set_markers (edit
, 0, 0, 0, 0);
3198 edit
->force
|= REDRAW_PAGE
;
3202 if (edit
->mark2
>= 0)
3204 edit
->end_mark_curs
= -1;
3205 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3206 edit
->curs_col
+ edit
->over_col
);
3207 edit
->force
|= REDRAW_PAGE
;
3211 edit
->end_mark_curs
= edit
->curs1
;
3212 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3213 edit
->curs_col
+ edit
->over_col
);
3218 /* --------------------------------------------------------------------------------------------- */
3219 /** highlight the word under cursor */
3222 edit_mark_current_word_cmd (WEdit
* edit
)
3226 for (pos
= edit
->curs1
; pos
!= 0; pos
--)
3230 c1
= edit_get_byte (edit
, pos
);
3231 c2
= edit_get_byte (edit
, pos
- 1);
3232 if (!isspace (c1
) && isspace (c2
))
3234 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3239 for (; pos
< edit
->last_byte
; pos
++)
3243 c1
= edit_get_byte (edit
, pos
);
3244 c2
= edit_get_byte (edit
, pos
+ 1);
3245 if (!isspace (c1
) && isspace (c2
))
3247 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3250 edit
->mark2
= min (pos
+ 1, edit
->last_byte
);
3252 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3255 /* --------------------------------------------------------------------------------------------- */
3258 edit_mark_current_line_cmd (WEdit
* edit
)
3260 long pos
= edit
->curs1
;
3262 edit
->mark1
= edit_bol (edit
, pos
);
3263 edit
->mark2
= edit_eol (edit
, pos
);
3265 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3268 /* --------------------------------------------------------------------------------------------- */
3271 edit_delete_line (WEdit
* edit
)
3274 * Delete right part of the line.
3275 * Note that edit_get_byte() returns '\n' when byte position is
3278 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3280 (void) edit_delete (edit
, 1);
3285 * Note that edit_delete() will not corrupt anything if called while
3286 * cursor position is EOF.
3288 (void) edit_delete (edit
, 1);
3291 * Delete left part of the line.
3292 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3294 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3296 (void) edit_backspace (edit
, 1);
3300 /* --------------------------------------------------------------------------------------------- */
3303 insert_spaces_tab (WEdit
* edit
, int half
)
3306 edit_update_curs_col (edit
);
3307 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3308 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3311 edit_insert (edit
, ' ');
3316 /* --------------------------------------------------------------------------------------------- */
3319 edit_indent_width (WEdit
* edit
, long p
)
3322 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3324 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3327 /* --------------------------------------------------------------------------------------------- */
3330 edit_insert_indent (WEdit
* edit
, int indent
)
3332 if (!option_fill_tabs_with_spaces
)
3334 while (indent
>= TAB_SIZE
)
3336 edit_insert (edit
, '\t');
3340 while (indent
-- > 0)
3341 edit_insert (edit
, ' ');
3344 /* --------------------------------------------------------------------------------------------- */
3347 edit_push_key_press (WEdit
* edit
)
3349 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3350 if (edit
->mark2
== -1)
3352 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3353 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3357 /* --------------------------------------------------------------------------------------------- */
3360 edit_find_bracket (WEdit
* edit
)
3362 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3363 if (last_bracket
!= edit
->bracket
)
3364 edit
->force
|= REDRAW_PAGE
;
3365 last_bracket
= edit
->bracket
;
3368 /* --------------------------------------------------------------------------------------------- */
3370 * This executes a command as though the user initiated it through a key
3371 * press. Callback with WIDGET_KEY as a message calls this after
3372 * translating the key press. This function can be used to pass any
3373 * command to the editor. Note that the screen wouldn't update
3374 * automatically. Either of command or char_for_insertion must be
3375 * passed as -1. Commands are executed, and char_for_insertion is
3376 * inserted at the cursor.
3380 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3382 if (command
== CK_Begin_Record_Macro
|| command
== CK_Begin_Record_Repeat
3383 || (macro_index
< 0 && (command
== CK_Begin_End_Macro
|| command
== CK_Begin_End_Repeat
)))
3386 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3389 if (macro_index
!= -1)
3391 edit
->force
|= REDRAW_COMPLETELY
;
3392 if (command
== CK_End_Record_Macro
|| command
== CK_Begin_End_Macro
)
3394 edit_store_macro_cmd (edit
);
3398 else if (command
== CK_End_Record_Repeat
|| command
== CK_Begin_End_Repeat
)
3400 edit_repeat_macro_cmd (edit
);
3407 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3409 record_macro_buf
[macro_index
].action
= command
;
3410 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3412 /* record the beginning of a set of editing actions initiated by a key press */
3413 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
3414 edit_push_key_press (edit
);
3416 edit_execute_cmd (edit
, command
, char_for_insertion
);
3417 if (edit
->column_highlight
)
3418 edit
->force
|= REDRAW_PAGE
;
3421 /* --------------------------------------------------------------------------------------------- */
3423 This executes a command at a lower level than macro recording.
3424 It also does not push a key_press onto the undo stack. This means
3425 that if it is called many times, a single undo command will undo
3426 all of them. It also does not check for the Undo command.
3429 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3431 edit
->force
|= REDRAW_LINE
;
3433 /* The next key press will unhighlight the found string, so update
3435 if (edit
->found_len
|| edit
->column_highlight
)
3436 edit
->force
|= REDRAW_PAGE
;
3438 if (command
/ 100 == 6)
3439 { /* a highlight command like shift-arrow */
3440 edit
->column_highlight
= 0;
3441 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3443 edit_mark_cmd (edit
, 1); /* clear */
3444 edit_mark_cmd (edit
, 0); /* marking on */
3446 edit
->highlight
= 1;
3449 { /* any other command */
3450 if (edit
->highlight
)
3451 edit_mark_cmd (edit
, 0); /* clear */
3452 edit
->highlight
= 0;
3455 /* first check for undo */
3456 if (command
== CK_Undo
)
3458 edit
->redo_stack_reset
= 0;
3459 edit_group_undo (edit
);
3460 edit
->found_len
= 0;
3461 edit
->prev_col
= edit_get_col (edit
);
3462 edit
->search_start
= edit
->curs1
;
3465 /* check for redo */
3466 if (command
== CK_Redo
)
3468 edit
->redo_stack_reset
= 0;
3469 edit_do_redo (edit
);
3470 edit
->found_len
= 0;
3471 edit
->prev_col
= edit_get_col (edit
);
3472 edit
->search_start
= edit
->curs1
;
3476 edit
->redo_stack_reset
= 1;
3478 /* An ordinary key press */
3479 if (char_for_insertion
>= 0)
3481 /* if non persistent selection and text selected */
3482 if (!option_persistent_selections
)
3484 if (edit
->mark1
!= edit
->mark2
)
3485 edit_block_delete_cmd (edit
);
3487 if (edit
->overwrite
)
3489 /* remove char only one time, after input first byte, multibyte chars */
3490 if ((!utf8_display
|| edit
->charpoint
== 0)
3491 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3492 edit_delete (edit
, 0);
3494 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3495 edit_insert_over (edit
);
3497 if (char_for_insertion
> 255 && utf8_display
== 0)
3499 unsigned char str
[6 + 1];
3501 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3511 while (str
[i
] != 0 && i
<= 6)
3513 char_for_insertion
= str
[i
];
3514 edit_insert (edit
, char_for_insertion
);
3520 edit_insert (edit
, char_for_insertion
);
3522 if (option_auto_para_formatting
)
3524 format_paragraph (edit
, 0);
3525 edit
->force
|= REDRAW_PAGE
;
3528 check_and_wrap_line (edit
);
3529 edit
->found_len
= 0;
3530 edit
->prev_col
= edit_get_col (edit
);
3531 edit
->search_start
= edit
->curs1
;
3532 edit_find_bracket (edit
);
3540 case CK_Begin_Page_Highlight
:
3541 case CK_End_Page_Highlight
:
3548 if (edit
->mark2
>= 0)
3550 if (!option_persistent_selections
)
3552 if (edit
->column_highlight
)
3553 edit_push_undo_action (edit
, COLUMN_ON
);
3554 edit
->column_highlight
= 0;
3555 edit_mark_cmd (edit
, 1);
3564 case CK_Begin_Page_Highlight
:
3565 case CK_End_Page_Highlight
:
3570 case CK_Word_Left_Highlight
:
3571 case CK_Word_Right_Highlight
:
3572 case CK_Up_Highlight
:
3573 case CK_Down_Highlight
:
3574 case CK_Up_Alt_Highlight
:
3575 case CK_Down_Alt_Highlight
:
3576 if (edit
->mark2
== -1)
3577 break; /*marking is following the cursor: may need to highlight a whole line */
3580 case CK_Left_Highlight
:
3581 case CK_Right_Highlight
:
3582 edit
->force
|= REDRAW_CHAR_ONLY
;
3585 /* basic cursor key commands */
3589 /* if non persistent selection and text selected */
3590 if (!option_persistent_selections
)
3592 if (edit
->mark1
!= edit
->mark2
)
3594 edit_block_delete_cmd (edit
);
3598 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3603 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3605 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3606 edit_backspace (edit
, 1);
3611 if (option_fake_half_tabs
)
3614 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3616 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3617 edit_backspace (edit
, 1);
3622 edit_backspace (edit
, 0);
3625 /* if non persistent selection and text selected */
3626 if (!option_persistent_selections
)
3628 if (edit
->mark1
!= edit
->mark2
)
3630 edit_block_delete_cmd (edit
);
3635 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3636 edit_insert_over (edit
);
3638 if (option_fake_half_tabs
)
3641 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3643 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3644 edit_delete (edit
, 1);
3648 edit_delete (edit
, 0);
3650 case CK_Delete_Word_Left
:
3652 edit_left_delete_word (edit
);
3654 case CK_Delete_Word_Right
:
3655 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3656 edit_insert_over (edit
);
3658 edit_right_delete_word (edit
);
3660 case CK_Delete_Line
:
3661 edit_delete_line (edit
);
3663 case CK_Delete_To_Line_End
:
3664 edit_delete_to_line_end (edit
);
3666 case CK_Delete_To_Line_Begin
:
3667 edit_delete_to_line_begin (edit
);
3671 if (option_auto_para_formatting
)
3673 edit_double_newline (edit
);
3674 if (option_return_does_auto_indent
)
3675 edit_auto_indent (edit
);
3676 format_paragraph (edit
, 0);
3680 edit_insert (edit
, '\n');
3681 if (option_return_does_auto_indent
)
3683 edit_auto_indent (edit
);
3688 edit_insert (edit
, '\n');
3691 case CK_Page_Up_Alt_Highlight
:
3692 edit
->column_highlight
= 1;
3694 case CK_Page_Up_Highlight
:
3695 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3697 case CK_Page_Down_Alt_Highlight
:
3698 edit
->column_highlight
= 1;
3700 case CK_Page_Down_Highlight
:
3701 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3703 case CK_Left_Alt_Highlight
:
3704 edit
->column_highlight
= 1;
3706 case CK_Left_Highlight
:
3707 if (option_fake_half_tabs
)
3709 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3711 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3714 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3715 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3719 edit_left_char_move_cmd (edit
);
3721 case CK_Right_Alt_Highlight
:
3722 edit
->column_highlight
= 1;
3724 case CK_Right_Highlight
:
3725 if (option_fake_half_tabs
)
3727 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3729 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3730 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3734 edit_right_char_move_cmd (edit
);
3737 case CK_Begin_Page_Highlight
:
3738 edit_begin_page (edit
);
3741 case CK_End_Page_Highlight
:
3742 edit_end_page (edit
);
3745 case CK_Word_Left_Highlight
:
3747 edit_left_word_move_cmd (edit
);
3750 case CK_Word_Right_Highlight
:
3752 edit_right_word_move_cmd (edit
);
3754 case CK_Up_Alt_Highlight
:
3755 edit
->column_highlight
= 1;
3757 case CK_Up_Highlight
:
3758 edit_move_up (edit
, 1, 0);
3760 case CK_Down_Alt_Highlight
:
3761 edit
->column_highlight
= 1;
3763 case CK_Down_Highlight
:
3764 edit_move_down (edit
, 1, 0);
3766 case CK_Paragraph_Up_Alt_Highlight
:
3767 edit
->column_highlight
= 1;
3768 case CK_Paragraph_Up
:
3769 case CK_Paragraph_Up_Highlight
:
3770 edit_move_up_paragraph (edit
, 0);
3772 case CK_Paragraph_Down_Alt_Highlight
:
3773 edit
->column_highlight
= 1;
3774 case CK_Paragraph_Down
:
3775 case CK_Paragraph_Down_Highlight
:
3776 edit_move_down_paragraph (edit
, 0);
3778 case CK_Scroll_Up_Alt_Highlight
:
3779 edit
->column_highlight
= 1;
3781 case CK_Scroll_Up_Highlight
:
3782 edit_move_up (edit
, 1, 1);
3784 case CK_Scroll_Down_Alt_Highlight
:
3785 edit
->column_highlight
= 1;
3786 case CK_Scroll_Down
:
3787 case CK_Scroll_Down_Highlight
:
3788 edit_move_down (edit
, 1, 1);
3791 case CK_Home_Highlight
:
3792 edit_cursor_to_bol (edit
);
3795 case CK_End_Highlight
:
3796 edit_cursor_to_eol (edit
);
3799 /* if text marked shift block */
3800 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3802 if (edit
->mark2
< 0)
3803 edit_mark_cmd (edit
, 0);
3804 edit_move_block_to_right (edit
);
3808 if (option_cursor_beyond_eol
)
3809 edit_insert_over (edit
);
3810 edit_tab_cmd (edit
);
3811 if (option_auto_para_formatting
)
3813 format_paragraph (edit
, 0);
3814 edit
->force
|= REDRAW_PAGE
;
3818 check_and_wrap_line (edit
);
3823 case CK_Toggle_Insert
:
3824 edit
->overwrite
= (edit
->overwrite
== 0);
3828 if (edit
->mark2
>= 0)
3830 if (edit
->column_highlight
)
3831 edit_push_undo_action (edit
, COLUMN_ON
);
3832 edit
->column_highlight
= 0;
3834 edit_mark_cmd (edit
, 0);
3836 case CK_Column_Mark
:
3837 if (!edit
->column_highlight
)
3838 edit_push_undo_action (edit
, COLUMN_OFF
);
3839 edit
->column_highlight
= 1;
3840 edit_mark_cmd (edit
, 0);
3843 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3844 edit
->force
|= REDRAW_PAGE
;
3847 if (edit
->column_highlight
)
3848 edit_push_undo_action (edit
, COLUMN_ON
);
3849 edit
->column_highlight
= 0;
3850 edit_mark_cmd (edit
, 1);
3853 if (edit
->column_highlight
)
3854 edit_push_undo_action (edit
, COLUMN_ON
);
3855 edit
->column_highlight
= 0;
3856 edit_mark_current_word_cmd (edit
);
3859 if (edit
->column_highlight
)
3860 edit_push_undo_action (edit
, COLUMN_ON
);
3861 edit
->column_highlight
= 0;
3862 edit_mark_current_line_cmd (edit
);
3864 case CK_Toggle_Line_State
:
3865 option_line_state
= !option_line_state
;
3866 if (option_line_state
)
3868 option_line_state_width
= LINE_STATE_WIDTH
;
3872 option_line_state_width
= 0;
3874 edit
->force
|= REDRAW_PAGE
;
3877 case CK_Toggle_Show_Margin
:
3878 show_right_margin
= !show_right_margin
;
3879 edit
->force
|= REDRAW_PAGE
;
3882 case CK_Toggle_Bookmark
:
3883 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3884 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3885 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3887 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3889 case CK_Flush_Bookmarks
:
3890 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3891 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3892 edit
->force
|= REDRAW_PAGE
;
3894 case CK_Next_Bookmark
:
3895 if (edit
->book_mark
)
3897 struct _book_mark
*p
;
3898 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3902 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3903 || p
->line
< edit
->start_line
)
3904 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3905 edit_move_to_line (edit
, p
->line
);
3909 case CK_Prev_Bookmark
:
3910 if (edit
->book_mark
)
3912 struct _book_mark
*p
;
3913 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3914 while (p
->line
== edit
->curs_line
)
3919 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3920 || p
->line
< edit
->start_line
)
3921 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3922 edit_move_to_line (edit
, p
->line
);
3927 case CK_Beginning_Of_Text
:
3928 case CK_Beginning_Of_Text_Highlight
:
3929 edit_move_to_top (edit
);
3931 case CK_End_Of_Text
:
3932 case CK_End_Of_Text_Highlight
:
3933 edit_move_to_bottom (edit
);
3937 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3938 edit_insert_over (edit
);
3939 edit_block_copy_cmd (edit
);
3942 edit_block_delete_cmd (edit
);
3945 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3946 edit_insert_over (edit
);
3947 edit_block_move_cmd (edit
);
3950 case CK_Shift_Block_Left
:
3951 if (edit
->mark1
!= edit
->mark2
)
3952 edit_move_block_to_left (edit
);
3954 case CK_Shift_Block_Right
:
3955 if (edit
->mark1
!= edit
->mark2
)
3956 edit_move_block_to_right (edit
);
3959 edit_copy_to_X_buf_cmd (edit
);
3962 edit_cut_to_X_buf_cmd (edit
);
3965 /* if non persistent selection and text selected */
3966 if (!option_persistent_selections
)
3968 if (edit
->mark1
!= edit
->mark2
)
3969 edit_block_delete_cmd (edit
);
3971 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3972 edit_insert_over (edit
);
3973 edit_paste_from_X_buf_cmd (edit
);
3975 case CK_Selection_History
:
3976 edit_paste_from_history (edit
);
3980 edit_save_as_cmd (edit
);
3983 edit_save_confirm_cmd (edit
);
3986 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3989 edit_save_block_cmd (edit
);
3991 case CK_Insert_File
:
3992 edit_insert_file_cmd (edit
);
3995 case CK_Load_Prev_File
:
3996 edit_load_back_cmd (edit
);
3998 case CK_Load_Next_File
:
3999 edit_load_forward_cmd (edit
);
4002 case CK_Load_Syntax_File
:
4003 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
4005 case CK_Choose_Syntax
:
4006 edit_syntax_dialog (edit
);
4009 case CK_Load_Menu_File
:
4010 edit_load_cmd (edit
, EDIT_FILE_MENU
);
4013 case CK_Toggle_Syntax
:
4014 option_syntax_highlighting
^= 1;
4015 if (option_syntax_highlighting
== 1)
4016 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
4017 edit
->force
|= REDRAW_PAGE
;
4020 case CK_Toggle_Tab_TWS
:
4021 enable_show_tabs_tws
^= 1;
4022 edit
->force
|= REDRAW_PAGE
;
4026 edit_search_cmd (edit
, FALSE
);
4029 edit_search_cmd (edit
, TRUE
);
4032 edit_replace_cmd (edit
, 0);
4034 case CK_Replace_Again
:
4035 edit_replace_cmd (edit
, 1);
4037 case CK_Complete_Word
:
4038 /* if text marked shift block */
4039 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
4041 edit_move_block_to_left (edit
);
4045 edit_complete_word_cmd (edit
);
4048 case CK_Find_Definition
:
4049 edit_get_match_keyword_cmd (edit
);
4052 dlg_stop (edit
->widget
.owner
);
4055 edit_new_cmd (edit
);
4058 edit_help_cmd (edit
);
4061 edit_refresh_cmd (edit
);
4063 case CK_SaveSetupCmd
:
4072 case CK_Edit_Options
:
4073 edit_options_dialog (edit
);
4075 case CK_Edit_Save_Mode
:
4076 menu_save_mode_cmd ();
4081 /* fool gcc to prevent a Y2K warning */
4082 char time_format
[] = "_c";
4083 time_format
[0] = '%';
4085 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4086 edit_print_string (edit
, s
);
4087 edit
->force
|= REDRAW_PAGE
;
4092 edit_goto_cmd (edit
);
4094 case CK_Paragraph_Format
:
4095 format_paragraph (edit
, 1);
4096 edit
->force
|= REDRAW_PAGE
;
4098 case CK_Delete_Macro
:
4099 edit_delete_macro_cmd (edit
);
4101 case CK_Match_Bracket
:
4102 edit_goto_matching_bracket (edit
);
4108 edit_sort_cmd (edit
);
4111 edit_ext_cmd (edit
);
4114 edit_mail_dialog (edit
);
4119 case CK_SelectCodepage
:
4120 edit_select_codepage_cmd (edit
);
4122 case CK_Insert_Literal
:
4123 edit_insert_literal_cmd (edit
);
4125 case CK_Execute_Macro
:
4126 edit_execute_macro_cmd (edit
);
4128 case CK_Begin_End_Macro
:
4129 edit_begin_end_macro_cmd (edit
);
4131 case CK_Begin_End_Repeat
:
4132 edit_begin_end_repeat_cmd (edit
);
4142 if ((command
/ 10000) == 1) /* a shell command */
4143 edit_block_process_cmd (edit
, shell_cmd
[command
- 10000], 1);
4145 /* keys which must set the col position, and the search vars */
4151 case CK_Replace_Again
:
4152 case CK_Complete_Word
:
4153 edit
->prev_col
= edit_get_col (edit
);
4156 case CK_Up_Highlight
:
4157 case CK_Up_Alt_Highlight
:
4159 case CK_Down_Highlight
:
4160 case CK_Down_Alt_Highlight
:
4162 case CK_Page_Up_Highlight
:
4163 case CK_Page_Up_Alt_Highlight
:
4165 case CK_Page_Down_Highlight
:
4166 case CK_Page_Down_Alt_Highlight
:
4167 case CK_Beginning_Of_Text
:
4168 case CK_Beginning_Of_Text_Highlight
:
4169 case CK_End_Of_Text
:
4170 case CK_End_Of_Text_Highlight
:
4171 case CK_Paragraph_Up
:
4172 case CK_Paragraph_Up_Highlight
:
4173 case CK_Paragraph_Up_Alt_Highlight
:
4174 case CK_Paragraph_Down
:
4175 case CK_Paragraph_Down_Highlight
:
4176 case CK_Paragraph_Down_Alt_Highlight
:
4178 case CK_Scroll_Up_Highlight
:
4179 case CK_Scroll_Up_Alt_Highlight
:
4180 case CK_Scroll_Down
:
4181 case CK_Scroll_Down_Highlight
:
4182 case CK_Scroll_Down_Alt_Highlight
:
4183 edit
->search_start
= edit
->curs1
;
4184 edit
->found_len
= 0;
4187 edit
->found_len
= 0;
4188 edit
->prev_col
= edit_get_col (edit
);
4189 edit
->search_start
= edit
->curs1
;
4191 edit_find_bracket (edit
);
4193 if (option_auto_para_formatting
)
4199 case CK_Delete_Word_Left
:
4200 case CK_Delete_Word_Right
:
4201 case CK_Delete_To_Line_End
:
4202 case CK_Delete_To_Line_Begin
:
4203 format_paragraph (edit
, 0);
4204 edit
->force
|= REDRAW_PAGE
;
4209 /* --------------------------------------------------------------------------------------------- */
4212 edit_stack_init (void)
4214 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4216 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4217 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4220 edit_stack_iterator
= 0;
4223 /* --------------------------------------------------------------------------------------------- */
4226 edit_stack_free (void)
4228 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4229 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4232 /* --------------------------------------------------------------------------------------------- */
4236 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4238 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4241 /* --------------------------------------------------------------------------------------------- */
4245 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4247 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4250 /* --------------------------------------------------------------------------------------------- */
4253 edit_unlock_file (WEdit
* edit
)
4258 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4259 ret
= unlock_file (fullpath
);
4265 /* --------------------------------------------------------------------------------------------- */
4268 edit_lock_file (WEdit
* edit
)
4273 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4274 ret
= lock_file (fullpath
);
4280 /* --------------------------------------------------------------------------------------------- */