1 /* editor low level data handling and cursor fundamentals.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * \brief Source: editor low level data handling and cursor fundamentals
33 #include <sys/types.h>
42 #include "lib/global.h"
44 #include "lib/tty/color.h"
45 #include "lib/tty/tty.h" /* attrset() */
46 #include "lib/tty/key.h" /* is_idle() */
47 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
48 #include "lib/vfs/mc-vfs/vfs.h"
49 #include "lib/strutil.h" /* utf string functions */
50 #include "lib/util.h" /* load_file_position(), save_file_position() */
51 #include "lib/timefmt.h" /* time formatting */
53 #include "lib/widget.h"
56 #include "lib/charsets.h" /* get_codepage_id */
59 #include "src/filemanager/cmd.h" /* view_other_cmd() */
60 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
62 #include "src/main.h" /* source_codepage */
63 #include "src/setup.h" /* option_tab_spacing */
64 #include "src/learn.h" /* learn_keys */
65 #include "src/keybind-defaults.h"
67 #include "edit-impl.h"
68 #include "edit-widget.h"
70 /*** global variables ****************************************************************************/
72 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
73 int option_typewriter_wrap
= 0;
74 int option_auto_para_formatting
= 0;
75 int option_fill_tabs_with_spaces
= 0;
76 int option_return_does_auto_indent
= 1;
77 int option_backspace_through_tabs
= 0;
78 int option_fake_half_tabs
= 1;
79 int option_save_mode
= EDIT_QUICK_SAVE
;
80 int option_save_position
= 1;
81 int option_max_undo
= 32768;
82 int option_persistent_selections
= 1;
83 int option_cursor_beyond_eol
= 0;
84 int option_line_state
= 0;
85 int option_line_state_width
= 0;
87 int option_edit_right_extreme
= 0;
88 int option_edit_left_extreme
= 0;
89 int option_edit_top_extreme
= 0;
90 int option_edit_bottom_extreme
= 0;
91 int enable_show_tabs_tws
= 1;
92 int option_check_nl_at_eof
= 0;
93 int option_group_undo
= 0;
94 int show_right_margin
= 0;
96 const char *option_whole_chars_search
= "0123456789abcdefghijklmnopqrstuvwxyz_";
97 char *option_backup_ext
= NULL
;
99 int edit_stack_iterator
= 0;
100 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
101 /* magic sequense for say than block is vertical */
102 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
104 /*** file scope macro definitions ****************************************************************/
106 #define TEMP_BUF_LEN 1024
108 #define space_width 1
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 /* detecting an error on save is easy: just check if every byte has been written. */
115 /* detecting an error on read, is not so easy 'cos there is not way to tell
116 whether you read everything or not. */
117 /* FIXME: add proper `triple_pipe_open' to read, write and check errors. */
118 static const struct edit_filters
120 const char *read
, *write
, *extension
;
124 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
125 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
126 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
127 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
128 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
132 static long last_bracket
= -1;
134 static const char *const shell_cmd
[] = SHELL_COMMANDS_i
;
136 /*** file scope functions ************************************************************************/
137 /* --------------------------------------------------------------------------------------------- */
141 * here's a quick sketch of the layout: (don't run this through indent.)
143 * (b1 is buffers1 and b2 is buffers2)
146 * \0\0\0\0\0m e _ f i l e . \nf i n . \n|T h i s _ i s _ s o\0\0\0\0\0\0\0\0\0
147 * ______________________________________|______________________________________
149 * ... | b2[2] | b2[1] | b2[0] | b1[0] | b1[1] | b1[2] | ...
150 * |-> |-> |-> |-> |-> |-> |
152 * _<------------------------->|<----------------->_
153 * WEdit->curs2 | WEdit->curs1
158 * file end|||file beginning
167 /* --------------------------------------------------------------------------------------------- */
169 static void user_menu (WEdit
* edit
);
170 static int left_of_four_spaces (WEdit
* edit
);
171 static inline void edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
);
173 /* --------------------------------------------------------------------------------------------- */
178 const char *header
= N_("About");
179 const char *button_name
= N_("&OK");
180 const char *const version
= "MCEdit " VERSION
;
181 char text
[BUF_LARGE
];
183 int win_len
, version_len
, button_len
;
190 button_name
= _(button_name
);
193 button_len
= str_term_width1 (button_name
) + 5;
194 version_len
= str_term_width1 (version
);
196 g_snprintf (text
, sizeof (text
),
197 _("Copyright (C) 1996-2010 the Free Software Foundation\n\n"
198 " A user friendly text editor\n"
199 " written for the Midnight Commander"));
201 win_len
= str_term_width1 (header
);
202 win_len
= max (win_len
, version_len
);
203 win_len
= max (win_len
, button_len
);
205 /* count width and height of text */
206 str_msg_term_size (text
, &lines
, &cols
);
208 cols
= max (win_len
, cols
) + 6;
211 about_dlg
= create_dlg (TRUE
, 0, 0, lines
, cols
, dialog_colors
, NULL
,
212 "[Internal File Editor]", header
, DLG_CENTER
| DLG_TRYUP
);
214 add_widget (about_dlg
, label_new (3, (cols
- version_len
) / 2, version
));
215 add_widget (about_dlg
, label_new (5, 3, text
));
216 add_widget (about_dlg
, button_new (lines
- 3, (cols
- button_len
) / 2,
217 B_ENTER
, NORMAL_BUTTON
, button_name
, NULL
));
220 destroy_dlg (about_dlg
);
223 /* --------------------------------------------------------------------------------------------- */
225 * Initialize the buffers for an empty files.
229 edit_init_buffers (WEdit
* edit
)
233 for (j
= 0; j
<= MAXBUFF
; j
++)
235 edit
->buffers1
[j
] = NULL
;
236 edit
->buffers2
[j
] = NULL
;
241 edit
->buffers2
[0] = g_malloc0 (EDIT_BUF_SIZE
);
244 /* --------------------------------------------------------------------------------------------- */
246 * Load file OR text into buffers. Set cursor to the beginning of file.
247 * @returns 1 on error.
251 edit_load_file_fast (WEdit
* edit
, const char *filename
)
257 edit
->curs2
= edit
->last_byte
;
258 buf2
= edit
->curs2
>> S_EDIT_BUF_SIZE
;
260 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
265 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
266 edit_error_dialog (_("Error"), errmsg
);
271 if (!edit
->buffers2
[buf2
])
272 edit
->buffers2
[buf2
] = g_malloc0 (EDIT_BUF_SIZE
);
277 (char *) edit
->buffers2
[buf2
] + EDIT_BUF_SIZE
-
278 (edit
->curs2
& M_EDIT_BUF_SIZE
), edit
->curs2
& M_EDIT_BUF_SIZE
) < 0)
281 for (buf
= buf2
- 1; buf
>= 0; buf
--)
283 /* edit->buffers2[0] is already allocated */
284 if (!edit
->buffers2
[buf
])
285 edit
->buffers2
[buf
] = g_malloc0 (EDIT_BUF_SIZE
);
286 if (mc_read (file
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) < 0)
294 char *err_str
= g_strdup_printf (_("Error reading %s"), filename
);
295 edit_error_dialog (_("Error"), err_str
);
302 /* --------------------------------------------------------------------------------------------- */
303 /** Return index of the filter or -1 is there is no appropriate filter */
306 edit_find_filter (const char *filename
)
310 if (filename
== NULL
)
313 l
= strlen (filename
);
314 for (i
= 0; i
< sizeof (all_filters
) / sizeof (all_filters
[0]); i
++)
316 e
= strlen (all_filters
[i
].extension
);
318 if (!strcmp (all_filters
[i
].extension
, filename
+ l
- e
))
324 /* --------------------------------------------------------------------------------------------- */
327 edit_get_filter (const char *filename
)
330 char *p
, *quoted_name
;
332 i
= edit_find_filter (filename
);
336 quoted_name
= name_quote (filename
, 0);
337 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
338 g_free (quoted_name
);
342 /* --------------------------------------------------------------------------------------------- */
345 edit_insert_stream (WEdit
* edit
, FILE * f
)
349 while ((c
= fgetc (f
)) >= 0)
351 edit_insert (edit
, c
);
357 /* --------------------------------------------------------------------------------------------- */
358 /** Open file and create it if necessary. Return 0 for success, 1 for error. */
361 check_file_access (WEdit
* edit
, const char *filename
, struct stat
*st
)
364 gchar
*errmsg
= NULL
;
366 /* Try opening an existing file */
367 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
372 * Try creating the file. O_EXCL prevents following broken links
373 * and opening existing files.
375 file
= mc_open (filename
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
378 errmsg
= g_strdup_printf (_("Cannot open %s for reading"), filename
);
383 /* New file, delete it if it's not modified or saved */
384 edit
->delete_file
= 1;
388 /* Check what we have opened */
389 if (mc_fstat (file
, st
) < 0)
391 errmsg
= g_strdup_printf (_("Cannot get size/permissions for %s"), filename
);
395 /* We want to open regular files only */
396 if (!S_ISREG (st
->st_mode
))
398 errmsg
= g_strdup_printf (_("\"%s\" is not a regular file"), filename
);
403 * Don't delete non-empty files.
404 * O_EXCL should prevent it, but let's be on the safe side.
407 edit
->delete_file
= 0;
409 if (st
->st_size
>= SIZE_LIMIT
)
410 errmsg
= g_strdup_printf (_("File \"%s\" is too large"), filename
);
413 (void) mc_close (file
);
417 edit_error_dialog (_("Error"), errmsg
);
424 /* --------------------------------------------------------------------------------------------- */
426 * Open the file and load it into the buffers, either directly or using
427 * a filter. Return 0 on success, 1 on error.
429 * Fast loading (edit_load_file_fast) is used when the file size is
430 * known. In this case the data is read into the buffers by blocks.
431 * If the file size is not known, the data is loaded byte by byte in
436 edit_load_file (WEdit
* edit
)
440 /* Cannot do fast load if a filter is used */
441 if (edit_find_filter (edit
->filename
) >= 0)
445 * VFS may report file size incorrectly, and slow load is not a big
446 * deal considering overhead in VFS.
448 if (!vfs_file_is_local (edit
->filename
))
452 * FIXME: line end translation should disable fast loading as well
453 * Consider doing fseek() to the end and ftell() for the real size.
458 /* If we are dealing with a real file, check that it exists */
459 if (check_file_access (edit
, edit
->filename
, &edit
->stat1
))
464 /* nothing to load */
468 edit_init_buffers (edit
);
472 edit
->last_byte
= edit
->stat1
.st_size
;
473 edit_load_file_fast (edit
, edit
->filename
);
474 /* If fast load was used, the number of lines wasn't calculated */
475 edit
->total_lines
= edit_count_lines (edit
, 0, edit
->last_byte
);
482 edit
->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
);
560 edit
->macro_i
= -1; /* not recording a macro */
563 /* --------------------------------------------------------------------------------------------- */
566 edit_set_keymap (void)
568 editor_map
= default_editor_keymap
;
569 if (editor_keymap
&& editor_keymap
->len
> 0)
570 editor_map
= (global_keymap_t
*) editor_keymap
->data
;
572 editor_x_map
= default_editor_x_keymap
;
573 if (editor_x_keymap
&& editor_x_keymap
->len
> 0)
574 editor_x_map
= (global_keymap_t
*) editor_x_keymap
->data
;
577 /* --------------------------------------------------------------------------------------------- */
579 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
580 then the file should be as it was when he loaded up. Then set edit->modified to 0.
584 edit_pop_undo_action (WEdit
* edit
)
587 unsigned long sp
= edit
->undo_stack_pointer
;
589 if (sp
== edit
->undo_stack_bottom
)
592 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
593 c
= edit
->undo_stack
[sp
];
596 /* edit->undo_stack[sp] = '@'; */
597 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
601 if (sp
== edit
->undo_stack_bottom
)
604 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
605 if (edit
->undo_stack
[sp
] == -2)
607 /* edit->undo_stack[sp] = '@'; */
608 edit
->undo_stack_pointer
= sp
;
611 edit
->undo_stack
[sp
]++;
617 edit_pop_redo_action (WEdit
* edit
)
620 unsigned long sp
= edit
->redo_stack_pointer
;
622 if (sp
== edit
->redo_stack_bottom
)
625 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
626 c
= edit
->redo_stack
[sp
];
629 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
633 if (sp
== edit
->redo_stack_bottom
)
636 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
637 if (edit
->redo_stack
[sp
] == -2)
638 edit
->redo_stack_pointer
= sp
;
640 edit
->redo_stack
[sp
]++;
646 get_prev_undo_action (WEdit
* edit
)
649 unsigned long sp
= edit
->undo_stack_pointer
;
651 if (sp
== edit
->undo_stack_bottom
)
654 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
655 c
= edit
->undo_stack
[sp
];
659 if (sp
== edit
->undo_stack_bottom
)
662 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 (!(my_type_of (c1
) & my_type_of (c2
)))
1060 if (isspace (c1
) && !isspace (c2
))
1063 if (!isspace (c1
) && isspace (c2
))
1068 /* --------------------------------------------------------------------------------------------- */
1071 edit_left_word_move_cmd (WEdit
* edit
)
1073 edit_left_word_move (edit
, 0);
1074 edit
->force
|= REDRAW_PAGE
;
1077 /* --------------------------------------------------------------------------------------------- */
1080 edit_right_word_move (WEdit
* edit
, int s
)
1085 if (edit
->column_highlight
1086 && edit
->mark1
!= edit
->mark2
1087 && edit
->over_col
== 0 && edit
->curs1
== edit_eol (edit
, edit
->curs1
))
1089 edit_cursor_move (edit
, 1);
1090 if (edit
->curs1
>= edit
->last_byte
)
1092 c1
= edit_get_byte (edit
, edit
->curs1
- 1);
1093 c2
= edit_get_byte (edit
, edit
->curs1
);
1094 if (!(my_type_of (c1
) & my_type_of (c2
)))
1096 if (isspace (c1
) && !isspace (c2
))
1099 if (!isspace (c1
) && isspace (c2
))
1104 /* --------------------------------------------------------------------------------------------- */
1107 edit_right_word_move_cmd (WEdit
* edit
)
1109 edit_right_word_move (edit
, 0);
1110 edit
->force
|= REDRAW_PAGE
;
1113 /* --------------------------------------------------------------------------------------------- */
1116 edit_right_char_move_cmd (WEdit
* edit
)
1122 c
= edit_get_utf (edit
, edit
->curs1
, &cw
);
1128 c
= edit_get_byte (edit
, edit
->curs1
);
1130 if (option_cursor_beyond_eol
&& c
== '\n')
1136 edit_cursor_move (edit
, cw
);
1140 /* --------------------------------------------------------------------------------------------- */
1143 edit_left_char_move_cmd (WEdit
* edit
)
1146 if (edit
->column_highlight
1147 && option_cursor_beyond_eol
1148 && edit
->mark1
!= edit
->mark2
1149 && edit
->over_col
== 0 && edit
->curs1
== edit_bol (edit
, edit
->curs1
))
1153 edit_get_prev_utf (edit
, edit
->curs1
, &cw
);
1157 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1163 edit_cursor_move (edit
, -cw
);
1167 /* --------------------------------------------------------------------------------------------- */
1168 /** Up or down cursor moving.
1169 direction = TRUE - move up
1174 edit_move_updown (WEdit
* edit
, unsigned long i
, int do_scroll
, gboolean direction
)
1177 unsigned long l
= (direction
) ? edit
->curs_line
: edit
->total_lines
- edit
->curs_line
;
1186 edit
->force
|= REDRAW_PAGE
;
1190 edit_scroll_upward (edit
, i
);
1192 edit_scroll_downward (edit
, i
);
1194 p
= edit_bol (edit
, edit
->curs1
);
1196 p
= (direction
) ? edit_move_backward (edit
, p
, i
) : edit_move_forward (edit
, p
, i
, 0);
1198 edit_cursor_move (edit
, p
- edit
->curs1
);
1200 edit_move_to_prev_col (edit
, p
);
1202 /* search start of current multibyte char (like CJK) */
1203 if (edit
->curs1
+ 1 < edit
->last_byte
)
1205 edit_right_char_move_cmd (edit
);
1206 edit_left_char_move_cmd (edit
);
1209 edit
->search_start
= edit
->curs1
;
1210 edit
->found_len
= 0;
1213 /* --------------------------------------------------------------------------------------------- */
1216 edit_right_delete_word (WEdit
* edit
)
1221 if (edit
->curs1
>= edit
->last_byte
)
1223 c1
= edit_delete (edit
, 1);
1224 c2
= edit_get_byte (edit
, edit
->curs1
);
1225 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1227 if (!(my_type_of (c1
) & my_type_of (c2
)))
1232 /* --------------------------------------------------------------------------------------------- */
1235 edit_left_delete_word (WEdit
* edit
)
1240 if (edit
->curs1
<= 0)
1242 c1
= edit_backspace (edit
, 1);
1243 c2
= edit_get_byte (edit
, edit
->curs1
- 1);
1244 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1246 if (!(my_type_of (c1
) & my_type_of (c2
)))
1251 /* --------------------------------------------------------------------------------------------- */
1253 the start column position is not recorded, and hence does not
1254 undo as it happed. But who would notice.
1258 edit_do_undo (WEdit
* edit
)
1263 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1265 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1272 edit_cursor_move (edit
, 1);
1275 edit_cursor_move (edit
, -1);
1279 edit_backspace (edit
, 1);
1283 edit_delete (edit
, 1);
1286 edit
->column_highlight
= 1;
1289 edit
->column_highlight
= 0;
1292 if (ac
>= 256 && ac
< 512)
1293 edit_insert_ahead (edit
, ac
- 256);
1294 if (ac
>= 0 && ac
< 256)
1295 edit_insert (edit
, ac
);
1297 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1299 edit
->mark1
= ac
- MARK_1
;
1300 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1302 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1304 edit
->mark2
= ac
- MARK_2
;
1305 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1307 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1309 edit
->end_mark_curs
= ac
- MARK_CURS
;
1312 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1315 if (edit
->start_display
> ac
- KEY_PRESS
)
1317 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1318 edit
->force
|= REDRAW_PAGE
;
1320 else if (edit
->start_display
< ac
- KEY_PRESS
)
1322 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1323 edit
->force
|= REDRAW_PAGE
;
1325 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1326 edit_update_curs_row (edit
);
1329 edit
->undo_stack_disable
= 0;
1333 edit_do_redo (WEdit
* edit
)
1338 if (edit
->redo_stack_reset
)
1342 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1349 edit_cursor_move (edit
, 1);
1352 edit_cursor_move (edit
, -1);
1355 edit_backspace (edit
, 1);
1358 edit_delete (edit
, 1);
1361 edit
->column_highlight
= 1;
1364 edit
->column_highlight
= 0;
1367 if (ac
>= 256 && ac
< 512)
1368 edit_insert_ahead (edit
, ac
- 256);
1369 if (ac
>= 0 && ac
< 256)
1370 edit_insert (edit
, ac
);
1372 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1374 edit
->mark1
= ac
- MARK_1
;
1375 edit
->column1
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark1
), 0, edit
->mark1
);
1377 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1379 edit
->mark2
= ac
- MARK_2
;
1380 edit
->column2
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->mark2
), 0, edit
->mark2
);
1382 /* more than one pop usually means something big */
1384 edit
->force
|= REDRAW_PAGE
;
1387 if (edit
->start_display
> ac
- KEY_PRESS
)
1389 edit
->start_line
-= edit_count_lines (edit
, ac
- KEY_PRESS
, edit
->start_display
);
1390 edit
->force
|= REDRAW_PAGE
;
1392 else if (edit
->start_display
< ac
- KEY_PRESS
)
1394 edit
->start_line
+= edit_count_lines (edit
, edit
->start_display
, ac
- KEY_PRESS
);
1395 edit
->force
|= REDRAW_PAGE
;
1397 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1398 edit_update_curs_row (edit
);
1404 edit_group_undo (WEdit
* edit
)
1406 long ac
= KEY_PRESS
;
1407 long cur_ac
= KEY_PRESS
;
1408 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1410 cur_ac
= get_prev_undo_action (edit
);
1411 edit_do_undo (edit
);
1412 ac
= get_prev_undo_action (edit
);
1413 /* exit from cycle if option_group_undo is not set,
1414 * and make single UNDO operation
1416 if (!option_group_undo
)
1421 /* --------------------------------------------------------------------------------------------- */
1424 edit_delete_to_line_end (WEdit
* edit
)
1426 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
1430 edit_delete (edit
, 1);
1434 /* --------------------------------------------------------------------------------------------- */
1437 edit_delete_to_line_begin (WEdit
* edit
)
1439 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
1443 edit_backspace (edit
, 1);
1447 /* --------------------------------------------------------------------------------------------- */
1450 is_aligned_on_a_tab (WEdit
* edit
)
1452 edit_update_curs_col (edit
);
1453 return !((edit
->curs_col
% (TAB_SIZE
* space_width
))
1454 && edit
->curs_col
% (TAB_SIZE
* space_width
) != (HALF_TAB_SIZE
* space_width
));
1457 /* --------------------------------------------------------------------------------------------- */
1460 right_of_four_spaces (WEdit
* edit
)
1463 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1464 ch
|= edit_get_byte (edit
, edit
->curs1
- i
);
1466 return is_aligned_on_a_tab (edit
);
1470 /* --------------------------------------------------------------------------------------------- */
1473 left_of_four_spaces (WEdit
* edit
)
1476 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1477 ch
|= edit_get_byte (edit
, edit
->curs1
+ i
);
1479 return is_aligned_on_a_tab (edit
);
1483 /* --------------------------------------------------------------------------------------------- */
1486 edit_auto_indent (WEdit
* edit
)
1491 /* use the previous line as a template */
1492 p
= edit_move_backward (edit
, p
, 1);
1493 /* copy the leading whitespace of the line */
1495 { /* no range check - the line _is_ \n-terminated */
1496 c
= edit_get_byte (edit
, p
++);
1497 if (c
!= ' ' && c
!= '\t')
1499 edit_insert (edit
, c
);
1503 /* --------------------------------------------------------------------------------------------- */
1506 edit_double_newline (WEdit
* edit
)
1508 edit_insert (edit
, '\n');
1509 if (edit_get_byte (edit
, edit
->curs1
) == '\n')
1511 if (edit_get_byte (edit
, edit
->curs1
- 2) == '\n')
1513 edit
->force
|= REDRAW_PAGE
;
1514 edit_insert (edit
, '\n');
1517 /* --------------------------------------------------------------------------------------------- */
1520 edit_tab_cmd (WEdit
* edit
)
1524 if (option_fake_half_tabs
)
1526 if (is_in_indent (edit
))
1528 /*insert a half tab (usually four spaces) unless there is a
1529 half tab already behind, then delete it and insert a
1531 if (!option_fill_tabs_with_spaces
&& right_of_four_spaces (edit
))
1533 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1534 edit_backspace (edit
, 1);
1535 edit_insert (edit
, '\t');
1539 insert_spaces_tab (edit
, 1);
1544 if (option_fill_tabs_with_spaces
)
1546 insert_spaces_tab (edit
, 0);
1550 edit_insert (edit
, '\t');
1554 /* --------------------------------------------------------------------------------------------- */
1557 check_and_wrap_line (WEdit
* edit
)
1560 if (!option_typewriter_wrap
)
1562 edit_update_curs_col (edit
);
1563 if (edit
->curs_col
< option_word_wrap_line_length
)
1569 c
= edit_get_byte (edit
, curs
);
1570 if (c
== '\n' || curs
<= 0)
1572 edit_insert (edit
, '\n');
1575 if (c
== ' ' || c
== '\t')
1577 int current
= edit
->curs1
;
1578 edit_cursor_move (edit
, curs
- edit
->curs1
+ 1);
1579 edit_insert (edit
, '\n');
1580 edit_cursor_move (edit
, current
- edit
->curs1
+ 1);
1586 /* --------------------------------------------------------------------------------------------- */
1587 /** this find the matching bracket in either direction, and sets edit->bracket */
1590 edit_get_bracket (WEdit
* edit
, int in_screen
, unsigned long furthest_bracket_search
)
1592 const char *const b
= "{}{[][()(", *p
;
1593 int i
= 1, a
, inc
= -1, c
, d
, n
= 0;
1594 unsigned long j
= 0;
1596 edit_update_curs_row (edit
);
1597 c
= edit_get_byte (edit
, edit
->curs1
);
1600 if (!furthest_bracket_search
)
1601 furthest_bracket_search
--;
1602 /* not on a bracket at all */
1605 /* the matching bracket */
1607 /* going left or right? */
1608 if (strchr ("{[(", c
))
1610 for (q
= edit
->curs1
+ inc
;; q
+= inc
)
1612 /* out of buffer? */
1613 if (q
>= edit
->last_byte
|| q
< 0)
1615 a
= edit_get_byte (edit
, q
);
1616 /* don't want to eat CPU */
1617 if (j
++ > furthest_bracket_search
)
1619 /* out of screen? */
1622 if (q
< edit
->start_display
)
1624 /* count lines if searching downward */
1625 if (inc
> 0 && a
== '\n')
1626 if (n
++ >= edit
->num_widget_lines
- edit
->curs_row
) /* out of screen */
1629 /* count bracket depth */
1630 i
+= (a
== c
) - (a
== d
);
1631 /* return if bracket depth is zero */
1639 /* --------------------------------------------------------------------------------------------- */
1642 edit_goto_matching_bracket (WEdit
* edit
)
1646 q
= edit_get_bracket (edit
, 0, 0);
1649 edit
->bracket
= edit
->curs1
;
1650 edit
->force
|= REDRAW_PAGE
;
1651 edit_cursor_move (edit
, q
- edit
->curs1
);
1655 /* --------------------------------------------------------------------------------------------- */
1658 edit_execute_macro (WEdit
* edit
, struct macro macro
[], int n
)
1662 if (edit
->macro_depth
++ > 256)
1664 edit_error_dialog (_("Error"), _("Macro recursion is too deep"));
1665 edit
->macro_depth
--;
1668 edit
->force
|= REDRAW_PAGE
;
1671 edit_execute_cmd (edit
, macro
[i
].command
, macro
[i
].ch
);
1673 edit_update_screen (edit
);
1674 edit
->macro_depth
--;
1677 /* --------------------------------------------------------------------------------------------- */
1678 /** User edit menu, like user menu (F2) but only in editor. */
1681 user_menu (WEdit
* edit
)
1685 long start_mark
, end_mark
;
1688 block_file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
1690 nomark
= eval_marks (edit
, &start_mark
, &end_mark
);
1692 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1694 /* run shell scripts from menu */
1695 if (user_menu_cmd (edit
) && (mc_stat (block_file
, &status
) == 0) && (status
.st_size
!= 0))
1700 /* i.e. we have marked block */
1702 rc
= edit_block_delete_cmd (edit
);
1708 ins_len
= edit_insert_file (edit
, block_file
);
1709 if (nomark
== 0 && ins_len
> 0)
1710 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1712 /* truncate block file */
1713 fd
= fopen (block_file
, "w");
1717 edit_refresh_cmd (edit
);
1718 edit
->force
|= REDRAW_COMPLETELY
;
1720 g_free (block_file
);
1723 /* --------------------------------------------------------------------------------------------- */
1724 /*** public functions ****************************************************************************/
1725 /* --------------------------------------------------------------------------------------------- */
1728 edit_get_byte (WEdit
* edit
, long byte_index
)
1731 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1734 if (byte_index
>= edit
->curs1
)
1736 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1737 return edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1];
1741 return edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
][byte_index
& M_EDIT_BUF_SIZE
];
1745 /* --------------------------------------------------------------------------------------------- */
1748 edit_get_byte_ptr (WEdit
* edit
, long byte_index
)
1751 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1754 if (byte_index
>= edit
->curs1
)
1756 p
= edit
->curs1
+ edit
->curs2
- byte_index
- 1;
1757 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1758 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1762 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] +
1763 (byte_index
& M_EDIT_BUF_SIZE
));
1767 /* --------------------------------------------------------------------------------------------- */
1770 edit_get_buf_ptr (WEdit
* edit
, long byte_index
)
1774 if (byte_index
>= (edit
->curs1
+ edit
->curs2
))
1780 if (byte_index
>= edit
->curs1
)
1782 p
= edit
->curs1
+ edit
->curs2
- 1;
1783 return (char *) (edit
->buffers2
[p
>> S_EDIT_BUF_SIZE
] +
1784 (EDIT_BUF_SIZE
- (p
& M_EDIT_BUF_SIZE
) - 1));
1788 return (char *) (edit
->buffers1
[byte_index
>> S_EDIT_BUF_SIZE
] + (0 & M_EDIT_BUF_SIZE
));
1792 /* --------------------------------------------------------------------------------------------- */
1795 edit_get_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1800 gchar
*next_ch
= NULL
;
1803 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1809 str
= edit_get_byte_ptr (edit
, byte_index
);
1817 res
= g_utf8_get_char_validated (str
, -1);
1827 /* Calculate UTF-8 char width */
1828 next_ch
= g_utf8_next_char (str
);
1831 width
= next_ch
- str
;
1839 *char_width
= width
;
1843 /* --------------------------------------------------------------------------------------------- */
1846 edit_get_prev_utf (WEdit
* edit
, long byte_index
, int *char_width
)
1848 gchar
*str
, *buf
= NULL
;
1851 gchar
*next_ch
= NULL
;
1857 if (byte_index
>= (edit
->curs1
+ edit
->curs2
) || byte_index
< 0)
1863 ch
= edit_get_utf (edit
, byte_index
, &width
);
1867 *char_width
= width
;
1871 str
= edit_get_byte_ptr (edit
, byte_index
);
1872 buf
= edit_get_buf_ptr (edit
, byte_index
);
1873 if (str
== NULL
|| buf
== NULL
)
1878 /* get prev utf8 char */
1880 str
= g_utf8_find_prev_char (buf
, str
);
1888 res
= g_utf8_get_char_validated (str
, -1);
1898 /* Calculate UTF-8 char width */
1899 next_ch
= g_utf8_next_char (str
);
1902 width
= next_ch
- str
;
1910 *char_width
= width
;
1914 /* --------------------------------------------------------------------------------------------- */
1917 edit_get_write_filter (const char *write_name
, const char *filename
)
1920 char *p
, *writename
;
1922 i
= edit_find_filter (filename
);
1926 writename
= name_quote (write_name
, 0);
1927 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1932 /* --------------------------------------------------------------------------------------------- */
1935 edit_write_stream (WEdit
* edit
, FILE * f
)
1939 if (edit
->lb
== LB_ASIS
)
1941 for (i
= 0; i
< edit
->last_byte
; i
++)
1942 if (fputc (edit_get_byte (edit
, i
), f
) < 0)
1947 /* change line breaks */
1948 for (i
= 0; i
< edit
->last_byte
; i
++)
1950 unsigned char c
= edit_get_byte (edit
, i
);
1952 if (!(c
== '\n' || c
== '\r'))
1954 /* not line break */
1955 if (fputc (c
, f
) < 0)
1959 { /* (c == '\n' || c == '\r') */
1960 unsigned char c1
= edit_get_byte (edit
, i
+ 1); /* next char */
1964 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1965 /* put one line break unconditionally */
1966 if (fputc ('\n', f
) < 0)
1969 i
++; /* 2 chars are processed */
1971 if (c
== '\r' && c1
== '\n')
1972 /* Windows line break; go to the next char */
1975 if (c
== '\r' && c1
== '\r')
1977 /* two Macintosh line breaks; put second line break */
1978 if (fputc ('\n', f
) < 0)
1983 if (fputc (c1
, f
) < 0)
1987 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1988 /* put one line break unconditionally */
1989 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1992 if (c
== '\r' && c1
== '\n')
1993 /* Windows line break; go to the next char */
1997 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1998 /* put one line break unconditionally */
1999 if (fputc ('\r', f
) < 0)
2002 i
++; /* 2 chars are processed */
2004 if (c
== '\r' && c1
== '\n')
2005 /* Windows line break; go to the next char */
2008 if (c
== '\n' && c1
== '\n')
2010 /* two Windows line breaks; put second line break */
2011 if (fputc ('\r', f
) < 0)
2016 if (fputc (c1
, f
) < 0)
2019 case LB_ASIS
: /* default without changes */
2025 return edit
->last_byte
;
2028 /* --------------------------------------------------------------------------------------------- */
2029 /** inserts a file at the cursor, returns count of inserted bytes on success */
2031 edit_insert_file (WEdit
* edit
, const char *filename
)
2036 p
= edit_get_filter (filename
);
2040 long current
= edit
->curs1
;
2041 f
= (FILE *) popen (p
, "r");
2044 edit_insert_stream (edit
, f
);
2045 ins_len
= edit
->curs1
- current
;
2046 edit_cursor_move (edit
, current
- edit
->curs1
);
2050 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
2051 edit_error_dialog (_("Error"), errmsg
);
2060 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
2061 edit_error_dialog (_("Error"), errmsg
);
2070 int i
, file
, blocklen
;
2071 long current
= edit
->curs1
;
2072 int vertical_insertion
= 0;
2074 file
= mc_open (filename
, O_RDONLY
| O_BINARY
);
2077 buf
= g_malloc0 (TEMP_BUF_LEN
);
2078 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
2081 /* if contain signature VERTICAL_MAGIC then it vertical block */
2082 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
2083 vertical_insertion
= 1;
2085 mc_lseek (file
, 0, SEEK_SET
);
2087 if (vertical_insertion
)
2089 blocklen
= edit_insert_column_of_text_from_file (edit
, file
);
2093 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
2095 for (i
= 0; i
< blocklen
; i
++)
2096 edit_insert (edit
, buf
[i
]);
2099 ins_len
= edit
->curs1
- current
;
2100 edit_cursor_move (edit
, current
- edit
->curs1
);
2109 /* --------------------------------------------------------------------------------------------- */
2111 * Fill in the edit structure. Return NULL on failure. Pass edit as
2112 * NULL to allocate a new structure.
2114 * If line is 0, try to restore saved position. Otherwise put the
2115 * cursor on that line and show it in the middle of the screen.
2119 edit_init (WEdit
* edit
, int lines
, int columns
, const char *filename
, long line
)
2122 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2123 if (option_line_state
)
2125 option_line_state_width
= LINE_STATE_WIDTH
;
2129 option_line_state_width
= 0;
2135 * Expand option_whole_chars_search by national letters using
2139 static char option_whole_chars_search_buf
[256];
2141 if (option_whole_chars_search_buf
!= option_whole_chars_search
)
2144 size_t len
= str_term_width1 (option_whole_chars_search
);
2146 strcpy (option_whole_chars_search_buf
, option_whole_chars_search
);
2148 for (i
= 1; i
<= sizeof (option_whole_chars_search_buf
); i
++)
2150 if (g_ascii_islower ((gchar
) i
) && !strchr (option_whole_chars_search
, i
))
2152 option_whole_chars_search_buf
[len
++] = i
;
2156 option_whole_chars_search_buf
[len
] = 0;
2157 option_whole_chars_search
= option_whole_chars_search_buf
;
2159 #endif /* ENABLE_NLS */
2160 edit
= g_malloc0 (sizeof (WEdit
));
2161 edit
->search
= NULL
;
2164 edit_purge_widget (edit
);
2165 edit
->num_widget_lines
= lines
;
2167 edit
->num_widget_columns
= columns
;
2168 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2169 edit
->stat1
.st_uid
= getuid ();
2170 edit
->stat1
.st_gid
= getgid ();
2171 edit
->stat1
.st_mtime
= 0;
2173 edit
->force
|= REDRAW_PAGE
;
2174 edit_set_filename (edit
, filename
);
2175 edit
->undo_stack_size
= START_STACK_SIZE
;
2176 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2177 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2179 edit
->redo_stack_size
= START_STACK_SIZE
;
2180 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2181 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2184 edit
->converter
= str_cnv_from_term
;
2185 edit_set_codeset (edit
);
2187 if (edit_load_file (edit
))
2189 /* edit_load_file already gives an error message */
2195 edit
->loading_done
= 1;
2198 edit_load_syntax (edit
, NULL
, NULL
);
2201 edit_get_syntax_color (edit
, -1, &color
);
2204 /* load saved cursor position */
2205 if ((line
== 0) && option_save_position
)
2207 edit_load_position (edit
);
2213 edit_move_display (edit
, line
- 1);
2214 edit_move_to_line (edit
, line
- 1);
2222 /* --------------------------------------------------------------------------------------------- */
2223 /** Clear the edit struct, freeing everything in it. Return 1 on success */
2226 edit_clean (WEdit
* edit
)
2233 /* a stale lock, remove it */
2235 edit
->locked
= edit_unlock_file (edit
);
2237 /* save cursor position */
2238 if (option_save_position
)
2239 edit_save_position (edit
);
2240 else if (edit
->serialized_bookmarks
!= NULL
)
2241 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2243 /* File specified on the mcedit command line and never saved */
2244 if (edit
->delete_file
)
2245 unlink (edit
->filename
);
2247 edit_free_syntax_rules (edit
);
2248 book_mark_flush (edit
, -1);
2249 for (; j
<= MAXBUFF
; j
++)
2251 g_free (edit
->buffers1
[j
]);
2252 g_free (edit
->buffers2
[j
]);
2255 g_free (edit
->undo_stack
);
2256 g_free (edit
->redo_stack
);
2257 g_free (edit
->filename
);
2260 mc_search_free (edit
->search
);
2261 edit
->search
= NULL
;
2263 if (edit
->converter
!= str_cnv_from_term
)
2264 str_close_conv (edit
->converter
);
2266 edit_purge_widget (edit
);
2271 /* --------------------------------------------------------------------------------------------- */
2272 /** returns 1 on success */
2275 edit_renew (WEdit
* edit
)
2277 int lines
= edit
->num_widget_lines
;
2278 int columns
= edit
->num_widget_columns
;
2281 return (edit_init (edit
, lines
, columns
, "", 0) != NULL
);
2284 /* --------------------------------------------------------------------------------------------- */
2286 * Load a new file into the editor. If it fails, preserve the old file.
2287 * To do it, allocate a new widget, initialize it and, if the new file
2288 * was loaded, copy the data to the old widget.
2289 * Return 1 on success, 0 on failure.
2293 edit_reload (WEdit
* edit
, const char *filename
)
2296 int lines
= edit
->num_widget_lines
;
2297 int columns
= edit
->num_widget_columns
;
2299 e
= g_malloc0 (sizeof (WEdit
));
2300 e
->widget
= edit
->widget
;
2301 if (!edit_init (e
, lines
, columns
, filename
, 0))
2307 memcpy (edit
, e
, sizeof (WEdit
));
2312 /* --------------------------------------------------------------------------------------------- */
2314 * Load a new file into the editor and set line. If it fails, preserve the old file.
2315 * To do it, allocate a new widget, initialize it and, if the new file
2316 * was loaded, copy the data to the old widget.
2317 * Return 1 on success, 0 on failure.
2321 edit_reload_line (WEdit
* edit
, const char *filename
, long line
)
2324 int lines
= edit
->num_widget_lines
;
2325 int columns
= edit
->num_widget_columns
;
2327 e
= g_malloc0 (sizeof (WEdit
));
2328 e
->widget
= edit
->widget
;
2329 if (!edit_init (e
, lines
, columns
, filename
, line
))
2335 memcpy (edit
, e
, sizeof (WEdit
));
2340 /* --------------------------------------------------------------------------------------------- */
2343 edit_set_codeset (WEdit
* edit
)
2348 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
2353 conv
= str_crt_conv_from (cp_id
);
2354 if (conv
!= INVALID_CONV
)
2356 if (edit
->converter
!= str_cnv_from_term
)
2357 str_close_conv (edit
->converter
);
2358 edit
->converter
= conv
;
2363 edit
->utf8
= str_isutf8 (cp_id
);
2370 /* --------------------------------------------------------------------------------------------- */
2372 Recording stack for undo:
2373 The following is an implementation of a compressed stack. Identical
2374 pushes are recorded by a negative prefix indicating the number of times the
2375 same char was pushed. This saves space for repeated curs-left or curs-right
2392 If the stack long int is 0-255 it represents a normal insert (from a backspace),
2393 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2394 of the cursor functions #define'd in edit-impl.h. 1000 through 700'000'000 is to
2395 set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2398 The only way the cursor moves or the buffer is changed is through the routines:
2399 insert, backspace, insert_ahead, delete, and cursor_move.
2400 These record the reverse undo movements onto the stack each time they are
2403 Each key press results in a set of actions (insert; delete ...). So each time
2404 a key is pressed the current position of start_display is pushed as
2405 KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2406 over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2407 tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2412 edit_push_undo_action (WEdit
* edit
, long c
, ...)
2414 unsigned long sp
= edit
->undo_stack_pointer
;
2418 /* first enlarge the stack if necessary */
2419 if (sp
> edit
->undo_stack_size
- 10)
2421 if (option_max_undo
< 256)
2422 option_max_undo
= 256;
2423 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2425 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2428 edit
->undo_stack
= t
;
2429 edit
->undo_stack_size
<<= 1;
2430 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2434 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2435 if (edit
->undo_stack_disable
)
2437 edit_push_redo_action (edit
, KEY_PRESS
);
2438 edit_push_redo_action (edit
, c
);
2441 else if (edit
->redo_stack_reset
)
2443 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2446 if (edit
->undo_stack_bottom
!= sp
2447 && spm1
!= edit
->undo_stack_bottom
2448 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2451 if (edit
->undo_stack
[spm1
] < 0)
2453 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2456 if (edit
->undo_stack
[spm1
] > -1000000000)
2458 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2460 edit
->undo_stack
[spm1
]--;
2468 d
= edit
->undo_stack
[spm1
];
2472 return; /* --> no need to push multiple do-nothings */
2473 edit
->undo_stack
[sp
] = -2;
2478 edit
->undo_stack
[sp
] = c
;
2481 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2483 /* if the sp wraps round and catches the undo_stack_bottom then erase
2484 * the first set of actions on the stack to make space - by moving
2485 * undo_stack_bottom forward one "key press" */
2486 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2487 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2488 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2491 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2493 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2494 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2496 /*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: */
2497 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2498 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2500 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2505 edit_push_redo_action (WEdit
* edit
, long c
, ...)
2507 unsigned long sp
= edit
->redo_stack_pointer
;
2510 /* first enlarge the stack if necessary */
2511 if (sp
> edit
->redo_stack_size
- 10)
2513 if (option_max_undo
< 256)
2514 option_max_undo
= 256;
2515 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2517 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2520 edit
->redo_stack
= t
;
2521 edit
->redo_stack_size
<<= 1;
2522 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2526 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2528 if (edit
->redo_stack_bottom
!= sp
2529 && spm1
!= edit
->redo_stack_bottom
2530 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2533 if (edit
->redo_stack
[spm1
] < 0)
2535 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2538 if (edit
->redo_stack
[spm1
] > -1000000000)
2540 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2541 edit
->redo_stack
[spm1
]--;
2548 d
= edit
->redo_stack
[spm1
];
2552 return; /* --> no need to push multiple do-nothings */
2553 edit
->redo_stack
[sp
] = -2;
2554 goto redo_check_bottom
;
2558 edit
->redo_stack
[sp
] = c
;
2561 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2563 /* if the sp wraps round and catches the redo_stack_bottom then erase
2564 * the first set of actions on the stack to make space - by moving
2565 * redo_stack_bottom forward one "key press" */
2566 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2567 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2568 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2571 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2573 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2574 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2577 * If a single key produced enough pushes to wrap all the way round then
2578 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2579 * The stack is then initialised:
2582 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2583 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2584 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2588 /* --------------------------------------------------------------------------------------------- */
2590 Basic low level single character buffer alterations and movements at the cursor.
2591 Returns char passed over, inserted or removed.
2595 edit_insert (WEdit
* edit
, int c
)
2597 /* check if file has grown to large */
2598 if (edit
->last_byte
>= SIZE_LIMIT
)
2601 /* first we must update the position of the display window */
2602 if (edit
->curs1
< edit
->start_display
)
2604 edit
->start_display
++;
2609 /* Mark file as modified, unless the file hasn't been fully loaded */
2610 if (edit
->loading_done
)
2612 edit_modification (edit
);
2615 /* now we must update some info on the file and check if a redraw is required */
2618 if (edit
->book_mark
)
2619 book_mark_inc (edit
, edit
->curs_line
);
2621 edit
->total_lines
++;
2622 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2625 /* save the reverse command onto the undo stack */
2626 /* ordinary char and not space */
2628 edit_push_undo_action (edit
, BACKSPACE
);
2630 edit_push_undo_action (edit
, BACKSPACE_BR
);
2631 /* update markers */
2632 edit
->mark1
+= (edit
->mark1
> edit
->curs1
);
2633 edit
->mark2
+= (edit
->mark2
> edit
->curs1
);
2634 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->curs1
);
2636 /* add a new buffer if we've reached the end of the last one */
2637 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2638 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2640 /* perform the insertion */
2641 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
]
2642 = (unsigned char) c
;
2644 /* update file length */
2647 /* update cursor position */
2651 /* --------------------------------------------------------------------------------------------- */
2652 /** same as edit_insert and move left */
2655 edit_insert_ahead (WEdit
* edit
, int c
)
2657 if (edit
->last_byte
>= SIZE_LIMIT
)
2660 if (edit
->curs1
< edit
->start_display
)
2662 edit
->start_display
++;
2666 edit_modification (edit
);
2669 if (edit
->book_mark
)
2670 book_mark_inc (edit
, edit
->curs_line
);
2671 edit
->total_lines
++;
2672 edit
->force
|= REDRAW_AFTER_CURSOR
;
2674 /* ordinary char and not space */
2676 edit_push_undo_action (edit
, DELCHAR
);
2678 edit_push_undo_action (edit
, DELCHAR_BR
);
2680 edit
->mark1
+= (edit
->mark1
>= edit
->curs1
);
2681 edit
->mark2
+= (edit
->mark2
>= edit
->curs1
);
2682 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->curs1
);
2684 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2685 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2686 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]
2687 [EDIT_BUF_SIZE
- (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2694 /* --------------------------------------------------------------------------------------------- */
2697 edit_delete (WEdit
* edit
, const int byte_delete
)
2707 /* if byte_delete = 1 then delete only one byte not multibyte char */
2708 if (edit
->utf8
&& byte_delete
== 0)
2710 edit_get_utf (edit
, edit
->curs1
, &cw
);
2715 if (edit
->mark2
!= edit
->mark1
)
2716 edit_push_markers (edit
);
2718 for (i
= 1; i
<= cw
; i
++)
2720 if (edit
->mark1
> edit
->curs1
)
2723 edit
->end_mark_curs
--;
2725 if (edit
->mark2
> edit
->curs1
)
2727 if (edit
->last_get_rule
> edit
->curs1
)
2728 edit
->last_get_rule
--;
2730 p
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2732 1) & M_EDIT_BUF_SIZE
) - 1];
2734 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2736 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2737 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = NULL
;
2741 edit_push_undo_action (edit
, p
+ 256);
2744 edit_modification (edit
);
2747 if (edit
->book_mark
)
2748 book_mark_dec (edit
, edit
->curs_line
);
2749 edit
->total_lines
--;
2750 edit
->force
|= REDRAW_AFTER_CURSOR
;
2752 if (edit
->curs1
< edit
->start_display
)
2754 edit
->start_display
--;
2762 /* --------------------------------------------------------------------------------------------- */
2763 /** moves the cursor right or left: increment positive or negative respectively */
2766 edit_cursor_move (WEdit
* edit
, long increment
)
2768 /* this is the same as a combination of two of the above routines, with only one push onto the undo stack */
2773 for (; increment
< 0; increment
++)
2778 edit_push_undo_action (edit
, CURS_RIGHT
);
2780 c
= edit_get_byte (edit
, edit
->curs1
- 1);
2781 if (!((edit
->curs2
+ 1) & M_EDIT_BUF_SIZE
))
2782 edit
->buffers2
[(edit
->curs2
+ 1) >> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2783 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2784 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1] = c
;
2786 c
= edit
->buffers1
[(edit
->curs1
- 1) >> S_EDIT_BUF_SIZE
][(edit
->curs1
-
2787 1) & M_EDIT_BUF_SIZE
];
2788 if (!((edit
->curs1
- 1) & M_EDIT_BUF_SIZE
))
2790 g_free (edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
]);
2791 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = NULL
;
2797 edit
->force
|= REDRAW_LINE_BELOW
;
2802 else if (increment
> 0)
2804 for (; increment
> 0; increment
--)
2809 edit_push_undo_action (edit
, CURS_LEFT
);
2811 c
= edit_get_byte (edit
, edit
->curs1
);
2812 if (!(edit
->curs1
& M_EDIT_BUF_SIZE
))
2813 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
] = g_malloc0 (EDIT_BUF_SIZE
);
2814 edit
->buffers1
[edit
->curs1
>> S_EDIT_BUF_SIZE
][edit
->curs1
& M_EDIT_BUF_SIZE
] = c
;
2816 c
= edit
->buffers2
[(edit
->curs2
- 1) >> S_EDIT_BUF_SIZE
][EDIT_BUF_SIZE
-
2818 1) & M_EDIT_BUF_SIZE
) - 1];
2819 if (!(edit
->curs2
& M_EDIT_BUF_SIZE
))
2821 g_free (edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
]);
2822 edit
->buffers2
[edit
->curs2
>> S_EDIT_BUF_SIZE
] = 0;
2828 edit
->force
|= REDRAW_LINE_ABOVE
;
2834 /* These functions return positions relative to lines */
2836 /* --------------------------------------------------------------------------------------------- */
2837 /** returns index of last char on line + 1 */
2840 edit_eol (WEdit
* edit
, long current
)
2842 if (current
>= edit
->last_byte
)
2843 return edit
->last_byte
;
2846 if (edit_get_byte (edit
, current
) == '\n')
2851 /* --------------------------------------------------------------------------------------------- */
2852 /** returns index of first char on line */
2855 edit_bol (WEdit
* edit
, long current
)
2861 if (edit_get_byte (edit
, current
- 1) == '\n')
2866 /* --------------------------------------------------------------------------------------------- */
2869 edit_count_lines (WEdit
* edit
, long current
, long upto
)
2872 if (upto
> edit
->last_byte
)
2873 upto
= edit
->last_byte
;
2876 while (current
< upto
)
2877 if (edit_get_byte (edit
, current
++) == '\n')
2882 /* --------------------------------------------------------------------------------------------- */
2883 /* If lines is zero this returns the count of lines from current to upto. */
2884 /* If upto is zero returns index of lines forward current. */
2887 edit_move_forward (WEdit
* edit
, long current
, long lines
, long upto
)
2891 return edit_count_lines (edit
, current
, upto
);
2900 next
= edit_eol (edit
, current
) + 1;
2901 if (next
> edit
->last_byte
)
2910 /* --------------------------------------------------------------------------------------------- */
2911 /** Returns offset of 'lines' lines up from current */
2914 edit_move_backward (WEdit
* edit
, long current
, long lines
)
2918 current
= edit_bol (edit
, current
);
2919 while ((lines
--) && current
!= 0)
2920 current
= edit_bol (edit
, current
- 1);
2924 /* --------------------------------------------------------------------------------------------- */
2925 /* If cols is zero this returns the count of columns from current to upto. */
2926 /* If upto is zero returns index of cols across from current. */
2929 edit_move_forward3 (WEdit
* edit
, long current
, int cols
, long upto
)
2940 q
= edit
->last_byte
+ 2;
2942 for (col
= 0, p
= current
; p
< q
; p
++)
2954 orig_c
= c
= edit_get_byte (edit
, p
);
2962 utf_ch
= edit_get_utf (edit
, p
, &cw
);
2967 if (g_unichar_iswide (utf_ch
))
2970 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2974 c
= convert_to_display_c (c
);
2978 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2986 else if ((c
< 32 || c
== 127) && (orig_c
== c
|| (!utf8_display
&& !edit
->utf8
)))
2987 /* '\r' is shown as ^M, so we must advance 2 characters */
2988 /* Caret notation for control characters */
2996 /* --------------------------------------------------------------------------------------------- */
2997 /** returns the current column position of the cursor */
3000 edit_get_col (WEdit
* edit
)
3002 return edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3005 /* --------------------------------------------------------------------------------------------- */
3006 /* Scrolling functions */
3007 /* --------------------------------------------------------------------------------------------- */
3010 edit_update_curs_row (WEdit
* edit
)
3012 edit
->curs_row
= edit
->curs_line
- edit
->start_line
;
3015 /* --------------------------------------------------------------------------------------------- */
3018 edit_update_curs_col (WEdit
* edit
)
3020 edit
->curs_col
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0, edit
->curs1
);
3023 /* --------------------------------------------------------------------------------------------- */
3026 edit_get_curs_col (const WEdit
* edit
)
3028 return edit
->curs_col
;
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** moves the display start position up by i lines */
3035 edit_scroll_upward (WEdit
* edit
, unsigned long i
)
3037 unsigned long lines_above
= edit
->start_line
;
3038 if (i
> lines_above
)
3042 edit
->start_line
-= i
;
3043 edit
->start_display
= edit_move_backward (edit
, edit
->start_display
, i
);
3044 edit
->force
|= REDRAW_PAGE
;
3045 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3047 edit_update_curs_row (edit
);
3051 /* --------------------------------------------------------------------------------------------- */
3052 /** returns 1 if could scroll, 0 otherwise */
3055 edit_scroll_downward (WEdit
* edit
, int i
)
3058 lines_below
= edit
->total_lines
- edit
->start_line
- (edit
->num_widget_lines
- 1);
3059 if (lines_below
> 0)
3061 if (i
> lines_below
)
3063 edit
->start_line
+= i
;
3064 edit
->start_display
= edit_move_forward (edit
, edit
->start_display
, i
, 0);
3065 edit
->force
|= REDRAW_PAGE
;
3066 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3068 edit_update_curs_row (edit
);
3071 /* --------------------------------------------------------------------------------------------- */
3074 edit_scroll_right (WEdit
* edit
, int i
)
3076 edit
->force
|= REDRAW_PAGE
;
3077 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3078 edit
->start_col
-= i
;
3081 /* --------------------------------------------------------------------------------------------- */
3084 edit_scroll_left (WEdit
* edit
, int i
)
3086 if (edit
->start_col
)
3088 edit
->start_col
+= i
;
3089 if (edit
->start_col
> 0)
3090 edit
->start_col
= 0;
3091 edit
->force
|= REDRAW_PAGE
;
3092 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
3096 /* --------------------------------------------------------------------------------------------- */
3097 /* high level cursor movement commands */
3098 /* --------------------------------------------------------------------------------------------- */
3101 edit_move_to_prev_col (WEdit
* edit
, long p
)
3103 int prev
= edit
->prev_col
;
3104 int over
= edit
->over_col
;
3105 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->curs1
);
3107 if (option_cursor_beyond_eol
)
3109 long line_len
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
3110 edit_eol (edit
, edit
->curs1
));
3112 if (line_len
< prev
+ edit
->over_col
)
3114 edit
->over_col
= prev
+ over
- line_len
;
3115 edit
->prev_col
= line_len
;
3116 edit
->curs_col
= line_len
;
3120 edit
->curs_col
= prev
+ over
;
3121 edit
->prev_col
= edit
->curs_col
;
3128 if (is_in_indent (edit
) && option_fake_half_tabs
)
3130 edit_update_curs_col (edit
);
3132 if (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
))
3134 int q
= edit
->curs_col
;
3135 edit
->curs_col
-= (edit
->curs_col
% (HALF_TAB_SIZE
* space_width
));
3136 p
= edit_bol (edit
, edit
->curs1
);
3137 edit_cursor_move (edit
,
3138 edit_move_forward3 (edit
, p
, edit
->curs_col
,
3140 if (!left_of_four_spaces (edit
))
3141 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, q
, 0) - edit
->curs1
);
3147 /* --------------------------------------------------------------------------------------------- */
3150 line_is_blank (WEdit
* edit
, long line
)
3152 return is_blank (edit
, edit_find_line (edit
, line
));
3155 /* --------------------------------------------------------------------------------------------- */
3156 /** move cursor to line 'line' */
3159 edit_move_to_line (WEdit
* e
, long line
)
3161 if (line
< e
->curs_line
)
3162 edit_move_up (e
, e
->curs_line
- line
, 0);
3164 edit_move_down (e
, line
- e
->curs_line
, 0);
3165 edit_scroll_screen_over_cursor (e
);
3168 /* --------------------------------------------------------------------------------------------- */
3169 /** scroll window so that first visible line is 'line' */
3172 edit_move_display (WEdit
* e
, long line
)
3174 if (line
< e
->start_line
)
3175 edit_scroll_upward (e
, e
->start_line
- line
);
3177 edit_scroll_downward (e
, line
- e
->start_line
);
3180 /* --------------------------------------------------------------------------------------------- */
3181 /** save markers onto undo stack */
3184 edit_push_markers (WEdit
* edit
)
3186 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3187 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
3188 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3191 /* --------------------------------------------------------------------------------------------- */
3194 edit_set_markers (WEdit
* edit
, long m1
, long m2
, int c1
, int c2
)
3203 /* --------------------------------------------------------------------------------------------- */
3204 /** highlight marker toggle */
3207 edit_mark_cmd (WEdit
* edit
, int unmark
)
3209 edit_push_markers (edit
);
3212 edit_set_markers (edit
, 0, 0, 0, 0);
3213 edit
->force
|= REDRAW_PAGE
;
3217 if (edit
->mark2
>= 0)
3219 edit
->end_mark_curs
= -1;
3220 edit_set_markers (edit
, edit
->curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3221 edit
->curs_col
+ edit
->over_col
);
3222 edit
->force
|= REDRAW_PAGE
;
3226 edit
->end_mark_curs
= edit
->curs1
;
3227 edit_set_markers (edit
, edit
->mark1
, edit
->curs1
, edit
->column1
,
3228 edit
->curs_col
+ edit
->over_col
);
3233 /* --------------------------------------------------------------------------------------------- */
3236 edit_delete_line (WEdit
* edit
)
3239 * Delete right part of the line.
3240 * Note that edit_get_byte() returns '\n' when byte position is
3243 while (edit_get_byte (edit
, edit
->curs1
) != '\n')
3245 (void) edit_delete (edit
, 1);
3250 * Note that edit_delete() will not corrupt anything if called while
3251 * cursor position is EOF.
3253 (void) edit_delete (edit
, 1);
3256 * Delete left part of the line.
3257 * Note, that edit_get_byte() returns '\n' when byte position is < 0.
3259 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n')
3261 (void) edit_backspace (edit
, 1);
3265 /* --------------------------------------------------------------------------------------------- */
3268 insert_spaces_tab (WEdit
* edit
, int half
)
3271 edit_update_curs_col (edit
);
3272 i
= ((edit
->curs_col
/ (option_tab_spacing
* space_width
/ (half
+ 1))) +
3273 1) * (option_tab_spacing
* space_width
/ (half
+ 1)) - edit
->curs_col
;
3276 edit_insert (edit
, ' ');
3281 /* --------------------------------------------------------------------------------------------- */
3284 edit_indent_width (WEdit
* edit
, long p
)
3287 while (strchr ("\t ", edit_get_byte (edit
, q
)) && q
< edit
->last_byte
- 1) /* move to the end of the leading whitespace of the line */
3289 return edit_move_forward3 (edit
, p
, 0, q
); /* count the number of columns of indentation */
3292 /* --------------------------------------------------------------------------------------------- */
3295 edit_insert_indent (WEdit
* edit
, int indent
)
3297 if (!option_fill_tabs_with_spaces
)
3299 while (indent
>= TAB_SIZE
)
3301 edit_insert (edit
, '\t');
3305 while (indent
-- > 0)
3306 edit_insert (edit
, ' ');
3309 /* --------------------------------------------------------------------------------------------- */
3312 edit_push_key_press (WEdit
* edit
)
3314 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3315 if (edit
->mark2
== -1)
3317 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3318 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3322 /* --------------------------------------------------------------------------------------------- */
3325 edit_find_bracket (WEdit
* edit
)
3327 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3328 if (last_bracket
!= edit
->bracket
)
3329 edit
->force
|= REDRAW_PAGE
;
3330 last_bracket
= edit
->bracket
;
3333 /* --------------------------------------------------------------------------------------------- */
3335 * This executes a command as though the user initiated it through a key
3336 * press. Callback with WIDGET_KEY as a message calls this after
3337 * translating the key press. This function can be used to pass any
3338 * command to the editor. Note that the screen wouldn't update
3339 * automatically. Either of command or char_for_insertion must be
3340 * passed as -1. Commands are executed, and char_for_insertion is
3341 * inserted at the cursor.
3345 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3347 if (command
== CK_Begin_Record_Macro
)
3350 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3353 if (command
== CK_End_Record_Macro
&& edit
->macro_i
!= -1)
3355 edit
->force
|= REDRAW_COMPLETELY
;
3356 edit_save_macro_cmd (edit
, edit
->macro
, edit
->macro_i
);
3360 if (edit
->macro_i
>= 0 && edit
->macro_i
< MAX_MACRO_LENGTH
- 1)
3362 edit
->macro
[edit
->macro_i
].command
= command
;
3363 edit
->macro
[edit
->macro_i
++].ch
= char_for_insertion
;
3365 /* record the beginning of a set of editing actions initiated by a key press */
3366 if (command
!= CK_Undo
&& command
!= CK_Ext_Mode
)
3367 edit_push_key_press (edit
);
3369 edit_execute_cmd (edit
, command
, char_for_insertion
);
3370 if (edit
->column_highlight
)
3371 edit
->force
|= REDRAW_PAGE
;
3374 /* --------------------------------------------------------------------------------------------- */
3376 This executes a command at a lower level than macro recording.
3377 It also does not push a key_press onto the undo stack. This means
3378 that if it is called many times, a single undo command will undo
3379 all of them. It also does not check for the Undo command.
3382 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3384 edit
->force
|= REDRAW_LINE
;
3386 /* The next key press will unhighlight the found string, so update
3388 if (edit
->found_len
|| edit
->column_highlight
)
3389 edit
->force
|= REDRAW_PAGE
;
3391 if (command
/ 100 == 6)
3392 { /* a highlight command like shift-arrow */
3393 edit
->column_highlight
= 0;
3394 if (!edit
->highlight
|| (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3396 edit_mark_cmd (edit
, 1); /* clear */
3397 edit_mark_cmd (edit
, 0); /* marking on */
3399 edit
->highlight
= 1;
3402 { /* any other command */
3403 if (edit
->highlight
)
3404 edit_mark_cmd (edit
, 0); /* clear */
3405 edit
->highlight
= 0;
3408 /* first check for undo */
3409 if (command
== CK_Undo
)
3411 edit
->redo_stack_reset
= 0;
3412 edit_group_undo (edit
);
3413 edit
->found_len
= 0;
3414 edit
->prev_col
= edit_get_col (edit
);
3415 edit
->search_start
= edit
->curs1
;
3418 /* check for redo */
3419 if (command
== CK_Redo
)
3421 edit
->redo_stack_reset
= 0;
3422 edit_do_redo (edit
);
3423 edit
->found_len
= 0;
3424 edit
->prev_col
= edit_get_col (edit
);
3425 edit
->search_start
= edit
->curs1
;
3429 edit
->redo_stack_reset
= 1;
3431 /* An ordinary key press */
3432 if (char_for_insertion
>= 0)
3434 /* if non persistent selection and text selected */
3435 if (!option_persistent_selections
)
3437 if (edit
->mark1
!= edit
->mark2
)
3438 edit_block_delete_cmd (edit
);
3440 if (edit
->overwrite
)
3442 /* remove char only one time, after input first byte, multibyte chars */
3443 if ((!utf8_display
|| edit
->charpoint
== 0)
3444 && edit_get_byte (edit
, edit
->curs1
) != '\n')
3445 edit_delete (edit
, 0);
3447 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3448 edit_insert_over (edit
);
3450 if (char_for_insertion
> 255 && utf8_display
== 0)
3452 unsigned char str
[6 + 1];
3454 int res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3464 while (str
[i
] != 0 && i
<= 6)
3466 char_for_insertion
= str
[i
];
3467 edit_insert (edit
, char_for_insertion
);
3473 edit_insert (edit
, char_for_insertion
);
3475 if (option_auto_para_formatting
)
3477 format_paragraph (edit
, 0);
3478 edit
->force
|= REDRAW_PAGE
;
3481 check_and_wrap_line (edit
);
3482 edit
->found_len
= 0;
3483 edit
->prev_col
= edit_get_col (edit
);
3484 edit
->search_start
= edit
->curs1
;
3485 edit_find_bracket (edit
);
3493 case CK_Begin_Page_Highlight
:
3494 case CK_End_Page_Highlight
:
3501 if (edit
->mark2
>= 0)
3503 if (!option_persistent_selections
)
3505 if (edit
->column_highlight
)
3506 edit_push_undo_action (edit
, COLUMN_ON
);
3507 edit
->column_highlight
= 0;
3508 edit_mark_cmd (edit
, 1);
3517 case CK_Begin_Page_Highlight
:
3518 case CK_End_Page_Highlight
:
3523 case CK_Word_Left_Highlight
:
3524 case CK_Word_Right_Highlight
:
3525 case CK_Up_Highlight
:
3526 case CK_Down_Highlight
:
3527 case CK_Up_Alt_Highlight
:
3528 case CK_Down_Alt_Highlight
:
3529 if (edit
->mark2
== -1)
3530 break; /*marking is following the cursor: may need to highlight a whole line */
3533 case CK_Left_Highlight
:
3534 case CK_Right_Highlight
:
3535 edit
->force
|= REDRAW_CHAR_ONLY
;
3538 /* basic cursor key commands */
3542 /* if non persistent selection and text selected */
3543 if (!option_persistent_selections
)
3545 if (edit
->mark1
!= edit
->mark2
)
3547 edit_block_delete_cmd (edit
);
3551 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3556 if (option_backspace_through_tabs
&& is_in_indent (edit
))
3558 while (edit_get_byte (edit
, edit
->curs1
- 1) != '\n' && edit
->curs1
> 0)
3559 edit_backspace (edit
, 1);
3564 if (option_fake_half_tabs
)
3567 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3569 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3570 edit_backspace (edit
, 1);
3575 edit_backspace (edit
, 0);
3578 /* if non persistent selection and text selected */
3579 if (!option_persistent_selections
)
3581 if (edit
->mark1
!= edit
->mark2
)
3583 edit_block_delete_cmd (edit
);
3588 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3589 edit_insert_over (edit
);
3591 if (option_fake_half_tabs
)
3594 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3596 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3597 edit_delete (edit
, 1);
3601 edit_delete (edit
, 0);
3603 case CK_Delete_Word_Left
:
3605 edit_left_delete_word (edit
);
3607 case CK_Delete_Word_Right
:
3608 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3609 edit_insert_over (edit
);
3611 edit_right_delete_word (edit
);
3613 case CK_Delete_Line
:
3614 edit_delete_line (edit
);
3616 case CK_Delete_To_Line_End
:
3617 edit_delete_to_line_end (edit
);
3619 case CK_Delete_To_Line_Begin
:
3620 edit_delete_to_line_begin (edit
);
3624 if (option_auto_para_formatting
)
3626 edit_double_newline (edit
);
3627 if (option_return_does_auto_indent
)
3628 edit_auto_indent (edit
);
3629 format_paragraph (edit
, 0);
3633 edit_insert (edit
, '\n');
3634 if (option_return_does_auto_indent
)
3636 edit_auto_indent (edit
);
3641 edit_insert (edit
, '\n');
3644 case CK_Page_Up_Alt_Highlight
:
3645 edit
->column_highlight
= 1;
3647 case CK_Page_Up_Highlight
:
3648 edit_move_up (edit
, edit
->num_widget_lines
- 1, 1);
3650 case CK_Page_Down_Alt_Highlight
:
3651 edit
->column_highlight
= 1;
3653 case CK_Page_Down_Highlight
:
3654 edit_move_down (edit
, edit
->num_widget_lines
- 1, 1);
3656 case CK_Left_Alt_Highlight
:
3657 edit
->column_highlight
= 1;
3659 case CK_Left_Highlight
:
3660 if (option_fake_half_tabs
)
3662 if (is_in_indent (edit
) && right_of_four_spaces (edit
))
3664 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3667 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3668 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3672 edit_left_char_move_cmd (edit
);
3674 case CK_Right_Alt_Highlight
:
3675 edit
->column_highlight
= 1;
3677 case CK_Right_Highlight
:
3678 if (option_fake_half_tabs
)
3680 if (is_in_indent (edit
) && left_of_four_spaces (edit
))
3682 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3683 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3687 edit_right_char_move_cmd (edit
);
3690 case CK_Begin_Page_Highlight
:
3691 edit_begin_page (edit
);
3694 case CK_End_Page_Highlight
:
3695 edit_end_page (edit
);
3698 case CK_Word_Left_Highlight
:
3700 edit_left_word_move_cmd (edit
);
3703 case CK_Word_Right_Highlight
:
3705 edit_right_word_move_cmd (edit
);
3707 case CK_Up_Alt_Highlight
:
3708 edit
->column_highlight
= 1;
3710 case CK_Up_Highlight
:
3711 edit_move_up (edit
, 1, 0);
3713 case CK_Down_Alt_Highlight
:
3714 edit
->column_highlight
= 1;
3716 case CK_Down_Highlight
:
3717 edit_move_down (edit
, 1, 0);
3719 case CK_Paragraph_Up_Alt_Highlight
:
3720 edit
->column_highlight
= 1;
3721 case CK_Paragraph_Up
:
3722 case CK_Paragraph_Up_Highlight
:
3723 edit_move_up_paragraph (edit
, 0);
3725 case CK_Paragraph_Down_Alt_Highlight
:
3726 edit
->column_highlight
= 1;
3727 case CK_Paragraph_Down
:
3728 case CK_Paragraph_Down_Highlight
:
3729 edit_move_down_paragraph (edit
, 0);
3731 case CK_Scroll_Up_Alt_Highlight
:
3732 edit
->column_highlight
= 1;
3734 case CK_Scroll_Up_Highlight
:
3735 edit_move_up (edit
, 1, 1);
3737 case CK_Scroll_Down_Alt_Highlight
:
3738 edit
->column_highlight
= 1;
3739 case CK_Scroll_Down
:
3740 case CK_Scroll_Down_Highlight
:
3741 edit_move_down (edit
, 1, 1);
3744 case CK_Home_Highlight
:
3745 edit_cursor_to_bol (edit
);
3748 case CK_End_Highlight
:
3749 edit_cursor_to_eol (edit
);
3752 /* if text marked shift block */
3753 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3755 if (edit
->mark2
< 0)
3756 edit_mark_cmd (edit
, 0);
3757 edit_move_block_to_right (edit
);
3761 if (option_cursor_beyond_eol
)
3762 edit_insert_over (edit
);
3763 edit_tab_cmd (edit
);
3764 if (option_auto_para_formatting
)
3766 format_paragraph (edit
, 0);
3767 edit
->force
|= REDRAW_PAGE
;
3771 check_and_wrap_line (edit
);
3776 case CK_Toggle_Insert
:
3777 edit
->overwrite
= (edit
->overwrite
== 0);
3781 if (edit
->mark2
>= 0)
3783 if (edit
->column_highlight
)
3784 edit_push_undo_action (edit
, COLUMN_ON
);
3785 edit
->column_highlight
= 0;
3787 edit_mark_cmd (edit
, 0);
3789 case CK_Column_Mark
:
3790 if (!edit
->column_highlight
)
3791 edit_push_undo_action (edit
, COLUMN_OFF
);
3792 edit
->column_highlight
= 1;
3793 edit_mark_cmd (edit
, 0);
3796 edit_set_markers (edit
, 0, edit
->last_byte
, 0, 0);
3797 edit
->force
|= REDRAW_PAGE
;
3800 if (edit
->column_highlight
)
3801 edit_push_undo_action (edit
, COLUMN_ON
);
3802 edit
->column_highlight
= 0;
3803 edit_mark_cmd (edit
, 1);
3806 case CK_Toggle_Line_State
:
3807 option_line_state
= !option_line_state
;
3808 if (option_line_state
)
3810 option_line_state_width
= LINE_STATE_WIDTH
;
3814 option_line_state_width
= 0;
3816 edit
->force
|= REDRAW_PAGE
;
3819 case CK_Toggle_Show_Margin
:
3820 show_right_margin
= !show_right_margin
;
3821 edit
->force
|= REDRAW_PAGE
;
3824 case CK_Toggle_Bookmark
:
3825 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_FOUND_COLOR
);
3826 if (book_mark_query_color (edit
, edit
->curs_line
, BOOK_MARK_COLOR
))
3827 book_mark_clear (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3829 book_mark_insert (edit
, edit
->curs_line
, BOOK_MARK_COLOR
);
3831 case CK_Flush_Bookmarks
:
3832 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3833 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3834 edit
->force
|= REDRAW_PAGE
;
3836 case CK_Next_Bookmark
:
3837 if (edit
->book_mark
)
3839 struct _book_mark
*p
;
3840 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3844 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3845 || p
->line
< edit
->start_line
)
3846 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3847 edit_move_to_line (edit
, p
->line
);
3851 case CK_Prev_Bookmark
:
3852 if (edit
->book_mark
)
3854 struct _book_mark
*p
;
3855 p
= (struct _book_mark
*) book_mark_find (edit
, edit
->curs_line
);
3856 while (p
->line
== edit
->curs_line
)
3861 if (p
->line
>= edit
->start_line
+ edit
->num_widget_lines
3862 || p
->line
< edit
->start_line
)
3863 edit_move_display (edit
, p
->line
- edit
->num_widget_lines
/ 2);
3864 edit_move_to_line (edit
, p
->line
);
3869 case CK_Beginning_Of_Text
:
3870 case CK_Beginning_Of_Text_Highlight
:
3871 edit_move_to_top (edit
);
3873 case CK_End_Of_Text
:
3874 case CK_End_Of_Text_Highlight
:
3875 edit_move_to_bottom (edit
);
3879 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3880 edit_insert_over (edit
);
3881 edit_block_copy_cmd (edit
);
3884 edit_block_delete_cmd (edit
);
3887 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3888 edit_insert_over (edit
);
3889 edit_block_move_cmd (edit
);
3892 case CK_Shift_Block_Left
:
3893 if (edit
->mark1
!= edit
->mark2
)
3894 edit_move_block_to_left (edit
);
3896 case CK_Shift_Block_Right
:
3897 if (edit
->mark1
!= edit
->mark2
)
3898 edit_move_block_to_right (edit
);
3901 edit_copy_to_X_buf_cmd (edit
);
3904 edit_cut_to_X_buf_cmd (edit
);
3907 /* if non persistent selection and text selected */
3908 if (!option_persistent_selections
)
3910 if (edit
->mark1
!= edit
->mark2
)
3911 edit_block_delete_cmd (edit
);
3913 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3914 edit_insert_over (edit
);
3915 edit_paste_from_X_buf_cmd (edit
);
3917 case CK_Selection_History
:
3918 edit_paste_from_history (edit
);
3922 edit_save_as_cmd (edit
);
3925 edit_save_confirm_cmd (edit
);
3928 edit_load_cmd (edit
, EDIT_FILE_COMMON
);
3931 edit_save_block_cmd (edit
);
3933 case CK_Insert_File
:
3934 edit_insert_file_cmd (edit
);
3937 case CK_Load_Prev_File
:
3938 edit_load_back_cmd (edit
);
3940 case CK_Load_Next_File
:
3941 edit_load_forward_cmd (edit
);
3944 case CK_Load_Syntax_File
:
3945 edit_load_cmd (edit
, EDIT_FILE_SYNTAX
);
3947 case CK_Choose_Syntax
:
3948 edit_syntax_dialog (edit
);
3951 case CK_Load_Menu_File
:
3952 edit_load_cmd (edit
, EDIT_FILE_MENU
);
3955 case CK_Toggle_Syntax
:
3956 option_syntax_highlighting
^= 1;
3957 if (option_syntax_highlighting
== 1)
3958 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
3959 edit
->force
|= REDRAW_PAGE
;
3962 case CK_Toggle_Tab_TWS
:
3963 enable_show_tabs_tws
^= 1;
3964 edit
->force
|= REDRAW_PAGE
;
3968 edit_search_cmd (edit
, FALSE
);
3971 edit_search_cmd (edit
, TRUE
);
3974 edit_replace_cmd (edit
, 0);
3976 case CK_Replace_Again
:
3977 edit_replace_cmd (edit
, 1);
3979 case CK_Complete_Word
:
3980 /* if text marked shift block */
3981 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3983 edit_move_block_to_left (edit
);
3987 edit_complete_word_cmd (edit
);
3990 case CK_Find_Definition
:
3991 edit_get_match_keyword_cmd (edit
);
3994 dlg_stop (edit
->widget
.owner
);
3997 edit_new_cmd (edit
);
4000 edit_help_cmd (edit
);
4003 edit_refresh_cmd (edit
);
4005 case CK_SaveSetupCmd
:
4014 case CK_Edit_Options
:
4015 edit_options_dialog (edit
);
4017 case CK_Edit_Save_Mode
:
4018 menu_save_mode_cmd ();
4023 /* fool gcc to prevent a Y2K warning */
4024 char time_format
[] = "_c";
4025 time_format
[0] = '%';
4027 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
4028 edit_print_string (edit
, s
);
4029 edit
->force
|= REDRAW_PAGE
;
4034 edit_goto_cmd (edit
);
4036 case CK_Paragraph_Format
:
4037 format_paragraph (edit
, 1);
4038 edit
->force
|= REDRAW_PAGE
;
4040 case CK_Delete_Macro
:
4041 edit_delete_macro_cmd (edit
);
4043 case CK_Match_Bracket
:
4044 edit_goto_matching_bracket (edit
);
4050 edit_sort_cmd (edit
);
4053 edit_ext_cmd (edit
);
4056 edit_mail_dialog (edit
);
4061 case CK_SelectCodepage
:
4062 edit_select_codepage_cmd (edit
);
4064 case CK_Insert_Literal
:
4065 edit_insert_literal_cmd (edit
);
4067 case CK_Execute_Macro
:
4068 edit_execute_macro_cmd (edit
);
4070 case CK_Begin_End_Macro
:
4071 edit_begin_end_macro_cmd (edit
);
4081 if ((command
/ 1000) == 1) /* a shell command */
4082 edit_block_process_cmd (edit
, shell_cmd
[command
- 1000], 1);
4083 if (command
> CK_Macro (0) && command
<= CK_Last_Macro
)
4084 { /* a macro command */
4085 struct macro m
[MAX_MACRO_LENGTH
];
4087 if (edit_load_macro_cmd (edit
, m
, &nm
, command
- 2000))
4088 edit_execute_macro (edit
, m
, nm
);
4091 /* keys which must set the col position, and the search vars */
4097 case CK_Replace_Again
:
4098 case CK_Complete_Word
:
4099 edit
->prev_col
= edit_get_col (edit
);
4102 case CK_Up_Highlight
:
4103 case CK_Up_Alt_Highlight
:
4105 case CK_Down_Highlight
:
4106 case CK_Down_Alt_Highlight
:
4108 case CK_Page_Up_Highlight
:
4109 case CK_Page_Up_Alt_Highlight
:
4111 case CK_Page_Down_Highlight
:
4112 case CK_Page_Down_Alt_Highlight
:
4113 case CK_Beginning_Of_Text
:
4114 case CK_Beginning_Of_Text_Highlight
:
4115 case CK_End_Of_Text
:
4116 case CK_End_Of_Text_Highlight
:
4117 case CK_Paragraph_Up
:
4118 case CK_Paragraph_Up_Highlight
:
4119 case CK_Paragraph_Up_Alt_Highlight
:
4120 case CK_Paragraph_Down
:
4121 case CK_Paragraph_Down_Highlight
:
4122 case CK_Paragraph_Down_Alt_Highlight
:
4124 case CK_Scroll_Up_Highlight
:
4125 case CK_Scroll_Up_Alt_Highlight
:
4126 case CK_Scroll_Down
:
4127 case CK_Scroll_Down_Highlight
:
4128 case CK_Scroll_Down_Alt_Highlight
:
4129 edit
->search_start
= edit
->curs1
;
4130 edit
->found_len
= 0;
4133 edit
->found_len
= 0;
4134 edit
->prev_col
= edit_get_col (edit
);
4135 edit
->search_start
= edit
->curs1
;
4137 edit_find_bracket (edit
);
4139 if (option_auto_para_formatting
)
4145 case CK_Delete_Word_Left
:
4146 case CK_Delete_Word_Right
:
4147 case CK_Delete_To_Line_End
:
4148 case CK_Delete_To_Line_Begin
:
4149 format_paragraph (edit
, 0);
4150 edit
->force
|= REDRAW_PAGE
;
4155 /* --------------------------------------------------------------------------------------------- */
4158 edit_stack_init (void)
4160 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4162 edit_history_moveto
[edit_stack_iterator
].filename
= NULL
;
4163 edit_history_moveto
[edit_stack_iterator
].line
= -1;
4166 edit_stack_iterator
= 0;
4169 /* --------------------------------------------------------------------------------------------- */
4172 edit_stack_free (void)
4174 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
4175 g_free (edit_history_moveto
[edit_stack_iterator
].filename
);
4178 /* --------------------------------------------------------------------------------------------- */
4182 edit_move_up (WEdit
* edit
, unsigned long i
, int do_scroll
)
4184 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
4187 /* --------------------------------------------------------------------------------------------- */
4191 edit_move_down (WEdit
* edit
, unsigned long i
, int do_scroll
)
4193 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
4196 /* --------------------------------------------------------------------------------------------- */
4199 edit_unlock_file (WEdit
* edit
)
4204 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4205 ret
= unlock_file (fullpath
);
4211 /* --------------------------------------------------------------------------------------------- */
4214 edit_lock_file (WEdit
* edit
)
4219 fullpath
= g_build_filename (edit
->dir
, edit
->filename
, (char *) NULL
);
4220 ret
= lock_file (fullpath
);
4226 /* --------------------------------------------------------------------------------------------- */