2 Editor low level data handling and cursor fundamentals.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
10 Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
11 Andrew Borodin <aborodin@vmail.ru> 2012, 2013
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: editor low level data handling and cursor fundamentals
38 #include <sys/types.h>
44 #include <stdint.h> /* UINTMAX_MAX */
48 #include "lib/global.h"
50 #include "lib/tty/color.h"
51 #include "lib/tty/tty.h" /* attrset() */
52 #include "lib/tty/key.h" /* is_idle() */
53 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
54 #include "lib/vfs/vfs.h"
55 #include "lib/strutil.h" /* utf string functions */
56 #include "lib/util.h" /* load_file_position(), save_file_position() */
57 #include "lib/timefmt.h" /* time formatting */
59 #include "lib/widget.h"
62 #include "lib/charsets.h" /* get_codepage_id */
65 #include "src/filemanager/usermenu.h" /* user_menu_cmd() */
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/learn.h" /* learn_keys */
69 #include "src/keybind-defaults.h"
71 #include "edit-impl.h"
72 #include "editwidget.h"
77 /*** global variables ****************************************************************************/
79 int option_word_wrap_line_length
= DEFAULT_WRAP_LINE_LENGTH
;
80 int option_typewriter_wrap
= 0;
81 int option_auto_para_formatting
= 0;
82 int option_fill_tabs_with_spaces
= 0;
83 int option_return_does_auto_indent
= 1;
84 int option_backspace_through_tabs
= 0;
85 int option_fake_half_tabs
= 1;
86 int option_save_mode
= EDIT_QUICK_SAVE
;
87 int option_save_position
= 1;
88 int option_max_undo
= 32768;
89 int option_persistent_selections
= 1;
90 int option_cursor_beyond_eol
= 0;
91 int option_line_state
= 0;
92 int option_line_state_width
= 0;
93 gboolean option_cursor_after_inserted_block
= FALSE
;
95 int option_edit_right_extreme
= 0;
96 int option_edit_left_extreme
= 0;
97 int option_edit_top_extreme
= 0;
98 int option_edit_bottom_extreme
= 0;
99 int enable_show_tabs_tws
= 1;
100 int option_check_nl_at_eof
= 0;
101 int option_group_undo
= 0;
102 int show_right_margin
= 0;
104 char *option_backup_ext
= NULL
;
105 char *option_filesize_threshold
= NULL
;
107 unsigned int edit_stack_iterator
= 0;
108 edit_stack_type edit_history_moveto
[MAX_HISTORY_MOVETO
];
109 /* magic sequense for say than block is vertical */
110 const char VERTICAL_MAGIC
[] = { '\1', '\1', '\1', '\1', '\n' };
112 /*** file scope macro definitions ****************************************************************/
114 #define TEMP_BUF_LEN 1024
116 #define space_width 1
118 /*** file scope type declarations ****************************************************************/
120 /*** file scope variables ************************************************************************/
122 /* detecting an error on save is easy: just check if every byte has been written. */
123 /* detecting an error on read, is not so easy 'cos there is not way to tell
124 whether you read everything or not. */
125 /* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
126 static const struct edit_filters
128 const char *read
, *write
, *extension
;
132 { "xz -cd %s 2>&1", "xz > %s", ".xz"},
133 { "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
134 { "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
135 { "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
136 { "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
140 static const off_t option_filesize_default_threshold
= 64 * 1024 * 1024; /* 64 MB */
142 /* --------------------------------------------------------------------------------------------- */
143 /*** file scope functions ************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
146 * Load file OR text into buffers. Set cursor to the beginning of file.
148 * @return FALSE on error.
152 edit_load_file_fast (edit_buffer_t
* buf
, const vfs_path_t
* filename_vpath
)
157 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
163 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
164 edit_error_dialog (_("Error"), errmsg
);
169 ret
= (edit_buffer_read_file (buf
, file
, buf
->size
) == buf
->size
);
174 errmsg
= g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath
));
175 edit_error_dialog (_("Error"), errmsg
);
183 /* --------------------------------------------------------------------------------------------- */
184 /** Return index of the filter or -1 is there is no appropriate filter */
187 edit_find_filter (const vfs_path_t
* filename_vpath
)
191 if (filename_vpath
== NULL
)
194 l
= strlen (vfs_path_as_str (filename_vpath
));
195 for (i
= 0; i
< G_N_ELEMENTS (all_filters
); i
++)
199 e
= strlen (all_filters
[i
].extension
);
201 if (!strcmp (all_filters
[i
].extension
, vfs_path_as_str (filename_vpath
) + l
- e
))
207 /* --------------------------------------------------------------------------------------------- */
210 edit_get_filter (const vfs_path_t
* filename_vpath
)
213 char *p
, *quoted_name
;
215 i
= edit_find_filter (filename_vpath
);
219 quoted_name
= name_quote (vfs_path_as_str (filename_vpath
), 0);
220 p
= g_strdup_printf (all_filters
[i
].read
, quoted_name
);
221 g_free (quoted_name
);
225 /* --------------------------------------------------------------------------------------------- */
228 edit_insert_stream (WEdit
* edit
, FILE * f
)
233 while ((c
= fgetc (f
)) >= 0)
235 edit_insert (edit
, c
);
241 /* --------------------------------------------------------------------------------------------- */
243 * Open file and create it if necessary.
245 * @param edit editor object
246 * @param filename_vpath file name
247 * @param st buffer for store stat info
248 * @return TRUE for success, FALSE for error.
252 check_file_access (WEdit
* edit
, const vfs_path_t
* filename_vpath
, struct stat
*st
)
254 static uintmax_t threshold
= UINTMAX_MAX
;
256 gchar
*errmsg
= NULL
;
259 /* Try opening an existing file */
260 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
, 0666);
264 * Try creating the file. O_EXCL prevents following broken links
265 * and opening existing files.
267 file
= mc_open (filename_vpath
, O_NONBLOCK
| O_RDONLY
| O_BINARY
| O_CREAT
| O_EXCL
, 0666);
271 g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath
));
275 /* New file, delete it if it's not modified or saved */
276 edit
->delete_file
= 1;
279 /* Check what we have opened */
280 if (mc_fstat (file
, st
) < 0)
283 g_strdup_printf (_("Cannot get size/permissions for %s"),
284 vfs_path_as_str (filename_vpath
));
288 /* We want to open regular files only */
289 if (!S_ISREG (st
->st_mode
))
292 g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath
));
296 /* get file size threshold for alarm */
297 if (threshold
== UINTMAX_MAX
)
299 gboolean err
= FALSE
;
301 threshold
= parse_integer (option_filesize_threshold
, &err
);
303 threshold
= option_filesize_default_threshold
;
307 * Don't delete non-empty files.
308 * O_EXCL should prevent it, but let's be on the safe side.
311 edit
->delete_file
= 0;
313 if ((uintmax_t) st
->st_size
> threshold
)
317 errmsg
= g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
318 vfs_path_as_str (filename_vpath
));
319 act
= edit_query_dialog2 (_("Warning"), errmsg
, _("&Yes"), _("&No"));
328 (void) mc_close (file
);
332 edit_error_dialog (_("Error"), errmsg
);
340 /* --------------------------------------------------------------------------------------------- */
343 * Open the file and load it into the buffers, either directly or using
344 * a filter. Return TRUE on success, FALSE on error.
346 * Fast loading (edit_load_file_fast) is used when the file size is
347 * known. In this case the data is read into the buffers by blocks.
348 * If the file size is not known, the data is loaded byte by byte in
351 * @param edit editor object
352 * @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
355 edit_load_file (WEdit
* edit
)
357 gboolean fast_load
= TRUE
;
359 /* Cannot do fast load if a filter is used */
360 if (edit_find_filter (edit
->filename_vpath
) >= 0)
364 * FIXME: line end translation should disable fast loading as well
365 * Consider doing fseek() to the end and ftell() for the real size.
367 if (edit
->filename_vpath
!= NULL
)
370 * VFS may report file size incorrectly, and slow load is not a big
371 * deal considering overhead in VFS.
373 if (!vfs_file_is_local (edit
->filename_vpath
))
376 /* If we are dealing with a real file, check that it exists */
377 if (!check_file_access (edit
, edit
->filename_vpath
, &edit
->stat1
))
385 /* nothing to load */
391 edit_buffer_init (&edit
->buffer
, edit
->stat1
.st_size
);
393 if (!edit_load_file_fast (&edit
->buffer
, edit
->filename_vpath
))
401 edit_buffer_init (&edit
->buffer
, 0);
403 if (edit
->filename_vpath
!= NULL
404 && *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) != '\0')
406 edit
->undo_stack_disable
= 1;
407 if (edit_insert_file (edit
, edit
->filename_vpath
) < 0)
412 edit
->undo_stack_disable
= 0;
419 /* --------------------------------------------------------------------------------------------- */
420 /** Restore saved cursor position in the file */
423 edit_load_position (WEdit
* edit
)
428 if (edit
->filename_vpath
== NULL
429 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
432 load_file_position (edit
->filename_vpath
, &line
, &column
, &offset
, &edit
->serialized_bookmarks
);
436 edit_move_to_line (edit
, line
- 1);
437 edit
->prev_col
= column
;
441 edit_cursor_move (edit
, offset
);
442 line
= edit
->buffer
.curs_line
;
443 edit
->search_start
= edit
->buffer
.curs1
;
446 book_mark_restore (edit
, BOOK_MARK_COLOR
);
448 edit_move_to_prev_col (edit
, edit_buffer_get_current_bol (&edit
->buffer
));
449 edit_move_display (edit
, line
- (WIDGET (edit
)->lines
/ 2));
452 /* --------------------------------------------------------------------------------------------- */
453 /** Save cursor position in the file */
456 edit_save_position (WEdit
* edit
)
458 if (edit
->filename_vpath
== NULL
459 || *(vfs_path_get_by_index (edit
->filename_vpath
, 0)->path
) == '\0')
462 book_mark_serialize (edit
, BOOK_MARK_COLOR
);
463 save_file_position (edit
->filename_vpath
, edit
->buffer
.curs_line
+ 1, edit
->curs_col
,
464 edit
->buffer
.curs1
, edit
->serialized_bookmarks
);
465 edit
->serialized_bookmarks
= NULL
;
468 /* --------------------------------------------------------------------------------------------- */
469 /** Clean the WEdit stricture except the widget part */
472 edit_purge_widget (WEdit
* edit
)
474 size_t len
= sizeof (WEdit
) - sizeof (Widget
);
475 char *start
= (char *) edit
+ sizeof (Widget
);
476 memset (start
, 0, len
);
479 /* --------------------------------------------------------------------------------------------- */
482 TODO: if the user undos until the stack bottom, and the stack has not wrapped,
483 then the file should be as it was when he loaded up. Then set edit->modified to 0.
487 edit_pop_undo_action (WEdit
* edit
)
490 unsigned long sp
= edit
->undo_stack_pointer
;
492 if (sp
== edit
->undo_stack_bottom
)
495 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
496 c
= edit
->undo_stack
[sp
];
499 /* edit->undo_stack[sp] = '@'; */
500 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
504 if (sp
== edit
->undo_stack_bottom
)
507 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
508 if (edit
->undo_stack
[sp
] == -2)
510 /* edit->undo_stack[sp] = '@'; */
511 edit
->undo_stack_pointer
= sp
;
514 edit
->undo_stack
[sp
]++;
520 edit_pop_redo_action (WEdit
* edit
)
523 unsigned long sp
= edit
->redo_stack_pointer
;
525 if (sp
== edit
->redo_stack_bottom
)
528 sp
= (sp
- 1) & edit
->redo_stack_size_mask
;
529 c
= edit
->redo_stack
[sp
];
532 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
536 if (sp
== edit
->redo_stack_bottom
)
539 c
= edit
->redo_stack
[(sp
- 1) & edit
->redo_stack_size_mask
];
540 if (edit
->redo_stack
[sp
] == -2)
541 edit
->redo_stack_pointer
= sp
;
543 edit
->redo_stack
[sp
]++;
549 get_prev_undo_action (WEdit
* edit
)
552 unsigned long sp
= edit
->undo_stack_pointer
;
554 if (sp
== edit
->undo_stack_bottom
)
557 sp
= (sp
- 1) & edit
->undo_stack_size_mask
;
558 c
= edit
->undo_stack
[sp
];
562 if (sp
== edit
->undo_stack_bottom
)
565 c
= edit
->undo_stack
[(sp
- 1) & edit
->undo_stack_size_mask
];
569 /* --------------------------------------------------------------------------------------------- */
570 /** is called whenever a modification is made by one of the four routines below */
573 edit_modification (WEdit
* edit
)
575 edit
->caches_valid
= FALSE
;
577 /* raise lock when file modified */
578 if (!edit
->modified
&& !edit
->delete_file
)
579 edit
->locked
= lock_file (edit
->filename_vpath
);
583 /* --------------------------------------------------------------------------------------------- */
584 /* high level cursor movement commands */
585 /* --------------------------------------------------------------------------------------------- */
586 /** check whether cursor is in indent part of line
588 * @param edit editor object
590 * @return TRUE if cursor is in indent, FALSE otherwise
594 is_in_indent (const edit_buffer_t
* buf
)
598 for (p
= edit_buffer_get_current_bol (buf
); p
< buf
->curs1
; p
++)
599 if (strchr (" \t", edit_buffer_get_byte (buf
, p
)) == NULL
)
605 /* --------------------------------------------------------------------------------------------- */
606 /** check whether line in editor is blank or not
608 * @param edit editor object
609 * @param offset position in file
611 * @return TRUE if line in blank, FALSE otherwise
615 is_blank (const edit_buffer_t
* buf
, off_t offset
)
619 s
= edit_buffer_get_bol (buf
, offset
);
620 f
= edit_buffer_get_eol (buf
, offset
) - 1;
625 c
= edit_buffer_get_byte (buf
, s
++);
632 /* --------------------------------------------------------------------------------------------- */
633 /** returns the offset of line i */
636 edit_find_line (WEdit
* edit
, long line
)
639 long m
= 2000000000; /* what is the magic number? */
641 if (!edit
->caches_valid
)
643 memset (edit
->line_numbers
, 0, sizeof (edit
->line_numbers
));
644 memset (edit
->line_offsets
, 0, sizeof (edit
->line_offsets
));
645 /* three offsets that we *know* are line 0 at 0 and these two: */
646 edit
->line_numbers
[1] = edit
->buffer
.curs_line
;
647 edit
->line_offsets
[1] = edit_buffer_get_current_bol (&edit
->buffer
);
648 edit
->line_numbers
[2] = edit
->buffer
.lines
;
649 edit
->line_offsets
[2] = edit_buffer_get_bol (&edit
->buffer
, edit
->buffer
.size
);
650 edit
->caches_valid
= TRUE
;
652 if (line
>= edit
->buffer
.lines
)
653 return edit
->line_offsets
[2];
656 /* find the closest known point */
657 for (i
= 0; i
< N_LINE_CACHES
; i
++)
661 n
= abs (edit
->line_numbers
[i
] - line
);
669 return edit
->line_offsets
[j
]; /* know the offset exactly */
670 if (m
== 1 && j
>= 3)
671 i
= j
; /* one line different - caller might be looping, so stay in this cache */
673 i
= 3 + (rand () % (N_LINE_CACHES
- 3));
674 if (line
> edit
->line_numbers
[j
])
675 edit
->line_offsets
[i
] =
676 edit_buffer_move_forward (&edit
->buffer
, edit
->line_offsets
[j
],
677 line
- edit
->line_numbers
[j
], 0);
679 edit
->line_offsets
[i
] =
680 edit_buffer_move_backward (&edit
->buffer
, edit
->line_offsets
[j
],
681 edit
->line_numbers
[j
] - line
);
682 edit
->line_numbers
[i
] = line
;
683 return edit
->line_offsets
[i
];
686 /* --------------------------------------------------------------------------------------------- */
687 /** moves up until a blank line is reached, or until just
688 before a non-blank line is reached */
691 edit_move_up_paragraph (WEdit
* edit
, gboolean do_scroll
)
695 if (edit
->buffer
.curs_line
> 1)
697 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
699 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
700 if (edit_line_is_blank (edit
, i
))
703 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
- 1))
705 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
706 if (!edit_line_is_blank (edit
, i
))
714 for (i
= edit
->buffer
.curs_line
- 1; i
!= 0; i
--)
715 if (edit_line_is_blank (edit
, i
))
720 edit_move_up (edit
, edit
->buffer
.curs_line
- i
, do_scroll
);
723 /* --------------------------------------------------------------------------------------------- */
724 /** moves down until a blank line is reached, or until just
725 before a non-blank line is reached */
728 edit_move_down_paragraph (WEdit
* edit
, gboolean do_scroll
)
732 if (edit
->buffer
.curs_line
>= edit
->buffer
.lines
- 1)
733 i
= edit
->buffer
.lines
;
734 else if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
736 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
737 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
740 else if (edit_line_is_blank (edit
, edit
->buffer
.curs_line
+ 1))
742 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
743 if (!edit_line_is_blank (edit
, i
) || i
> edit
->buffer
.lines
)
751 for (i
= edit
->buffer
.curs_line
+ 1; i
!= 0; i
++)
752 if (edit_line_is_blank (edit
, i
) || i
>= edit
->buffer
.lines
)
755 edit_move_down (edit
, i
- edit
->buffer
.curs_line
, do_scroll
);
758 /* --------------------------------------------------------------------------------------------- */
761 edit_begin_page (WEdit
* edit
)
763 edit_update_curs_row (edit
);
764 edit_move_up (edit
, edit
->curs_row
, 0);
767 /* --------------------------------------------------------------------------------------------- */
770 edit_end_page (WEdit
* edit
)
772 edit_update_curs_row (edit
);
773 edit_move_down (edit
, WIDGET (edit
)->lines
- edit
->curs_row
- 1, 0);
777 /* --------------------------------------------------------------------------------------------- */
778 /** goto beginning of text */
781 edit_move_to_top (WEdit
* edit
)
783 if (edit
->buffer
.curs_line
!= 0)
785 edit_cursor_move (edit
, -edit
->buffer
.curs1
);
786 edit_move_to_prev_col (edit
, 0);
787 edit
->force
|= REDRAW_PAGE
;
788 edit
->search_start
= 0;
789 edit_update_curs_row (edit
);
793 /* --------------------------------------------------------------------------------------------- */
794 /** goto end of text */
797 edit_move_to_bottom (WEdit
* edit
)
799 if (edit
->buffer
.curs_line
< edit
->buffer
.lines
)
801 edit_move_down (edit
, edit
->buffer
.lines
- edit
->curs_row
, 0);
802 edit
->start_display
= edit
->buffer
.size
;
803 edit
->start_line
= edit
->buffer
.lines
;
804 edit_scroll_upward (edit
, WIDGET (edit
)->lines
- 1);
805 edit
->force
|= REDRAW_PAGE
;
809 /* --------------------------------------------------------------------------------------------- */
810 /** goto beginning of line */
813 edit_cursor_to_bol (WEdit
* edit
)
815 edit_cursor_move (edit
, edit_buffer_get_current_bol (&edit
->buffer
) - edit
->buffer
.curs1
);
816 edit
->search_start
= edit
->buffer
.curs1
;
817 edit
->prev_col
= edit_get_col (edit
);
821 /* --------------------------------------------------------------------------------------------- */
822 /** goto end of line */
825 edit_cursor_to_eol (WEdit
* edit
)
827 edit_cursor_move (edit
, edit_buffer_get_current_eol (&edit
->buffer
) - edit
->buffer
.curs1
);
828 edit
->search_start
= edit
->buffer
.curs1
;
829 edit
->prev_col
= edit_get_col (edit
);
833 /* --------------------------------------------------------------------------------------------- */
840 const char option_chars_move_whole_word
[] =
841 "!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
847 if (*option_chars_move_whole_word
== '!')
851 if (g_ascii_isupper ((gchar
) c
))
853 else if (g_ascii_islower ((gchar
) c
))
855 else if (g_ascii_isalpha (c
))
857 else if (isdigit (c
))
859 else if (isspace (c
))
861 q
= strchr (option_chars_move_whole_word
, c
);
866 for (x
= 1, p
= option_chars_move_whole_word
; p
< q
; p
++)
871 while ((q
= strchr (q
+ 1, c
)));
875 /* --------------------------------------------------------------------------------------------- */
878 edit_left_word_move (WEdit
* edit
, int s
)
884 if (edit
->column_highlight
885 && edit
->mark1
!= edit
->mark2
886 && edit
->over_col
== 0
887 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
889 edit_cursor_move (edit
, -1);
890 if (edit
->buffer
.curs1
== 0)
892 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
893 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
894 if (c1
== '\n' || c2
== '\n')
896 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
898 if (isspace (c1
) && !isspace (c2
))
900 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
905 /* --------------------------------------------------------------------------------------------- */
908 edit_left_word_move_cmd (WEdit
* edit
)
910 edit_left_word_move (edit
, 0);
911 edit
->force
|= REDRAW_PAGE
;
914 /* --------------------------------------------------------------------------------------------- */
917 edit_right_word_move (WEdit
* edit
, int s
)
923 if (edit
->column_highlight
924 && edit
->mark1
!= edit
->mark2
925 && edit
->over_col
== 0
926 && edit
->buffer
.curs1
== edit_buffer_get_current_eol (&edit
->buffer
))
928 edit_cursor_move (edit
, 1);
929 if (edit
->buffer
.curs1
>= edit
->buffer
.size
)
931 c1
= edit_buffer_get_previous_byte (&edit
->buffer
);
932 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
933 if (c1
== '\n' || c2
== '\n')
935 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
937 if (isspace (c1
) && !isspace (c2
))
939 if (s
!= 0 && !isspace (c1
) && isspace (c2
))
944 /* --------------------------------------------------------------------------------------------- */
947 edit_right_word_move_cmd (WEdit
* edit
)
949 edit_right_word_move (edit
, 0);
950 edit
->force
|= REDRAW_PAGE
;
953 /* --------------------------------------------------------------------------------------------- */
956 edit_right_char_move_cmd (WEdit
* edit
)
964 c
= edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
970 c
= edit_buffer_get_current_byte (&edit
->buffer
);
972 if (option_cursor_beyond_eol
&& c
== '\n')
975 edit_cursor_move (edit
, cw
);
978 /* --------------------------------------------------------------------------------------------- */
981 edit_left_char_move_cmd (WEdit
* edit
)
985 if (edit
->column_highlight
986 && option_cursor_beyond_eol
987 && edit
->mark1
!= edit
->mark2
988 && edit
->over_col
== 0 && edit
->buffer
.curs1
== edit_buffer_get_current_bol (&edit
->buffer
))
993 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
999 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
1002 edit_cursor_move (edit
, -cw
);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Up or down cursor moving.
1007 direction = TRUE - move up
1012 edit_move_updown (WEdit
* edit
, long lines
, gboolean do_scroll
, gboolean direction
)
1015 long l
= direction
? edit
->buffer
.curs_line
: edit
->buffer
.lines
- edit
->buffer
.curs_line
;
1024 edit
->force
|= REDRAW_PAGE
;
1028 edit_scroll_upward (edit
, lines
);
1030 edit_scroll_downward (edit
, lines
);
1032 p
= edit_buffer_get_current_bol (&edit
->buffer
);
1033 p
= direction
? edit_buffer_move_backward (&edit
->buffer
, p
, lines
) :
1034 edit_buffer_move_forward (&edit
->buffer
, p
, lines
, 0);
1035 edit_cursor_move (edit
, p
- edit
->buffer
.curs1
);
1036 edit_move_to_prev_col (edit
, p
);
1039 /* search start of current multibyte char (like CJK) */
1040 if (edit
->buffer
.curs1
> 0 && edit
->buffer
.curs1
+ 1 < edit
->buffer
.size
1041 && edit_buffer_get_current_byte (&edit
->buffer
) >= 256)
1043 edit_right_char_move_cmd (edit
);
1044 edit_left_char_move_cmd (edit
);
1048 edit
->search_start
= edit
->buffer
.curs1
;
1049 edit
->found_len
= 0;
1052 /* --------------------------------------------------------------------------------------------- */
1055 edit_right_delete_word (WEdit
* edit
)
1057 while (edit
->buffer
.curs1
< edit
->buffer
.size
)
1061 c1
= edit_delete (edit
, TRUE
);
1062 c2
= edit_buffer_get_current_byte (&edit
->buffer
);
1063 if (c1
== '\n' || c2
== '\n')
1065 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1067 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1072 /* --------------------------------------------------------------------------------------------- */
1075 edit_left_delete_word (WEdit
* edit
)
1077 while (edit
->buffer
.curs1
> 0)
1081 c1
= edit_backspace (edit
, TRUE
);
1082 c2
= edit_buffer_get_previous_byte (&edit
->buffer
);
1083 if (c1
== '\n' || c2
== '\n')
1085 if ((isspace (c1
) == 0) != (isspace (c2
) == 0))
1087 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
1092 /* --------------------------------------------------------------------------------------------- */
1094 the start column position is not recorded, and hence does not
1095 undo as it happed. But who would notice.
1099 edit_do_undo (WEdit
* edit
)
1104 edit
->undo_stack_disable
= 1; /* don't record undo's onto undo stack! */
1106 while ((ac
= edit_pop_undo_action (edit
)) < KEY_PRESS
)
1113 edit_cursor_move (edit
, 1);
1116 edit_cursor_move (edit
, -1);
1120 edit_backspace (edit
, TRUE
);
1124 edit_delete (edit
, TRUE
);
1127 edit
->column_highlight
= 1;
1130 edit
->column_highlight
= 0;
1133 if (ac
>= 256 && ac
< 512)
1134 edit_insert_ahead (edit
, ac
- 256);
1135 if (ac
>= 0 && ac
< 256)
1136 edit_insert (edit
, ac
);
1138 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1140 edit
->mark1
= ac
- MARK_1
;
1142 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1145 if (ac
>= MARK_2
- 2 && ac
< MARK_CURS
- 2)
1147 edit
->mark2
= ac
- MARK_2
;
1149 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1152 else if (ac
>= MARK_CURS
- 2 && ac
< KEY_PRESS
)
1154 edit
->end_mark_curs
= ac
- MARK_CURS
;
1157 edit
->force
|= REDRAW_PAGE
; /* more than one pop usually means something big */
1160 if (edit
->start_display
> ac
- KEY_PRESS
)
1163 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1164 edit
->force
|= REDRAW_PAGE
;
1166 else if (edit
->start_display
< ac
- KEY_PRESS
)
1169 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1170 edit
->force
|= REDRAW_PAGE
;
1172 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1173 edit_update_curs_row (edit
);
1176 edit
->undo_stack_disable
= 0;
1179 /* --------------------------------------------------------------------------------------------- */
1182 edit_do_redo (WEdit
* edit
)
1187 if (edit
->redo_stack_reset
)
1191 while ((ac
= edit_pop_redo_action (edit
)) < KEY_PRESS
)
1198 edit_cursor_move (edit
, 1);
1201 edit_cursor_move (edit
, -1);
1204 edit_backspace (edit
, TRUE
);
1207 edit_delete (edit
, TRUE
);
1210 edit
->column_highlight
= 1;
1213 edit
->column_highlight
= 0;
1216 if (ac
>= 256 && ac
< 512)
1217 edit_insert_ahead (edit
, ac
- 256);
1218 if (ac
>= 0 && ac
< 256)
1219 edit_insert (edit
, ac
);
1221 if (ac
>= MARK_1
- 2 && ac
< MARK_2
- 2)
1223 edit
->mark1
= ac
- MARK_1
;
1225 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark1
),
1228 else if (ac
>= MARK_2
- 2 && ac
< KEY_PRESS
)
1230 edit
->mark2
= ac
- MARK_2
;
1232 (long) edit_move_forward3 (edit
, edit_buffer_get_bol (&edit
->buffer
, edit
->mark2
),
1235 /* more than one pop usually means something big */
1237 edit
->force
|= REDRAW_PAGE
;
1240 if (edit
->start_display
> ac
- KEY_PRESS
)
1243 edit_buffer_count_lines (&edit
->buffer
, ac
- KEY_PRESS
, edit
->start_display
);
1244 edit
->force
|= REDRAW_PAGE
;
1246 else if (edit
->start_display
< ac
- KEY_PRESS
)
1249 edit_buffer_count_lines (&edit
->buffer
, edit
->start_display
, ac
- KEY_PRESS
);
1250 edit
->force
|= REDRAW_PAGE
;
1252 edit
->start_display
= ac
- KEY_PRESS
; /* see push and pop above */
1253 edit_update_curs_row (edit
);
1259 /* --------------------------------------------------------------------------------------------- */
1262 edit_group_undo (WEdit
* edit
)
1264 long ac
= KEY_PRESS
;
1265 long cur_ac
= KEY_PRESS
;
1266 while (ac
!= STACK_BOTTOM
&& ac
== cur_ac
)
1268 cur_ac
= get_prev_undo_action (edit
);
1269 edit_do_undo (edit
);
1270 ac
= get_prev_undo_action (edit
);
1271 /* exit from cycle if option_group_undo is not set,
1272 * and make single UNDO operation
1274 if (!option_group_undo
)
1279 /* --------------------------------------------------------------------------------------------- */
1282 edit_delete_to_line_end (WEdit
* edit
)
1284 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs2
!= 0)
1285 edit_delete (edit
, TRUE
);
1288 /* --------------------------------------------------------------------------------------------- */
1291 edit_delete_to_line_begin (WEdit
* edit
)
1293 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
!= 0)
1294 edit_backspace (edit
, TRUE
);
1297 /* --------------------------------------------------------------------------------------------- */
1300 is_aligned_on_a_tab (WEdit
* edit
)
1304 edit_update_curs_col (edit
);
1305 curs_col
= edit
->curs_col
% (TAB_SIZE
* space_width
);
1306 return (curs_col
== 0 || curs_col
== (HALF_TAB_SIZE
* space_width
));
1309 /* --------------------------------------------------------------------------------------------- */
1312 right_of_four_spaces (WEdit
* edit
)
1316 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1317 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- i
);
1319 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1322 /* --------------------------------------------------------------------------------------------- */
1325 left_of_four_spaces (WEdit
* edit
)
1329 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
1330 ch
|= edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
+ i
);
1332 return (ch
== ' ' && is_aligned_on_a_tab (edit
));
1335 /* --------------------------------------------------------------------------------------------- */
1338 edit_auto_indent (WEdit
* edit
)
1342 p
= edit
->buffer
.curs1
;
1343 /* use the previous line as a template */
1344 p
= edit_buffer_move_backward (&edit
->buffer
, p
, 1);
1345 /* copy the leading whitespace of the line */
1347 { /* no range check - the line _is_ \n-terminated */
1350 c
= edit_buffer_get_byte (&edit
->buffer
, p
++);
1351 if (c
!= ' ' && c
!= '\t')
1353 edit_insert (edit
, c
);
1357 /* --------------------------------------------------------------------------------------------- */
1360 edit_double_newline (WEdit
* edit
)
1362 edit_insert (edit
, '\n');
1363 if (edit_buffer_get_current_byte (&edit
->buffer
) == '\n'
1364 || edit_buffer_get_byte (&edit
->buffer
, edit
->buffer
.curs1
- 2) == '\n')
1366 edit
->force
|= REDRAW_PAGE
;
1367 edit_insert (edit
, '\n');
1370 /* --------------------------------------------------------------------------------------------- */
1373 insert_spaces_tab (WEdit
* edit
, gboolean half
)
1377 edit_update_curs_col (edit
);
1378 i
= option_tab_spacing
* space_width
;
1383 i
= ((edit
->curs_col
/ i
) + 1) * i
- edit
->curs_col
;
1386 edit_insert (edit
, ' ');
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_tab_cmd (WEdit
* edit
)
1397 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
1399 /* insert a half tab (usually four spaces) unless there is a
1400 half tab already behind, then delete it and insert a
1402 if (option_fill_tabs_with_spaces
|| !right_of_four_spaces (edit
))
1403 insert_spaces_tab (edit
, TRUE
);
1408 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
1409 edit_backspace (edit
, TRUE
);
1410 edit_insert (edit
, '\t');
1413 else if (option_fill_tabs_with_spaces
)
1414 insert_spaces_tab (edit
, FALSE
);
1416 edit_insert (edit
, '\t');
1419 /* --------------------------------------------------------------------------------------------- */
1422 check_and_wrap_line (WEdit
* edit
)
1426 if (!option_typewriter_wrap
)
1428 edit_update_curs_col (edit
);
1429 if (edit
->curs_col
< option_word_wrap_line_length
)
1431 curs
= edit
->buffer
.curs1
;
1437 c
= edit_buffer_get_byte (&edit
->buffer
, curs
);
1438 if (c
== '\n' || curs
<= 0)
1440 edit_insert (edit
, '\n');
1443 if (c
== ' ' || c
== '\t')
1445 off_t current
= edit
->buffer
.curs1
;
1446 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
+ 1);
1447 edit_insert (edit
, '\n');
1448 edit_cursor_move (edit
, current
- edit
->buffer
.curs1
+ 1);
1454 /* --------------------------------------------------------------------------------------------- */
1455 /** this find the matching bracket in either direction, and sets edit->bracket
1457 * @param edit editor object
1458 * @param in_screen seach only on the current screen
1459 * @param furthest_bracket_search count of the bytes for search
1461 * @return position of the found bracket (-1 if no match)
1465 edit_get_bracket (WEdit
* edit
, gboolean in_screen
, unsigned long furthest_bracket_search
)
1467 const char *const b
= "{}{[][()(", *p
;
1468 int i
= 1, inc
= -1, c
, d
, n
= 0;
1469 unsigned long j
= 0;
1472 edit_update_curs_row (edit
);
1473 c
= edit_buffer_get_current_byte (&edit
->buffer
);
1476 if (!furthest_bracket_search
)
1477 furthest_bracket_search
--;
1478 /* not on a bracket at all */
1481 /* the matching bracket */
1483 /* going left or right? */
1484 if (strchr ("{[(", c
))
1486 for (q
= edit
->buffer
.curs1
+ inc
;; q
+= inc
)
1490 /* out of buffer? */
1491 if (q
>= edit
->buffer
.size
|| q
< 0)
1493 a
= edit_buffer_get_byte (&edit
->buffer
, q
);
1494 /* don't want to eat CPU */
1495 if (j
++ > furthest_bracket_search
)
1497 /* out of screen? */
1500 if (q
< edit
->start_display
)
1502 /* count lines if searching downward */
1503 if (inc
> 0 && a
== '\n')
1504 if (n
++ >= WIDGET (edit
)->lines
- edit
->curs_row
) /* out of screen */
1507 /* count bracket depth */
1508 i
+= (a
== c
) - (a
== d
);
1509 /* return if bracket depth is zero */
1517 /* --------------------------------------------------------------------------------------------- */
1520 edit_goto_matching_bracket (WEdit
* edit
)
1524 q
= edit_get_bracket (edit
, 0, 0);
1527 edit
->bracket
= edit
->buffer
.curs1
;
1528 edit
->force
|= REDRAW_PAGE
;
1529 edit_cursor_move (edit
, q
- edit
->buffer
.curs1
);
1533 /* --------------------------------------------------------------------------------------------- */
1536 edit_move_block_to_right (WEdit
* edit
)
1538 off_t start_mark
, end_mark
;
1539 long cur_bol
, start_bol
;
1541 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1544 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1545 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1549 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1550 if (!edit_line_is_blank (edit
, edit
->buffer
.curs_line
))
1552 if (option_fill_tabs_with_spaces
)
1553 insert_spaces_tab (edit
, option_fake_half_tabs
);
1555 edit_insert (edit
, '\t');
1556 edit_cursor_move (edit
,
1557 edit_buffer_get_bol (&edit
->buffer
, cur_bol
) - edit
->buffer
.curs1
);
1563 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1565 while (cur_bol
>= start_bol
);
1567 edit
->force
|= REDRAW_PAGE
;
1570 /* --------------------------------------------------------------------------------------------- */
1573 edit_move_block_to_left (WEdit
* edit
)
1575 off_t start_mark
, end_mark
;
1576 off_t cur_bol
, start_bol
;
1579 if (!eval_marks (edit
, &start_mark
, &end_mark
))
1582 start_bol
= edit_buffer_get_bol (&edit
->buffer
, start_mark
);
1583 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, end_mark
- 1);
1590 edit_cursor_move (edit
, cur_bol
- edit
->buffer
.curs1
);
1592 if (option_fake_half_tabs
)
1593 del_tab_width
= HALF_TAB_SIZE
;
1595 del_tab_width
= option_tab_spacing
;
1597 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1598 if (next_char
== '\t')
1599 edit_delete (edit
, TRUE
);
1600 else if (next_char
== ' ')
1601 for (i
= 1; i
<= del_tab_width
; i
++)
1603 if (next_char
== ' ')
1604 edit_delete (edit
, TRUE
);
1605 next_char
= edit_buffer_get_current_byte (&edit
->buffer
);
1611 cur_bol
= edit_buffer_get_bol (&edit
->buffer
, cur_bol
- 1);
1613 while (cur_bol
>= start_bol
);
1615 edit
->force
|= REDRAW_PAGE
;
1618 /* --------------------------------------------------------------------------------------------- */
1620 * prints at the cursor
1621 * @return number of chars printed
1625 edit_print_string (WEdit
* e
, const char *s
)
1629 while (s
[i
] != '\0')
1630 edit_execute_cmd (e
, CK_InsertChar
, (unsigned char) s
[i
++]);
1631 e
->force
|= REDRAW_COMPLETELY
;
1632 edit_update_screen (e
);
1636 /* --------------------------------------------------------------------------------------------- */
1639 edit_insert_column_from_file (WEdit
* edit
, int file
, off_t
* start_pos
, off_t
* end_pos
,
1640 long *col1
, long *col2
)
1644 off_t blocklen
= -1, width
= 0;
1645 unsigned char *data
;
1647 cursor
= edit
->buffer
.curs1
;
1648 col
= edit_get_col (edit
);
1649 data
= g_malloc0 (TEMP_BUF_LEN
);
1651 while ((blocklen
= mc_read (file
, (char *) data
, TEMP_BUF_LEN
)) > 0)
1656 pn
= strchr ((char *) data
, '\n');
1657 width
= pn
== NULL
? blocklen
: pn
- (char *) data
;
1659 for (i
= 0; i
< blocklen
; i
++)
1661 if (data
[i
] != '\n')
1662 edit_insert (edit
, data
[i
]);
1664 { /* fill in and move to next line */
1668 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
1669 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1670 edit_insert (edit
, ' ');
1672 for (p
= edit
->buffer
.curs1
;; p
++)
1674 if (p
== edit
->buffer
.size
)
1676 edit_cursor_move (edit
, edit
->buffer
.size
- edit
->buffer
.curs1
);
1677 edit_insert_ahead (edit
, '\n');
1681 if (edit_buffer_get_byte (&edit
->buffer
, p
) == '\n')
1688 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->buffer
.curs1
);
1690 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1691 edit_insert (edit
, ' ');
1696 *col2
= col
+ width
;
1697 *start_pos
= cursor
;
1698 *end_pos
= edit
->buffer
.curs1
;
1699 edit_cursor_move (edit
, cursor
- edit
->buffer
.curs1
);
1705 /* --------------------------------------------------------------------------------------------- */
1706 /*** public functions ****************************************************************************/
1707 /* --------------------------------------------------------------------------------------------- */
1709 /** User edit menu, like user menu (F2) but only in editor. */
1712 user_menu (WEdit
* edit
, const char *menu_file
, int selected_entry
)
1717 off_t start_mark
, end_mark
;
1719 vfs_path_t
*block_file_vpath
;
1721 block_file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
1722 block_file_vpath
= vfs_path_from_str (block_file
);
1723 curs
= edit
->buffer
.curs1
;
1724 nomark
= !eval_marks (edit
, &start_mark
, &end_mark
);
1726 edit_save_block (edit
, block_file
, start_mark
, end_mark
);
1728 /* run shell scripts from menu */
1729 if (user_menu_cmd (edit
, menu_file
, selected_entry
)
1730 && (mc_stat (block_file_vpath
, &status
) == 0) && (status
.st_size
!= 0))
1735 /* i.e. we have marked block */
1737 rc
= edit_block_delete_cmd (edit
);
1743 ins_len
= edit_insert_file (edit
, block_file_vpath
);
1744 if (!nomark
&& ins_len
> 0)
1745 edit_set_markers (edit
, start_mark
, start_mark
+ ins_len
, 0, 0);
1747 /* truncate block file */
1748 fd
= fopen (block_file
, "w");
1752 g_free (block_file
);
1753 vfs_path_free (block_file_vpath
);
1755 edit_cursor_move (edit
, curs
- edit
->buffer
.curs1
);
1756 edit
->force
|= REDRAW_PAGE
;
1757 widget_redraw (WIDGET (edit
));
1760 /* --------------------------------------------------------------------------------------------- */
1763 edit_get_write_filter (const vfs_path_t
* write_name_vpath
, const vfs_path_t
* filename_vpath
)
1766 char *p
, *writename
;
1767 const vfs_path_element_t
*path_element
;
1769 i
= edit_find_filter (filename_vpath
);
1773 path_element
= vfs_path_get_by_index (write_name_vpath
, -1);
1774 writename
= name_quote (path_element
->path
, 0);
1775 p
= g_strdup_printf (all_filters
[i
].write
, writename
);
1780 /* --------------------------------------------------------------------------------------------- */
1782 * @param edit editor object
1783 * @param f value of stream file
1784 * @return the length of the file
1788 edit_write_stream (WEdit
* edit
, FILE * f
)
1792 if (edit
->lb
== LB_ASIS
)
1794 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1795 if (fputc (edit_buffer_get_byte (&edit
->buffer
, i
), f
) < 0)
1800 /* change line breaks */
1801 for (i
= 0; i
< edit
->buffer
.size
; i
++)
1805 c
= edit_buffer_get_byte (&edit
->buffer
, i
);
1806 if (!(c
== '\n' || c
== '\r'))
1808 /* not line break */
1809 if (fputc (c
, f
) < 0)
1813 { /* (c == '\n' || c == '\r') */
1816 c1
= edit_buffer_get_byte (&edit
->buffer
, i
+ 1); /* next char */
1820 case LB_UNIX
: /* replace "\r\n" or '\r' to '\n' */
1821 /* put one line break unconditionally */
1822 if (fputc ('\n', f
) < 0)
1825 i
++; /* 2 chars are processed */
1827 if (c
== '\r' && c1
== '\n')
1828 /* Windows line break; go to the next char */
1831 if (c
== '\r' && c1
== '\r')
1833 /* two Macintosh line breaks; put second line break */
1834 if (fputc ('\n', f
) < 0)
1839 if (fputc (c1
, f
) < 0)
1843 case LB_WIN
: /* replace '\n' or '\r' to "\r\n" */
1844 /* put one line break unconditionally */
1845 if (fputc ('\r', f
) < 0 || fputc ('\n', f
) < 0)
1848 if (c
== '\r' && c1
== '\n')
1849 /* Windows line break; go to the next char */
1853 case LB_MAC
: /* replace "\r\n" or '\n' to '\r' */
1854 /* put one line break unconditionally */
1855 if (fputc ('\r', f
) < 0)
1858 i
++; /* 2 chars are processed */
1860 if (c
== '\r' && c1
== '\n')
1861 /* Windows line break; go to the next char */
1864 if (c
== '\n' && c1
== '\n')
1866 /* two Windows line breaks; put second line break */
1867 if (fputc ('\r', f
) < 0)
1872 if (fputc (c1
, f
) < 0)
1875 case LB_ASIS
: /* default without changes */
1881 return edit
->buffer
.size
;
1884 /* --------------------------------------------------------------------------------------------- */
1887 is_break_char (char c
)
1889 return (isspace (c
) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c
));
1892 /* --------------------------------------------------------------------------------------------- */
1893 /** inserts a file at the cursor, returns count of inserted bytes on success */
1896 edit_insert_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
1902 p
= edit_get_filter (filename_vpath
);
1903 current
= edit
->buffer
.curs1
;
1909 f
= (FILE *) popen (p
, "r");
1912 edit_insert_stream (edit
, f
);
1914 /* Place cursor at the end of text selection */
1915 if (!option_cursor_after_inserted_block
)
1917 ins_len
= edit
->buffer
.curs1
- current
;
1918 edit_cursor_move (edit
, -ins_len
);
1924 errmsg
= g_strdup_printf (_("Error reading from pipe: %s"), p
);
1925 edit_error_dialog (_("Error"), errmsg
);
1934 errmsg
= g_strdup_printf (_("Cannot open pipe for reading: %s"), p
);
1935 edit_error_dialog (_("Error"), errmsg
);
1945 int vertical_insertion
= 0;
1948 file
= mc_open (filename_vpath
, O_RDONLY
| O_BINARY
);
1952 buf
= g_malloc0 (TEMP_BUF_LEN
);
1953 blocklen
= mc_read (file
, buf
, sizeof (VERTICAL_MAGIC
));
1956 /* if contain signature VERTICAL_MAGIC then it vertical block */
1957 if (memcmp (buf
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
)) == 0)
1958 vertical_insertion
= 1;
1960 mc_lseek (file
, 0, SEEK_SET
);
1963 if (vertical_insertion
)
1968 blocklen
= edit_insert_column_from_file (edit
, file
, &mark1
, &mark2
, &c1
, &c2
);
1969 edit_set_markers (edit
, edit
->buffer
.curs1
, mark2
, c1
, c2
);
1971 /* highlight inserted text then not persistent blocks */
1972 if (!option_persistent_selections
&& edit
->modified
)
1974 if (!edit
->column_highlight
)
1975 edit_push_undo_action (edit
, COLUMN_OFF
);
1976 edit
->column_highlight
= 1;
1983 while ((blocklen
= mc_read (file
, (char *) buf
, TEMP_BUF_LEN
)) > 0)
1985 for (i
= 0; i
< blocklen
; i
++)
1986 edit_insert (edit
, buf
[i
]);
1988 /* highlight inserted text then not persistent blocks */
1989 if (!option_persistent_selections
&& edit
->modified
)
1991 edit_set_markers (edit
, edit
->buffer
.curs1
, current
, 0, 0);
1992 if (edit
->column_highlight
)
1993 edit_push_undo_action (edit
, COLUMN_ON
);
1994 edit
->column_highlight
= 0;
1997 /* Place cursor at the end of text selection */
1998 if (!option_cursor_after_inserted_block
)
2000 ins_len
= edit
->buffer
.curs1
- current
;
2001 edit_cursor_move (edit
, -ins_len
);
2005 edit
->force
|= REDRAW_PAGE
;
2015 /* --------------------------------------------------------------------------------------------- */
2017 * Fill in the edit structure. Return NULL on failure. Pass edit as
2018 * NULL to allocate a new structure.
2020 * If line is 0, try to restore saved position. Otherwise put the
2021 * cursor on that line and show it in the middle of the screen.
2025 edit_init (WEdit
* edit
, int y
, int x
, int lines
, int cols
, const vfs_path_t
* filename_vpath
,
2028 gboolean to_free
= FALSE
;
2030 option_auto_syntax
= 1; /* Resetting to auto on every invokation */
2031 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
2035 /* save some widget parameters */
2036 gboolean fullscreen
= edit
->fullscreen
;
2037 int y_prev
= edit
->y_prev
;
2038 int x_prev
= edit
->x_prev
;
2039 int lines_prev
= edit
->lines_prev
;
2040 int cols_prev
= edit
->cols_prev
;
2042 edit_purge_widget (edit
);
2044 /* restore saved parameters */
2045 edit
->fullscreen
= fullscreen
;
2046 edit
->y_prev
= y_prev
;
2047 edit
->x_prev
= x_prev
;
2048 edit
->lines_prev
= lines_prev
;
2049 edit
->cols_prev
= cols_prev
;
2053 edit
= g_malloc0 (sizeof (WEdit
));
2056 widget_init (WIDGET (edit
), y
, x
, lines
, cols
, NULL
, NULL
);
2057 edit
->fullscreen
= TRUE
;
2058 edit_save_size (edit
);
2061 edit
->drag_state
= MCEDIT_DRAG_NORMAL
;
2063 edit
->stat1
.st_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
2064 edit
->stat1
.st_uid
= getuid ();
2065 edit
->stat1
.st_gid
= getgid ();
2066 edit
->stat1
.st_mtime
= 0;
2070 edit
->last_bracket
= -1;
2071 edit
->force
|= REDRAW_PAGE
;
2073 /* set file name before load file */
2074 edit_set_filename (edit
, filename_vpath
);
2076 edit
->undo_stack_size
= START_STACK_SIZE
;
2077 edit
->undo_stack_size_mask
= START_STACK_SIZE
- 1;
2078 edit
->undo_stack
= g_malloc0 ((edit
->undo_stack_size
+ 10) * sizeof (long));
2080 edit
->redo_stack_size
= START_STACK_SIZE
;
2081 edit
->redo_stack_size_mask
= START_STACK_SIZE
- 1;
2082 edit
->redo_stack
= g_malloc0 ((edit
->redo_stack_size
+ 10) * sizeof (long));
2086 edit
->converter
= str_cnv_from_term
;
2087 edit_set_codeset (edit
);
2090 if (!edit_load_file (edit
))
2092 /* edit_load_file already gives an error message */
2098 edit
->loading_done
= 1;
2101 edit_load_syntax (edit
, NULL
, NULL
);
2102 edit_get_syntax_color (edit
, -1);
2104 /* load saved cursor position */
2105 if ((line
== 0) && option_save_position
)
2106 edit_load_position (edit
);
2111 edit_move_display (edit
, line
- 1);
2112 edit_move_to_line (edit
, line
- 1);
2115 edit_load_macro_cmd (edit
);
2120 /* --------------------------------------------------------------------------------------------- */
2122 /** Clear the edit struct, freeing everything in it. Return TRUE on success */
2124 edit_clean (WEdit
* edit
)
2129 /* a stale lock, remove it */
2131 edit
->locked
= unlock_file (edit
->filename_vpath
);
2133 /* save cursor position */
2134 if (option_save_position
)
2135 edit_save_position (edit
);
2136 else if (edit
->serialized_bookmarks
!= NULL
)
2137 edit
->serialized_bookmarks
= (GArray
*) g_array_free (edit
->serialized_bookmarks
, TRUE
);
2139 /* File specified on the mcedit command line and never saved */
2140 if (edit
->delete_file
)
2141 unlink (vfs_path_get_last_path_str (edit
->filename_vpath
));
2143 edit_free_syntax_rules (edit
);
2144 book_mark_flush (edit
, -1);
2146 edit_buffer_clean (&edit
->buffer
);
2148 g_free (edit
->undo_stack
);
2149 g_free (edit
->redo_stack
);
2150 vfs_path_free (edit
->filename_vpath
);
2151 vfs_path_free (edit
->dir_vpath
);
2152 mc_search_free (edit
->search
);
2153 edit
->search
= NULL
;
2156 if (edit
->converter
!= str_cnv_from_term
)
2157 str_close_conv (edit
->converter
);
2160 edit_purge_widget (edit
);
2165 /* --------------------------------------------------------------------------------------------- */
2168 * Load a new file into the editor and set line. If it fails, preserve the old file.
2169 * To do it, allocate a new widget, initialize it and, if the new file
2170 * was loaded, copy the data to the old widget.
2172 * @return TRUE on success, FALSE on failure.
2175 edit_reload_line (WEdit
* edit
, const vfs_path_t
* filename_vpath
, long line
)
2177 Widget
*w
= WIDGET (edit
);
2180 e
= g_malloc0 (sizeof (WEdit
));
2182 /* save some widget parameters */
2183 e
->fullscreen
= edit
->fullscreen
;
2184 e
->y_prev
= edit
->y_prev
;
2185 e
->x_prev
= edit
->x_prev
;
2186 e
->lines_prev
= edit
->lines_prev
;
2187 e
->cols_prev
= edit
->cols_prev
;
2189 if (edit_init (e
, w
->y
, w
->x
, w
->lines
, w
->cols
, filename_vpath
, line
) == NULL
)
2196 memcpy (edit
, e
, sizeof (WEdit
));
2202 /* --------------------------------------------------------------------------------------------- */
2206 edit_set_codeset (WEdit
* edit
)
2211 get_codepage_id (mc_global
.source_codepage
>=
2212 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2217 conv
= str_crt_conv_from (cp_id
);
2218 if (conv
!= INVALID_CONV
)
2220 if (edit
->converter
!= str_cnv_from_term
)
2221 str_close_conv (edit
->converter
);
2222 edit
->converter
= conv
;
2227 edit
->utf8
= str_isutf8 (cp_id
);
2231 /* --------------------------------------------------------------------------------------------- */
2234 * Recording stack for undo:
2235 * The following is an implementation of a compressed stack. Identical
2236 * pushes are recorded by a negative prefix indicating the number of times the
2237 * same char was pushed. This saves space for repeated curs-left or curs-right
2254 * If the stack long int is 0-255 it represents a normal insert (from a backspace),
2255 * 256-512 is an insert ahead (from a delete), If it is betwen 600 and 700 it is one
2256 * of the cursor functions define'd in edit-impl.h. 1000 through 700'000'000 is to
2257 * set edit->mark1 position. 700'000'000 through 1400'000'000 is to set edit->mark2
2260 * The only way the cursor moves or the buffer is changed is through the routines:
2261 * insert, backspace, insert_ahead, delete, and cursor_move.
2262 * These record the reverse undo movements onto the stack each time they are
2265 * Each key press results in a set of actions (insert; delete ...). So each time
2266 * a key is pressed the current position of start_display is pushed as
2267 * KEY_PRESS + start_display. Then for undoing, we pop until we get to a number
2268 * over KEY_PRESS. We then assign this number less KEY_PRESS to start_display. So undo
2269 * tracks scrolling and key actions exactly. (KEY_PRESS is about (2^31) * (2/3) = 1400'000'000)
2273 * @param edit editor object
2274 * @param c code of the action
2278 edit_push_undo_action (WEdit
* edit
, long c
)
2280 unsigned long sp
= edit
->undo_stack_pointer
;
2284 /* first enlarge the stack if necessary */
2285 if (sp
> edit
->undo_stack_size
- 10)
2287 if (option_max_undo
< 256)
2288 option_max_undo
= 256;
2289 if (edit
->undo_stack_size
< (unsigned long) option_max_undo
)
2291 t
= g_realloc (edit
->undo_stack
, (edit
->undo_stack_size
* 2 + 10) * sizeof (long));
2294 edit
->undo_stack
= t
;
2295 edit
->undo_stack_size
<<= 1;
2296 edit
->undo_stack_size_mask
= edit
->undo_stack_size
- 1;
2300 spm1
= (edit
->undo_stack_pointer
- 1) & edit
->undo_stack_size_mask
;
2301 if (edit
->undo_stack_disable
)
2303 edit_push_redo_action (edit
, KEY_PRESS
);
2304 edit_push_redo_action (edit
, c
);
2308 if (edit
->redo_stack_reset
)
2309 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2311 if (edit
->undo_stack_bottom
!= sp
2312 && spm1
!= edit
->undo_stack_bottom
2313 && ((sp
- 2) & edit
->undo_stack_size_mask
) != edit
->undo_stack_bottom
)
2316 if (edit
->undo_stack
[spm1
] < 0)
2318 d
= edit
->undo_stack
[(sp
- 2) & edit
->undo_stack_size_mask
];
2319 if (d
== c
&& edit
->undo_stack
[spm1
] > -1000000000)
2321 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2322 edit
->undo_stack
[spm1
]--;
2328 d
= edit
->undo_stack
[spm1
];
2332 return; /* --> no need to push multiple do-nothings */
2333 edit
->undo_stack
[sp
] = -2;
2338 edit
->undo_stack
[sp
] = c
;
2341 edit
->undo_stack_pointer
= (edit
->undo_stack_pointer
+ 1) & edit
->undo_stack_size_mask
;
2343 /* if the sp wraps round and catches the undo_stack_bottom then erase
2344 * the first set of actions on the stack to make space - by moving
2345 * undo_stack_bottom forward one "key press" */
2346 c
= (edit
->undo_stack_pointer
+ 2) & edit
->undo_stack_size_mask
;
2347 if ((unsigned long) c
== edit
->undo_stack_bottom
||
2348 (((unsigned long) c
+ 1) & edit
->undo_stack_size_mask
) == edit
->undo_stack_bottom
)
2351 edit
->undo_stack_bottom
= (edit
->undo_stack_bottom
+ 1) & edit
->undo_stack_size_mask
;
2353 while (edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
2354 && edit
->undo_stack_bottom
!= edit
->undo_stack_pointer
);
2356 /*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: */
2357 if (edit
->undo_stack_pointer
!= edit
->undo_stack_bottom
2358 && edit
->undo_stack
[edit
->undo_stack_bottom
] < KEY_PRESS
)
2360 edit
->undo_stack_bottom
= edit
->undo_stack_pointer
= 0;
2364 /* --------------------------------------------------------------------------------------------- */
2367 edit_push_redo_action (WEdit
* edit
, long c
)
2369 unsigned long sp
= edit
->redo_stack_pointer
;
2372 /* first enlarge the stack if necessary */
2373 if (sp
> edit
->redo_stack_size
- 10)
2375 if (option_max_undo
< 256)
2376 option_max_undo
= 256;
2377 if (edit
->redo_stack_size
< (unsigned long) option_max_undo
)
2379 t
= g_realloc (edit
->redo_stack
, (edit
->redo_stack_size
* 2 + 10) * sizeof (long));
2382 edit
->redo_stack
= t
;
2383 edit
->redo_stack_size
<<= 1;
2384 edit
->redo_stack_size_mask
= edit
->redo_stack_size
- 1;
2388 spm1
= (edit
->redo_stack_pointer
- 1) & edit
->redo_stack_size_mask
;
2390 if (edit
->redo_stack_bottom
!= sp
2391 && spm1
!= edit
->redo_stack_bottom
2392 && ((sp
- 2) & edit
->redo_stack_size_mask
) != edit
->redo_stack_bottom
)
2395 if (edit
->redo_stack
[spm1
] < 0)
2397 d
= edit
->redo_stack
[(sp
- 2) & edit
->redo_stack_size_mask
];
2398 if (d
== c
&& edit
->redo_stack
[spm1
] > -1000000000)
2400 if (c
< KEY_PRESS
) /* --> no need to push multiple do-nothings */
2401 edit
->redo_stack
[spm1
]--;
2407 d
= edit
->redo_stack
[spm1
];
2411 return; /* --> no need to push multiple do-nothings */
2412 edit
->redo_stack
[sp
] = -2;
2413 goto redo_check_bottom
;
2417 edit
->redo_stack
[sp
] = c
;
2420 edit
->redo_stack_pointer
= (edit
->redo_stack_pointer
+ 1) & edit
->redo_stack_size_mask
;
2422 /* if the sp wraps round and catches the redo_stack_bottom then erase
2423 * the first set of actions on the stack to make space - by moving
2424 * redo_stack_bottom forward one "key press" */
2425 c
= (edit
->redo_stack_pointer
+ 2) & edit
->redo_stack_size_mask
;
2426 if ((unsigned long) c
== edit
->redo_stack_bottom
||
2427 (((unsigned long) c
+ 1) & edit
->redo_stack_size_mask
) == edit
->redo_stack_bottom
)
2430 edit
->redo_stack_bottom
= (edit
->redo_stack_bottom
+ 1) & edit
->redo_stack_size_mask
;
2432 while (edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
2433 && edit
->redo_stack_bottom
!= edit
->redo_stack_pointer
);
2436 * If a single key produced enough pushes to wrap all the way round then
2437 * we would notice that the [redo_stack_bottom] does not contain KEY_PRESS.
2438 * The stack is then initialised:
2441 if (edit
->redo_stack_pointer
!= edit
->redo_stack_bottom
2442 && edit
->redo_stack
[edit
->redo_stack_bottom
] < KEY_PRESS
)
2443 edit
->redo_stack_bottom
= edit
->redo_stack_pointer
= 0;
2446 /* --------------------------------------------------------------------------------------------- */
2448 Basic low level single character buffer alterations and movements at the cursor.
2452 edit_insert (WEdit
* edit
, int c
)
2454 /* first we must update the position of the display window */
2455 if (edit
->buffer
.curs1
< edit
->start_display
)
2457 edit
->start_display
++;
2462 /* Mark file as modified, unless the file hasn't been fully loaded */
2463 if (edit
->loading_done
)
2464 edit_modification (edit
);
2466 /* now we must update some info on the file and check if a redraw is required */
2469 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2470 edit
->buffer
.curs_line
++;
2471 edit
->buffer
.lines
++;
2472 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
2475 /* save the reverse command onto the undo stack */
2476 /* ordinary char and not space */
2478 edit_push_undo_action (edit
, BACKSPACE
);
2480 edit_push_undo_action (edit
, BACKSPACE_BR
);
2481 /* update markers */
2482 edit
->mark1
+= (edit
->mark1
> edit
->buffer
.curs1
) ? 1 : 0;
2483 edit
->mark2
+= (edit
->mark2
> edit
->buffer
.curs1
) ? 1 : 0;
2484 edit
->last_get_rule
+= (edit
->last_get_rule
> edit
->buffer
.curs1
) ? 1 : 0;
2486 edit_buffer_insert (&edit
->buffer
, c
);
2488 /* update file length */
2489 edit
->buffer
.size
++;
2492 /* --------------------------------------------------------------------------------------------- */
2493 /** same as edit_insert and move left */
2496 edit_insert_ahead (WEdit
* edit
, int c
)
2498 if (edit
->buffer
.curs1
< edit
->start_display
)
2500 edit
->start_display
++;
2504 edit_modification (edit
);
2507 book_mark_inc (edit
, edit
->buffer
.curs_line
);
2508 edit
->buffer
.lines
++;
2509 edit
->force
|= REDRAW_AFTER_CURSOR
;
2511 /* ordinary char and not space */
2513 edit_push_undo_action (edit
, DELCHAR
);
2515 edit_push_undo_action (edit
, DELCHAR_BR
);
2517 edit
->mark1
+= (edit
->mark1
>= edit
->buffer
.curs1
) ? 1 : 0;
2518 edit
->mark2
+= (edit
->mark2
>= edit
->buffer
.curs1
) ? 1 : 0;
2519 edit
->last_get_rule
+= (edit
->last_get_rule
>= edit
->buffer
.curs1
) ? 1 : 0;
2521 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2523 edit
->buffer
.size
++;
2526 /* --------------------------------------------------------------------------------------------- */
2529 edit_insert_over (WEdit
* edit
)
2533 for (i
= 0; i
< edit
->over_col
; i
++)
2534 edit_insert (edit
, ' ');
2539 /* --------------------------------------------------------------------------------------------- */
2542 edit_delete (WEdit
* edit
, gboolean byte_delete
)
2548 if (edit
->buffer
.curs2
== 0)
2552 /* if byte_delete == TRUE then delete only one byte not multibyte char */
2553 if (edit
->utf8
&& !byte_delete
)
2555 edit_buffer_get_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
2563 if (edit
->mark2
!= edit
->mark1
)
2564 edit_push_markers (edit
);
2566 for (i
= 1; i
<= cw
; i
++)
2568 if (edit
->mark1
> edit
->buffer
.curs1
)
2571 edit
->end_mark_curs
--;
2573 if (edit
->mark2
> edit
->buffer
.curs1
)
2575 if (edit
->last_get_rule
> edit
->buffer
.curs1
)
2576 edit
->last_get_rule
--;
2578 p
= edit_buffer_delete (&edit
->buffer
);
2580 edit
->buffer
.size
--;
2581 edit_push_undo_action (edit
, p
+ 256);
2584 edit_modification (edit
);
2587 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2588 edit
->buffer
.lines
--;
2589 edit
->force
|= REDRAW_AFTER_CURSOR
;
2591 if (edit
->buffer
.curs1
< edit
->start_display
)
2593 edit
->start_display
--;
2601 /* --------------------------------------------------------------------------------------------- */
2604 edit_backspace (WEdit
* edit
, gboolean byte_delete
)
2610 if (edit
->buffer
.curs1
== 0)
2613 if (edit
->mark2
!= edit
->mark1
)
2614 edit_push_markers (edit
);
2617 if (edit
->utf8
&& !byte_delete
)
2619 edit_buffer_get_prev_utf (&edit
->buffer
, edit
->buffer
.curs1
, &cw
);
2627 for (i
= 1; i
<= cw
; i
++)
2629 if (edit
->mark1
>= edit
->buffer
.curs1
)
2632 edit
->end_mark_curs
--;
2634 if (edit
->mark2
>= edit
->buffer
.curs1
)
2636 if (edit
->last_get_rule
>= edit
->buffer
.curs1
)
2637 edit
->last_get_rule
--;
2639 p
= edit_buffer_backspace (&edit
->buffer
);
2641 edit
->buffer
.size
--;
2642 edit_push_undo_action (edit
, p
);
2644 edit_modification (edit
);
2647 book_mark_dec (edit
, edit
->buffer
.curs_line
);
2648 edit
->buffer
.curs_line
--;
2649 edit
->buffer
.lines
--;
2650 edit
->force
|= REDRAW_AFTER_CURSOR
;
2653 if (edit
->buffer
.curs1
< edit
->start_display
)
2655 edit
->start_display
--;
2663 /* --------------------------------------------------------------------------------------------- */
2664 /** moves the cursor right or left: increment positive or negative respectively */
2667 edit_cursor_move (WEdit
* edit
, off_t increment
)
2671 for (; increment
< 0 && edit
->buffer
.curs1
!= 0; increment
++)
2675 edit_push_undo_action (edit
, CURS_RIGHT
);
2677 c
= edit_buffer_get_previous_byte (&edit
->buffer
);
2678 edit_buffer_insert_ahead (&edit
->buffer
, c
);
2679 c
= edit_buffer_backspace (&edit
->buffer
);
2682 edit
->buffer
.curs_line
--;
2683 edit
->force
|= REDRAW_LINE_BELOW
;
2689 for (; increment
> 0 && edit
->buffer
.curs2
!= 0; increment
--)
2693 edit_push_undo_action (edit
, CURS_LEFT
);
2695 c
= edit_buffer_get_current_byte (&edit
->buffer
);
2696 edit_buffer_insert (&edit
->buffer
, c
);
2697 c
= edit_buffer_delete (&edit
->buffer
);
2700 edit
->buffer
.curs_line
++;
2701 edit
->force
|= REDRAW_LINE_ABOVE
;
2707 /* --------------------------------------------------------------------------------------------- */
2708 /* If cols is zero this returns the count of columns from current to upto. */
2709 /* If upto is zero returns index of cols across from current. */
2712 edit_move_forward3 (const WEdit
* edit
, off_t current
, long cols
, off_t upto
)
2723 q
= edit
->buffer
.size
+ 2;
2725 for (col
= 0, p
= current
; p
< q
; p
++)
2737 orig_c
= c
= edit_buffer_get_byte (&edit
->buffer
, p
);
2745 utf_ch
= edit_buffer_get_utf (&edit
->buffer
, p
, &cw
);
2746 if (mc_global
.utf8_display
)
2750 if (g_unichar_iswide (utf_ch
))
2753 else if (cw
> 1 && g_unichar_isprint (utf_ch
))
2757 c
= convert_to_display_c (c
);
2761 return (upto
!= 0 ? (off_t
) col
: p
);
2763 col
+= TAB_SIZE
- col
% TAB_SIZE
;
2764 else if ((c
< 32 || c
== 127) && (orig_c
== c
2766 || (!mc_global
.utf8_display
&& !edit
->utf8
)
2769 /* '\r' is shown as ^M, so we must advance 2 characters */
2770 /* Caret notation for control characters */
2778 /* --------------------------------------------------------------------------------------------- */
2779 /** returns the current column position of the cursor */
2782 edit_get_col (const WEdit
* edit
)
2784 return (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2785 edit
->buffer
.curs1
);
2788 /* --------------------------------------------------------------------------------------------- */
2789 /* Scrolling functions */
2790 /* --------------------------------------------------------------------------------------------- */
2793 edit_update_curs_row (WEdit
* edit
)
2795 edit
->curs_row
= edit
->buffer
.curs_line
- edit
->start_line
;
2798 /* --------------------------------------------------------------------------------------------- */
2801 edit_update_curs_col (WEdit
* edit
)
2803 edit
->curs_col
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
),
2804 0, edit
->buffer
.curs1
);
2807 /* --------------------------------------------------------------------------------------------- */
2810 edit_get_curs_col (const WEdit
* edit
)
2812 return edit
->curs_col
;
2815 /* --------------------------------------------------------------------------------------------- */
2816 /** moves the display start position up by i lines */
2819 edit_scroll_upward (WEdit
* edit
, long i
)
2821 long lines_above
= edit
->start_line
;
2823 if (i
> lines_above
)
2827 edit
->start_line
-= i
;
2828 edit
->start_display
= edit_buffer_move_backward (&edit
->buffer
, edit
->start_display
, i
);
2829 edit
->force
|= REDRAW_PAGE
;
2830 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2832 edit_update_curs_row (edit
);
2836 /* --------------------------------------------------------------------------------------------- */
2839 edit_scroll_downward (WEdit
* edit
, long i
)
2843 lines_below
= edit
->buffer
.lines
- edit
->start_line
- (WIDGET (edit
)->lines
- 1);
2844 if (lines_below
> 0)
2846 if (i
> lines_below
)
2848 edit
->start_line
+= i
;
2849 edit
->start_display
= edit_buffer_move_forward (&edit
->buffer
, edit
->start_display
, i
, 0);
2850 edit
->force
|= REDRAW_PAGE
;
2851 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2853 edit_update_curs_row (edit
);
2856 /* --------------------------------------------------------------------------------------------- */
2859 edit_scroll_right (WEdit
* edit
, long i
)
2861 edit
->force
|= REDRAW_PAGE
;
2862 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2863 edit
->start_col
-= i
;
2866 /* --------------------------------------------------------------------------------------------- */
2869 edit_scroll_left (WEdit
* edit
, long i
)
2871 if (edit
->start_col
)
2873 edit
->start_col
+= i
;
2874 if (edit
->start_col
> 0)
2875 edit
->start_col
= 0;
2876 edit
->force
|= REDRAW_PAGE
;
2877 edit
->force
&= (0xfff - REDRAW_CHAR_ONLY
);
2881 /* --------------------------------------------------------------------------------------------- */
2882 /* high level cursor movement commands */
2883 /* --------------------------------------------------------------------------------------------- */
2886 edit_move_to_prev_col (WEdit
* edit
, off_t p
)
2888 long prev
= edit
->prev_col
;
2889 long over
= edit
->over_col
;
2891 edit_cursor_move (edit
,
2892 edit_move_forward3 (edit
, p
, prev
+ edit
->over_col
, 0) - edit
->buffer
.curs1
);
2894 if (option_cursor_beyond_eol
)
2898 line_len
= (long) edit_move_forward3 (edit
, edit_buffer_get_current_bol (&edit
->buffer
), 0,
2899 edit_buffer_get_current_eol (&edit
->buffer
));
2900 if (line_len
< prev
+ edit
->over_col
)
2902 edit
->over_col
= prev
+ over
- line_len
;
2903 edit
->prev_col
= line_len
;
2904 edit
->curs_col
= line_len
;
2908 edit
->curs_col
= prev
+ over
;
2909 edit
->prev_col
= edit
->curs_col
;
2916 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
))
2918 long fake_half_tabs
;
2920 edit_update_curs_col (edit
);
2922 fake_half_tabs
= HALF_TAB_SIZE
* space_width
;
2923 if (fake_half_tabs
!= 0 && edit
->curs_col
% fake_half_tabs
!= 0)
2928 edit
->curs_col
-= (edit
->curs_col
% fake_half_tabs
);
2929 p
= edit_buffer_get_current_bol (&edit
->buffer
);
2930 edit_cursor_move (edit
,
2931 edit_move_forward3 (edit
, p
, edit
->curs_col
,
2932 0) - edit
->buffer
.curs1
);
2933 if (!left_of_four_spaces (edit
))
2934 edit_cursor_move (edit
,
2935 edit_move_forward3 (edit
, p
, q
, 0) - edit
->buffer
.curs1
);
2941 /* --------------------------------------------------------------------------------------------- */
2942 /** check whether line in editor is blank or not
2944 * @param edit editor object
2945 * @param line number of line
2947 * @return TRUE if line in blank, FALSE otherwise
2951 edit_line_is_blank (WEdit
* edit
, long line
)
2953 return is_blank (&edit
->buffer
, edit_find_line (edit
, line
));
2956 /* --------------------------------------------------------------------------------------------- */
2957 /** move cursor to line 'line' */
2960 edit_move_to_line (WEdit
* e
, long line
)
2962 if (line
< e
->buffer
.curs_line
)
2963 edit_move_up (e
, e
->buffer
.curs_line
- line
, 0);
2965 edit_move_down (e
, line
- e
->buffer
.curs_line
, 0);
2966 edit_scroll_screen_over_cursor (e
);
2969 /* --------------------------------------------------------------------------------------------- */
2970 /** scroll window so that first visible line is 'line' */
2973 edit_move_display (WEdit
* e
, long line
)
2975 if (line
< e
->start_line
)
2976 edit_scroll_upward (e
, e
->start_line
- line
);
2978 edit_scroll_downward (e
, line
- e
->start_line
);
2981 /* --------------------------------------------------------------------------------------------- */
2982 /** save markers onto undo stack */
2985 edit_push_markers (WEdit
* edit
)
2987 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
2988 edit_push_undo_action (edit
, MARK_2
+ edit
->mark2
);
2989 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
2992 /* --------------------------------------------------------------------------------------------- */
2995 edit_set_markers (WEdit
* edit
, off_t m1
, off_t m2
, long c1
, long c2
)
3004 /* --------------------------------------------------------------------------------------------- */
3005 /** highlight marker toggle */
3008 edit_mark_cmd (WEdit
* edit
, gboolean unmark
)
3010 edit_push_markers (edit
);
3013 edit_set_markers (edit
, 0, 0, 0, 0);
3014 edit
->force
|= REDRAW_PAGE
;
3016 else if (edit
->mark2
>= 0)
3018 edit
->end_mark_curs
= -1;
3019 edit_set_markers (edit
, edit
->buffer
.curs1
, -1, edit
->curs_col
+ edit
->over_col
,
3020 edit
->curs_col
+ edit
->over_col
);
3021 edit
->force
|= REDRAW_PAGE
;
3025 edit
->end_mark_curs
= edit
->buffer
.curs1
;
3026 edit_set_markers (edit
, edit
->mark1
, edit
->buffer
.curs1
, edit
->column1
,
3027 edit
->curs_col
+ edit
->over_col
);
3031 /* --------------------------------------------------------------------------------------------- */
3032 /** highlight the word under cursor */
3035 edit_mark_current_word_cmd (WEdit
* edit
)
3039 for (pos
= edit
->buffer
.curs1
; pos
!= 0; pos
--)
3043 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3044 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
- 1);
3045 if (!isspace (c1
) && isspace (c2
))
3047 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3052 for (; pos
< edit
->buffer
.size
; pos
++)
3056 c1
= edit_buffer_get_byte (&edit
->buffer
, pos
);
3057 c2
= edit_buffer_get_byte (&edit
->buffer
, pos
+ 1);
3058 if (!isspace (c1
) && isspace (c2
))
3060 if ((my_type_of (c1
) & my_type_of (c2
)) == 0)
3063 edit
->mark2
= min (pos
+ 1, edit
->buffer
.size
);
3065 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3068 /* --------------------------------------------------------------------------------------------- */
3071 edit_mark_current_line_cmd (WEdit
* edit
)
3073 edit
->mark1
= edit_buffer_get_current_bol (&edit
->buffer
);
3074 edit
->mark2
= edit_buffer_get_current_eol (&edit
->buffer
);
3076 edit
->force
|= REDRAW_LINE_ABOVE
| REDRAW_AFTER_CURSOR
;
3079 /* --------------------------------------------------------------------------------------------- */
3082 edit_delete_line (WEdit
* edit
)
3085 * Delete right part of the line.
3086 * Note that edit_buffer_get_byte() returns '\n' when byte position is
3089 while (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3090 (void) edit_delete (edit
, TRUE
);
3094 * Note that edit_delete() will not corrupt anything if called while
3095 * cursor position is EOF.
3097 (void) edit_delete (edit
, TRUE
);
3100 * Delete left part of the line.
3101 * Note, that edit_buffer_get_byte() returns '\n' when byte position is < 0.
3103 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n')
3104 (void) edit_backspace (edit
, TRUE
);
3107 /* --------------------------------------------------------------------------------------------- */
3110 edit_push_key_press (WEdit
* edit
)
3112 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3113 if (edit
->mark2
== -1)
3115 edit_push_undo_action (edit
, MARK_1
+ edit
->mark1
);
3116 edit_push_undo_action (edit
, MARK_CURS
+ edit
->end_mark_curs
);
3120 /* --------------------------------------------------------------------------------------------- */
3123 edit_find_bracket (WEdit
* edit
)
3125 edit
->bracket
= edit_get_bracket (edit
, 1, 10000);
3126 if (edit
->last_bracket
!= edit
->bracket
)
3127 edit
->force
|= REDRAW_PAGE
;
3128 edit
->last_bracket
= edit
->bracket
;
3131 /* --------------------------------------------------------------------------------------------- */
3133 * This executes a command as though the user initiated it through a key
3134 * press. Callback with MSG_KEY as a message calls this after
3135 * translating the key press. This function can be used to pass any
3136 * command to the editor. Note that the screen wouldn't update
3137 * automatically. Either of command or char_for_insertion must be
3138 * passed as -1. Commands are executed, and char_for_insertion is
3139 * inserted at the cursor.
3143 edit_execute_key_command (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3145 if (command
== CK_MacroStartRecord
|| command
== CK_RepeatStartRecord
3147 && (command
== CK_MacroStartStopRecord
|| command
== CK_RepeatStartStopRecord
)))
3150 edit
->force
|= REDRAW_CHAR_ONLY
| REDRAW_LINE
;
3153 if (macro_index
!= -1)
3155 edit
->force
|= REDRAW_COMPLETELY
;
3156 if (command
== CK_MacroStopRecord
|| command
== CK_MacroStartStopRecord
)
3158 edit_store_macro_cmd (edit
);
3162 if (command
== CK_RepeatStopRecord
|| command
== CK_RepeatStartStopRecord
)
3164 edit_repeat_macro_cmd (edit
);
3170 if (macro_index
>= 0 && macro_index
< MAX_MACRO_LENGTH
- 1)
3172 record_macro_buf
[macro_index
].action
= command
;
3173 record_macro_buf
[macro_index
++].ch
= char_for_insertion
;
3175 /* record the beginning of a set of editing actions initiated by a key press */
3176 if (command
!= CK_Undo
&& command
!= CK_ExtendedKeyMap
)
3177 edit_push_key_press (edit
);
3179 edit_execute_cmd (edit
, command
, char_for_insertion
);
3180 if (edit
->column_highlight
)
3181 edit
->force
|= REDRAW_PAGE
;
3184 /* --------------------------------------------------------------------------------------------- */
3186 This executes a command at a lower level than macro recording.
3187 It also does not push a key_press onto the undo stack. This means
3188 that if it is called many times, a single undo command will undo
3189 all of them. It also does not check for the Undo command.
3192 edit_execute_cmd (WEdit
* edit
, unsigned long command
, int char_for_insertion
)
3194 Widget
*w
= WIDGET (edit
);
3196 if (command
== CK_WindowFullscreen
)
3198 edit_toggle_fullscreen (edit
);
3202 /* handle window state */
3203 if (edit_handle_move_resize (edit
, command
))
3206 edit
->force
|= REDRAW_LINE
;
3208 /* The next key press will unhighlight the found string, so update
3210 if (edit
->found_len
|| edit
->column_highlight
)
3211 edit
->force
|= REDRAW_PAGE
;
3215 /* a mark command with shift-arrow */
3218 case CK_MarkToWordBegin
:
3219 case CK_MarkToWordEnd
:
3225 case CK_MarkPageDown
:
3226 case CK_MarkToFileBegin
:
3227 case CK_MarkToFileEnd
:
3228 case CK_MarkToPageBegin
:
3229 case CK_MarkToPageEnd
:
3230 case CK_MarkScrollUp
:
3231 case CK_MarkScrollDown
:
3232 case CK_MarkParagraphUp
:
3233 case CK_MarkParagraphDown
:
3234 /* a mark command with alt-arrow */
3235 case CK_MarkColumnPageUp
:
3236 case CK_MarkColumnPageDown
:
3237 case CK_MarkColumnLeft
:
3238 case CK_MarkColumnRight
:
3239 case CK_MarkColumnUp
:
3240 case CK_MarkColumnDown
:
3241 case CK_MarkColumnScrollUp
:
3242 case CK_MarkColumnScrollDown
:
3243 case CK_MarkColumnParagraphUp
:
3244 case CK_MarkColumnParagraphDown
:
3245 edit
->column_highlight
= 0;
3246 if (edit
->highlight
== 0 || (edit
->mark2
!= -1 && edit
->mark1
!= edit
->mark2
))
3248 edit_mark_cmd (edit
, TRUE
); /* clear */
3249 edit_mark_cmd (edit
, FALSE
); /* marking on */
3251 edit
->highlight
= 1;
3254 /* any other command */
3256 if (edit
->highlight
)
3257 edit_mark_cmd (edit
, FALSE
); /* clear */
3258 edit
->highlight
= 0;
3261 /* first check for undo */
3262 if (command
== CK_Undo
)
3264 edit
->redo_stack_reset
= 0;
3265 edit_group_undo (edit
);
3266 edit
->found_len
= 0;
3267 edit
->prev_col
= edit_get_col (edit
);
3268 edit
->search_start
= edit
->buffer
.curs1
;
3271 /* check for redo */
3272 if (command
== CK_Redo
)
3274 edit
->redo_stack_reset
= 0;
3275 edit_do_redo (edit
);
3276 edit
->found_len
= 0;
3277 edit
->prev_col
= edit_get_col (edit
);
3278 edit
->search_start
= edit
->buffer
.curs1
;
3282 edit
->redo_stack_reset
= 1;
3284 /* An ordinary key press */
3285 if (char_for_insertion
>= 0)
3287 /* if non persistent selection and text selected */
3288 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3289 edit_block_delete_cmd (edit
);
3291 if (edit
->overwrite
)
3293 /* remove char only one time, after input first byte, multibyte chars */
3295 if (!mc_global
.utf8_display
|| edit
->charpoint
== 0)
3297 if (edit_buffer_get_current_byte (&edit
->buffer
) != '\n')
3299 edit_delete (edit
, FALSE
);
3301 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3302 edit_insert_over (edit
);
3304 if (char_for_insertion
> 255 && !mc_global
.utf8_display
)
3306 unsigned char str
[UTF8_CHAR_LEN
+ 1];
3310 res
= g_unichar_to_utf8 (char_for_insertion
, (char *) str
);
3320 while (i
<= UTF8_CHAR_LEN
&& str
[i
] != '\0')
3322 char_for_insertion
= str
[i
];
3323 edit_insert (edit
, char_for_insertion
);
3329 edit_insert (edit
, char_for_insertion
);
3331 if (option_auto_para_formatting
)
3333 format_paragraph (edit
, FALSE
);
3334 edit
->force
|= REDRAW_PAGE
;
3337 check_and_wrap_line (edit
);
3338 edit
->found_len
= 0;
3339 edit
->prev_col
= edit_get_col (edit
);
3340 edit
->search_start
= edit
->buffer
.curs1
;
3341 edit_find_bracket (edit
);
3347 case CK_TopOnScreen
:
3348 case CK_BottomOnScreen
:
3361 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3363 if (edit
->column_highlight
)
3364 edit_push_undo_action (edit
, COLUMN_ON
);
3365 edit
->column_highlight
= 0;
3366 edit_mark_cmd (edit
, TRUE
);
3372 case CK_TopOnScreen
:
3373 case CK_BottomOnScreen
:
3374 case CK_MarkToPageBegin
:
3375 case CK_MarkToPageEnd
:
3380 case CK_MarkToWordBegin
:
3381 case CK_MarkToWordEnd
:
3384 case CK_MarkColumnUp
:
3385 case CK_MarkColumnDown
:
3386 if (edit
->mark2
== -1)
3387 break; /*marking is following the cursor: may need to highlight a whole line */
3392 edit
->force
|= REDRAW_CHAR_ONLY
;
3395 /* basic cursor key commands */
3399 /* if non persistent selection and text selected */
3400 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3401 edit_block_delete_cmd (edit
);
3402 else if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3404 else if (option_backspace_through_tabs
&& is_in_indent (&edit
->buffer
))
3406 while (edit_buffer_get_previous_byte (&edit
->buffer
) != '\n' && edit
->buffer
.curs1
> 0)
3407 edit_backspace (edit
, TRUE
);
3409 else if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
)
3410 && right_of_four_spaces (edit
))
3414 for (i
= 0; i
< HALF_TAB_SIZE
; i
++)
3415 edit_backspace (edit
, TRUE
);
3418 edit_backspace (edit
, FALSE
);
3421 /* if non persistent selection and text selected */
3422 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3423 edit_block_delete_cmd (edit
);
3426 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3427 edit_insert_over (edit
);
3429 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3433 for (i
= 1; i
<= HALF_TAB_SIZE
; i
++)
3434 edit_delete (edit
, TRUE
);
3437 edit_delete (edit
, FALSE
);
3440 case CK_DeleteToWordBegin
:
3442 edit_left_delete_word (edit
);
3444 case CK_DeleteToWordEnd
:
3445 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3446 edit_insert_over (edit
);
3448 edit_right_delete_word (edit
);
3451 edit_delete_line (edit
);
3453 case CK_DeleteToHome
:
3454 edit_delete_to_line_begin (edit
);
3456 case CK_DeleteToEnd
:
3457 edit_delete_to_line_end (edit
);
3461 if (option_auto_para_formatting
)
3463 edit_double_newline (edit
);
3464 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3465 edit_auto_indent (edit
);
3466 format_paragraph (edit
, FALSE
);
3470 edit_insert (edit
, '\n');
3471 if (option_return_does_auto_indent
&& !bracketed_pasting_in_progress
)
3472 edit_auto_indent (edit
);
3476 edit_insert (edit
, '\n');
3479 case CK_MarkColumnPageUp
:
3480 edit
->column_highlight
= 1;
3483 edit_move_up (edit
, w
->lines
- 1, 1);
3485 case CK_MarkColumnPageDown
:
3486 edit
->column_highlight
= 1;
3488 case CK_MarkPageDown
:
3489 edit_move_down (edit
, w
->lines
- 1, 1);
3491 case CK_MarkColumnLeft
:
3492 edit
->column_highlight
= 1;
3495 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && right_of_four_spaces (edit
))
3497 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3500 edit_cursor_move (edit
, -HALF_TAB_SIZE
);
3501 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3504 edit_left_char_move_cmd (edit
);
3506 case CK_MarkColumnRight
:
3507 edit
->column_highlight
= 1;
3510 if (option_fake_half_tabs
&& is_in_indent (&edit
->buffer
) && left_of_four_spaces (edit
))
3512 edit_cursor_move (edit
, HALF_TAB_SIZE
);
3513 edit
->force
&= (0xFFF - REDRAW_CHAR_ONLY
);
3516 edit_right_char_move_cmd (edit
);
3518 case CK_TopOnScreen
:
3519 case CK_MarkToPageBegin
:
3520 edit_begin_page (edit
);
3522 case CK_BottomOnScreen
:
3523 case CK_MarkToPageEnd
:
3524 edit_end_page (edit
);
3527 case CK_MarkToWordBegin
:
3529 edit_left_word_move_cmd (edit
);
3532 case CK_MarkToWordEnd
:
3534 edit_right_word_move_cmd (edit
);
3536 case CK_MarkColumnUp
:
3537 edit
->column_highlight
= 1;
3540 edit_move_up (edit
, 1, 0);
3542 case CK_MarkColumnDown
:
3543 edit
->column_highlight
= 1;
3546 edit_move_down (edit
, 1, 0);
3548 case CK_MarkColumnParagraphUp
:
3549 edit
->column_highlight
= 1;
3550 case CK_ParagraphUp
:
3551 case CK_MarkParagraphUp
:
3552 edit_move_up_paragraph (edit
, 0);
3554 case CK_MarkColumnParagraphDown
:
3555 edit
->column_highlight
= 1;
3556 case CK_ParagraphDown
:
3557 case CK_MarkParagraphDown
:
3558 edit_move_down_paragraph (edit
, 0);
3560 case CK_MarkColumnScrollUp
:
3561 edit
->column_highlight
= 1;
3563 case CK_MarkScrollUp
:
3564 edit_move_up (edit
, 1, 1);
3566 case CK_MarkColumnScrollDown
:
3567 edit
->column_highlight
= 1;
3569 case CK_MarkScrollDown
:
3570 edit_move_down (edit
, 1, 1);
3574 edit_cursor_to_bol (edit
);
3578 edit_cursor_to_eol (edit
);
3581 /* if text marked shift block */
3582 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3584 if (edit
->mark2
< 0)
3585 edit_mark_cmd (edit
, FALSE
);
3586 edit_move_block_to_right (edit
);
3590 if (option_cursor_beyond_eol
)
3591 edit_insert_over (edit
);
3592 edit_tab_cmd (edit
);
3593 if (option_auto_para_formatting
)
3595 format_paragraph (edit
, FALSE
);
3596 edit
->force
|= REDRAW_PAGE
;
3599 check_and_wrap_line (edit
);
3603 case CK_InsertOverwrite
:
3604 edit
->overwrite
= !edit
->overwrite
;
3608 if (edit
->mark2
>= 0)
3610 if (edit
->column_highlight
)
3611 edit_push_undo_action (edit
, COLUMN_ON
);
3612 edit
->column_highlight
= 0;
3614 edit_mark_cmd (edit
, FALSE
);
3617 if (!edit
->column_highlight
)
3618 edit_push_undo_action (edit
, COLUMN_OFF
);
3619 edit
->column_highlight
= 1;
3620 edit_mark_cmd (edit
, FALSE
);
3623 edit_set_markers (edit
, 0, edit
->buffer
.size
, 0, 0);
3624 edit
->force
|= REDRAW_PAGE
;
3627 if (edit
->column_highlight
)
3628 edit_push_undo_action (edit
, COLUMN_ON
);
3629 edit
->column_highlight
= 0;
3630 edit_mark_cmd (edit
, TRUE
);
3633 if (edit
->column_highlight
)
3634 edit_push_undo_action (edit
, COLUMN_ON
);
3635 edit
->column_highlight
= 0;
3636 edit_mark_current_word_cmd (edit
);
3639 if (edit
->column_highlight
)
3640 edit_push_undo_action (edit
, COLUMN_ON
);
3641 edit
->column_highlight
= 0;
3642 edit_mark_current_line_cmd (edit
);
3646 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_FOUND_COLOR
);
3647 if (book_mark_query_color (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
))
3648 book_mark_clear (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3650 book_mark_insert (edit
, edit
->buffer
.curs_line
, BOOK_MARK_COLOR
);
3652 case CK_BookmarkFlush
:
3653 book_mark_flush (edit
, BOOK_MARK_COLOR
);
3654 book_mark_flush (edit
, BOOK_MARK_FOUND_COLOR
);
3655 edit
->force
|= REDRAW_PAGE
;
3657 case CK_BookmarkNext
:
3658 if (edit
->book_mark
!= NULL
)
3660 edit_book_mark_t
*p
;
3662 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3663 if (p
->next
!= NULL
)
3666 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3667 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3668 edit_move_to_line (edit
, p
->line
);
3672 case CK_BookmarkPrev
:
3673 if (edit
->book_mark
!= NULL
)
3675 edit_book_mark_t
*p
;
3677 p
= book_mark_find (edit
, edit
->buffer
.curs_line
);
3678 while (p
->line
== edit
->buffer
.curs_line
)
3679 if (p
->prev
!= NULL
)
3683 if (p
->line
>= edit
->start_line
+ w
->lines
|| p
->line
< edit
->start_line
)
3684 edit_move_display (edit
, p
->line
- w
->lines
/ 2);
3685 edit_move_to_line (edit
, p
->line
);
3691 case CK_MarkToFileBegin
:
3692 edit_move_to_top (edit
);
3695 case CK_MarkToFileEnd
:
3696 edit_move_to_bottom (edit
);
3700 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3701 edit_insert_over (edit
);
3702 edit_block_copy_cmd (edit
);
3705 edit_block_delete_cmd (edit
);
3708 edit_block_move_cmd (edit
);
3711 case CK_BlockShiftLeft
:
3712 if (edit
->mark1
!= edit
->mark2
)
3713 edit_move_block_to_left (edit
);
3715 case CK_BlockShiftRight
:
3716 if (edit
->mark1
!= edit
->mark2
)
3717 edit_move_block_to_right (edit
);
3720 edit_copy_to_X_buf_cmd (edit
);
3723 edit_cut_to_X_buf_cmd (edit
);
3726 /* if non persistent selection and text selected */
3727 if (!option_persistent_selections
&& edit
->mark1
!= edit
->mark2
)
3728 edit_block_delete_cmd (edit
);
3729 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
3730 edit_insert_over (edit
);
3731 edit_paste_from_X_buf_cmd (edit
);
3732 if (!option_persistent_selections
&& edit
->mark2
>= 0)
3734 if (edit
->column_highlight
)
3735 edit_push_undo_action (edit
, COLUMN_ON
);
3736 edit
->column_highlight
= 0;
3737 edit_mark_cmd (edit
, TRUE
);
3741 edit_paste_from_history (edit
);
3745 edit_save_as_cmd (edit
);
3748 edit_save_confirm_cmd (edit
);
3751 edit_save_block_cmd (edit
);
3754 edit_insert_file_cmd (edit
);
3758 edit_load_back_cmd (edit
);
3761 edit_load_forward_cmd (edit
);
3764 case CK_SyntaxChoose
:
3765 edit_syntax_dialog (edit
);
3769 edit_search_cmd (edit
, FALSE
);
3771 case CK_SearchContinue
:
3772 edit_search_cmd (edit
, TRUE
);
3775 edit_replace_cmd (edit
, 0);
3777 case CK_ReplaceContinue
:
3778 edit_replace_cmd (edit
, 1);
3781 /* if text marked shift block */
3782 if (edit
->mark1
!= edit
->mark2
&& !option_persistent_selections
)
3783 edit_move_block_to_left (edit
);
3785 edit_complete_word_cmd (edit
);
3788 edit_get_match_keyword_cmd (edit
);
3792 case CK_SpellCheckCurrentWord
:
3793 edit_suggest_current_word (edit
);
3796 edit_spellcheck_file (edit
);
3798 case CK_SpellCheckSelectLang
:
3799 edit_set_spell_lang ();
3806 /* fool gcc to prevent a Y2K warning */
3807 char time_format
[] = "_c";
3808 time_format
[0] = '%';
3810 FMT_LOCALTIME_CURRENT (s
, sizeof (s
), time_format
);
3811 edit_print_string (edit
, s
);
3812 edit
->force
|= REDRAW_PAGE
;
3816 edit_goto_cmd (edit
);
3818 case CK_ParagraphFormat
:
3819 format_paragraph (edit
, TRUE
);
3820 edit
->force
|= REDRAW_PAGE
;
3822 case CK_MacroDelete
:
3823 edit_delete_macro_cmd (edit
);
3825 case CK_MatchBracket
:
3826 edit_goto_matching_bracket (edit
);
3829 user_menu (edit
, NULL
, -1);
3832 edit_sort_cmd (edit
);
3834 case CK_ExternalCommand
:
3835 edit_ext_cmd (edit
);
3838 edit_mail_dialog (edit
);
3841 case CK_SelectCodepage
:
3842 edit_select_codepage_cmd (edit
);
3845 case CK_InsertLiteral
:
3846 edit_insert_literal_cmd (edit
);
3848 case CK_MacroStartStopRecord
:
3849 edit_begin_end_macro_cmd (edit
);
3851 case CK_RepeatStartStopRecord
:
3852 edit_begin_end_repeat_cmd (edit
);
3854 case CK_ExtendedKeyMap
:
3855 edit
->extmod
= TRUE
;
3862 if ((command
/ CK_PipeBlock (0)) == 1)
3863 edit_block_process_cmd (edit
, command
- CK_PipeBlock (0));
3865 /* keys which must set the col position, and the search vars */
3869 case CK_SearchContinue
:
3871 case CK_ReplaceContinue
:
3873 edit
->prev_col
= edit_get_col (edit
);
3877 case CK_MarkColumnUp
:
3880 case CK_MarkColumnDown
:
3883 case CK_MarkColumnPageUp
:
3885 case CK_MarkPageDown
:
3886 case CK_MarkColumnPageDown
:
3888 case CK_MarkToFileBegin
:
3890 case CK_MarkToFileEnd
:
3891 case CK_ParagraphUp
:
3892 case CK_MarkParagraphUp
:
3893 case CK_MarkColumnParagraphUp
:
3894 case CK_ParagraphDown
:
3895 case CK_MarkParagraphDown
:
3896 case CK_MarkColumnParagraphDown
:
3898 case CK_MarkScrollUp
:
3899 case CK_MarkColumnScrollUp
:
3901 case CK_MarkScrollDown
:
3902 case CK_MarkColumnScrollDown
:
3903 edit
->search_start
= edit
->buffer
.curs1
;
3904 edit
->found_len
= 0;
3907 edit
->found_len
= 0;
3908 edit
->prev_col
= edit_get_col (edit
);
3909 edit
->search_start
= edit
->buffer
.curs1
;
3911 edit_find_bracket (edit
);
3913 if (option_auto_para_formatting
)
3919 case CK_DeleteToWordBegin
:
3920 case CK_DeleteToWordEnd
:
3921 case CK_DeleteToHome
:
3922 case CK_DeleteToEnd
:
3923 format_paragraph (edit
, FALSE
);
3924 edit
->force
|= REDRAW_PAGE
;
3929 /* --------------------------------------------------------------------------------------------- */
3932 edit_stack_init (void)
3934 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3936 edit_history_moveto
[edit_stack_iterator
].filename_vpath
= NULL
;
3937 edit_history_moveto
[edit_stack_iterator
].line
= -1;
3940 edit_stack_iterator
= 0;
3943 /* --------------------------------------------------------------------------------------------- */
3946 edit_stack_free (void)
3948 for (edit_stack_iterator
= 0; edit_stack_iterator
< MAX_HISTORY_MOVETO
; edit_stack_iterator
++)
3949 vfs_path_free (edit_history_moveto
[edit_stack_iterator
].filename_vpath
);
3952 /* --------------------------------------------------------------------------------------------- */
3956 edit_move_up (WEdit
* edit
, long i
, gboolean do_scroll
)
3958 edit_move_updown (edit
, i
, do_scroll
, TRUE
);
3961 /* --------------------------------------------------------------------------------------------- */
3965 edit_move_down (WEdit
* edit
, long i
, gboolean do_scroll
)
3967 edit_move_updown (edit
, i
, do_scroll
, FALSE
);
3970 /* --------------------------------------------------------------------------------------------- */