2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru>, 2012, 2013
11 Ilia Maslakov <il.smind@gmail.com>, 2012
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 high level editing commands
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
46 #include <sys/types.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
59 #include "lib/strutil.h" /* utf string functions */
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
66 #include "lib/charsets.h"
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
72 #include "src/selcodepage.h"
74 #include "src/keybind-defaults.h"
75 #include "src/util.h" /* check_for_default() */
76 #include "src/filemanager/layout.h" /* mc_refresh() */
78 #include "edit-impl.h"
79 #include "editwidget.h"
80 #include "editcmd_dialogs.h"
83 #include "spell_dialogs.h"
87 /*** global variables ****************************************************************************/
89 /* search and replace: */
90 int search_create_bookmark
= FALSE
;
92 /* queries on a save */
93 int edit_confirm_save
= 1;
95 /*** file scope macro definitions ****************************************************************/
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
110 /*** file scope type declarations ****************************************************************/
112 /*** file scope variables ************************************************************************/
114 static unsigned long edit_save_mode_radio_id
, edit_save_mode_input_id
;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
121 edit_save_mode_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
126 if (sender
!= NULL
&& sender
->id
== edit_save_mode_radio_id
)
130 ww
= dlg_find_by_id (DIALOG (w
), edit_save_mode_input_id
);
131 widget_disable (ww
, RADIO (sender
)->sel
!= 2);
135 return MSG_NOT_HANDLED
;
138 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
142 /* --------------------------------------------------------------------------------------------- */
144 /* If 0 (quick save) then a) create/truncate <filename> file,
145 b) save to <filename>;
146 if 1 (safe save) then a) save to <tempnam>,
147 b) rename <tempnam> to <filename>;
148 if 2 (do backups) then a) save to <tempnam>,
149 b) rename <filename> to <filename.backup_ext>,
150 c) rename <tempnam> to <filename>. */
152 /* returns 0 on error, -1 on abort */
155 edit_save_file (WEdit
* edit
, const vfs_path_t
* filename_vpath
)
160 int this_save_mode
, fd
= -1;
161 vfs_path_t
*real_filename_vpath
;
162 vfs_path_t
*savename_vpath
= NULL
;
163 const char *start_filename
;
164 const vfs_path_element_t
*vpath_element
;
166 vpath_element
= vfs_path_get_by_index (filename_vpath
, 0);
167 if (vpath_element
== NULL
)
170 start_filename
= vpath_element
->path
;
171 if (*start_filename
== '\0')
174 if (*start_filename
!= PATH_SEP
&& edit
->dir_vpath
!= NULL
)
176 real_filename_vpath
= vfs_path_append_vpath_new (edit
->dir_vpath
, filename_vpath
, NULL
);
180 real_filename_vpath
= vfs_path_clone (filename_vpath
);
183 this_save_mode
= option_save_mode
;
184 if (this_save_mode
!= EDIT_QUICK_SAVE
)
186 if (!vfs_file_is_local (real_filename_vpath
)
187 || (fd
= mc_open (real_filename_vpath
, O_RDONLY
| O_BINARY
)) == -1)
190 * The file does not exists yet, so no safe save or
191 * backup are necessary.
193 this_save_mode
= EDIT_QUICK_SAVE
;
199 if (this_save_mode
== EDIT_QUICK_SAVE
&& !edit
->skip_detach_prompt
)
204 rv
= mc_stat (real_filename_vpath
, &sb
);
205 if (rv
== 0 && sb
.st_nlink
> 1)
207 rv
= edit_query_dialog3 (_("Warning"),
208 _("File has hard-links. Detach before saving?"),
209 _("&Yes"), _("&No"), _("&Cancel"));
213 this_save_mode
= EDIT_SAFE_SAVE
;
216 edit
->skip_detach_prompt
= 1;
219 vfs_path_free (real_filename_vpath
);
224 /* Prevent overwriting changes from other editor sessions. */
225 if (rv
== 0 && edit
->stat1
.st_mtime
!= 0 && edit
->stat1
.st_mtime
!= sb
.st_mtime
)
227 /* The default action is "Cancel". */
230 rv
= edit_query_dialog2 (_("Warning"),
231 _("The file has been modified in the meantime. Save anyway?"),
232 _("&Yes"), _("&Cancel"));
235 vfs_path_free (real_filename_vpath
);
241 if (this_save_mode
!= EDIT_QUICK_SAVE
)
243 char *savedir
, *saveprefix
;
245 savedir
= vfs_path_tokens_get (real_filename_vpath
, 0, -1);
247 savedir
= g_strdup (".");
249 /* Token-related function never return leading slash, so we need add it manually */
250 saveprefix
= mc_build_filename ("/", savedir
, "cooledit", NULL
);
252 fd
= mc_mkstemps (&savename_vpath
, saveprefix
, NULL
);
254 if (savename_vpath
== NULL
)
256 vfs_path_free (real_filename_vpath
);
260 * Close for now because mc_mkstemps use pure open system call
261 * to create temporary file and it needs to be reopened by
262 * VFS-aware mc_open().
267 savename_vpath
= vfs_path_clone (real_filename_vpath
);
269 (void) mc_chown (savename_vpath
, edit
->stat1
.st_uid
, edit
->stat1
.st_gid
);
270 (void) mc_chmod (savename_vpath
, edit
->stat1
.st_mode
);
272 fd
= mc_open (savename_vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_BINARY
, edit
->stat1
.st_mode
);
277 p
= edit_get_write_filter (savename_vpath
, real_filename_vpath
);
283 file
= (FILE *) popen (p
, "w");
287 filelen
= edit_write_stream (edit
, file
);
291 if (pclose (file
) != 0)
293 tmp
= g_strdup_printf (_("Error writing to pipe: %s"), p
);
294 edit_error_dialog (_("Error"), tmp
);
303 tmp
= g_strdup_printf (_("Cannot open pipe for writing: %s"), p
);
304 edit_error_dialog (_("Error"), get_sys_error (tmp
));
311 else if (edit
->lb
== LB_ASIS
)
312 { /* do not change line breaks */
315 filelen
= edit
->last_byte
;
316 while (buf
<= (edit
->curs1
>> S_EDIT_BUF_SIZE
) - 1)
318 if (mc_write (fd
, (char *) edit
->buffers1
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
326 (fd
, (char *) edit
->buffers1
[buf
],
327 edit
->curs1
& M_EDIT_BUF_SIZE
) != (edit
->curs1
& M_EDIT_BUF_SIZE
))
331 else if (edit
->curs2
)
334 buf
= (edit
->curs2
>> S_EDIT_BUF_SIZE
);
337 (char *) edit
->buffers2
[buf
] + EDIT_BUF_SIZE
-
338 (edit
->curs2
& M_EDIT_BUF_SIZE
) - 1,
339 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
)) != 1 + (edit
->curs2
& M_EDIT_BUF_SIZE
))
347 if (mc_write (fd
, (char *) edit
->buffers2
[buf
], EDIT_BUF_SIZE
) != EDIT_BUF_SIZE
)
359 /* Update the file information, especially the mtime. */
360 if (mc_stat (savename_vpath
, &edit
->stat1
) == -1)
364 { /* change line breaks */
366 const vfs_path_element_t
*path_element
;
370 path_element
= vfs_path_get_by_index (savename_vpath
, -1);
371 file
= (FILE *) fopen (path_element
->path
, "w");
374 filelen
= edit_write_stream (edit
, file
);
381 msg
= g_strdup_printf (_("Cannot open file for writing: %s"), path_element
->path
);
382 edit_error_dialog (_("Error"), msg
);
388 if (filelen
!= edit
->last_byte
)
391 if (this_save_mode
== EDIT_DO_BACKUP
)
393 char *tmp_store_filename
;
394 vfs_path_element_t
*last_vpath_element
;
395 vfs_path_t
*tmp_vpath
;
399 assert (option_backup_ext
!= NULL
);
401 /* add backup extention to the path */
402 tmp_vpath
= vfs_path_clone (real_filename_vpath
);
403 last_vpath_element
= (vfs_path_element_t
*) vfs_path_get_by_index (tmp_vpath
, -1);
404 tmp_store_filename
= last_vpath_element
->path
;
405 last_vpath_element
->path
= g_strdup_printf ("%s%s", tmp_store_filename
, option_backup_ext
);
406 g_free (tmp_store_filename
);
408 ok
= (mc_rename (real_filename_vpath
, tmp_vpath
) != -1);
409 vfs_path_free (tmp_vpath
);
414 if (this_save_mode
!= EDIT_QUICK_SAVE
)
415 if (mc_rename (savename_vpath
, real_filename_vpath
) == -1)
418 vfs_path_free (real_filename_vpath
);
419 vfs_path_free (savename_vpath
);
422 /* FIXME: Is this safe ?
423 * if (this_save_mode != EDIT_QUICK_SAVE)
424 * mc_unlink (savename);
426 vfs_path_free (real_filename_vpath
);
427 vfs_path_free (savename_vpath
);
431 /* --------------------------------------------------------------------------------------------- */
434 edit_check_newline (WEdit
* edit
)
436 return !(option_check_nl_at_eof
&& edit
->last_byte
> 0
437 && edit_get_byte (edit
, edit
->last_byte
- 1) != '\n'
438 && edit_query_dialog2 (_("Warning"),
439 _("The file you are saving is not finished with a newline"),
440 _("C&ontinue"), _("&Cancel")));
443 /* --------------------------------------------------------------------------------------------- */
446 edit_get_save_file_as (WEdit
* edit
)
448 static LineBreaks cur_lb
= LB_ASIS
;
451 vfs_path_t
*ret_vpath
= NULL
;
453 const char *lb_names
[LB_NAMES
] = {
454 N_("&Do not change"),
455 N_("&Unix format (LF)"),
456 N_("&Windows/DOS format (CR LF)"),
457 N_("&Macintosh format (CR)")
460 filename
= vfs_path_to_str (edit
->filename_vpath
);
463 quick_widget_t quick_widgets
[] = {
465 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above
, filename
, "save-as",
466 &filename_res
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
467 QUICK_SEPARATOR (TRUE
),
468 QUICK_LABEL (N_("Change line breaks to:"), NULL
),
469 QUICK_RADIO (LB_NAMES
, lb_names
, (int *) &cur_lb
, NULL
),
470 QUICK_BUTTONS_OK_CANCEL
,
475 quick_dialog_t qdlg
= {
477 N_("Save As"), "[Save File As]",
478 quick_widgets
, NULL
, NULL
481 if (quick_dialog (&qdlg
) != B_CANCEL
)
486 fname
= tilde_expand (filename_res
);
487 g_free (filename_res
);
488 ret_vpath
= vfs_path_from_str (fname
);
498 /* --------------------------------------------------------------------------------------------- */
500 /** returns TRUE on success */
503 edit_save_cmd (WEdit
* edit
)
505 int res
, save_lock
= 0;
507 if (!edit
->locked
&& !edit
->delete_file
)
508 save_lock
= lock_file (edit
->filename_vpath
);
509 res
= edit_save_file (edit
, edit
->filename_vpath
);
511 /* Maintain modify (not save) lock on failure */
512 if ((res
> 0 && edit
->locked
) || save_lock
)
513 edit
->locked
= unlock_file (edit
->filename_vpath
);
515 /* On failure try 'save as', it does locking on its own */
517 return edit_save_as_cmd (edit
);
518 edit
->force
|= REDRAW_COMPLETELY
;
521 edit
->delete_file
= 0;
528 /* --------------------------------------------------------------------------------------------- */
532 * @param h screen the owner of editor window
533 * @param vpath vfs file path
534 * @return TRUE if file content was successfully loaded, FALSE otherwise
537 static inline gboolean
538 edit_load_file_from_filename (WDialog
* h
, const vfs_path_t
* vpath
)
540 Widget
*w
= WIDGET (h
);
542 return edit_add_window (h
, w
->y
+ 1, w
->x
, w
->lines
- 2, w
->cols
, vpath
, 0);
545 /* --------------------------------------------------------------------------------------------- */
548 edit_delete_column_of_text (WEdit
* edit
)
555 eval_marks (edit
, &m1
, &m2
);
556 n
= edit_move_forward (edit
, m1
, 0, m2
) + 1;
557 c
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m1
), 0, m1
);
558 d
= (long) edit_move_forward3 (edit
, edit_bol (edit
, m2
), 0, m2
);
559 b
= max (min (c
, d
), min (edit
->column1
, edit
->column2
));
560 c
= max (c
, max (edit
->column1
, edit
->column2
));
564 r
= edit_bol (edit
, edit
->curs1
);
565 p
= edit_move_forward3 (edit
, r
, b
, 0);
566 q
= edit_move_forward3 (edit
, r
, c
, 0);
571 edit_cursor_move (edit
, p
- edit
->curs1
);
574 /* delete line between margins */
575 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
576 edit_delete (edit
, TRUE
);
580 /* move to next line except on the last delete */
581 edit_cursor_move (edit
, edit_move_forward (edit
, edit
->curs1
, 1, 0) - edit
->curs1
);
585 /* --------------------------------------------------------------------------------------------- */
586 /** if success return 0 */
589 edit_block_delete (WEdit
* edit
)
591 off_t start_mark
, end_mark
;
593 long curs_line
, c1
, c2
;
595 if (eval_marks (edit
, &start_mark
, &end_mark
))
597 if (edit
->column_highlight
&& edit
->mark2
< 0)
598 edit_mark_cmd (edit
, FALSE
);
599 if ((end_mark
- start_mark
) > option_max_undo
/ 2)
601 /* Warning message with a query to continue or cancel the operation */
602 if (edit_query_dialog2
605 ("Block is large, you may not be able to undo this action"),
606 _("C&ontinue"), _("&Cancel")))
611 c1
= min (edit
->column1
, edit
->column2
);
612 c2
= max (edit
->column1
, edit
->column2
);
616 edit_push_markers (edit
);
618 curs_line
= edit
->curs_line
;
620 curs_pos
= edit
->curs_col
+ edit
->over_col
;
622 /* move cursor to start of selection */
623 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
624 edit_scroll_screen_over_cursor (edit
);
626 if (start_mark
< end_mark
)
628 if (edit
->column_highlight
)
633 edit_mark_cmd (edit
, FALSE
);
634 edit_delete_column_of_text (edit
);
635 /* move cursor to the saved position */
636 edit_move_to_line (edit
, curs_line
);
637 /* calculate line width and cursor position before cut */
638 line_width
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), 0,
639 edit_eol (edit
, edit
->curs1
));
640 if (option_cursor_beyond_eol
&& curs_pos
> line_width
)
641 edit
->over_col
= curs_pos
- line_width
;
647 for (count
= start_mark
; count
< end_mark
; count
++)
648 edit_delete (edit
, TRUE
);
651 edit_set_markers (edit
, 0, 0, 0, 0);
652 edit
->force
|= REDRAW_PAGE
;
656 /* --------------------------------------------------------------------------------------------- */
658 * Get EOL symbol for searching.
660 * @param edit editor object
665 edit_search_get_current_end_line_char (const WEdit
* edit
)
676 /* --------------------------------------------------------------------------------------------- */
678 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
680 * @param search search object
681 * @return result of checks.
684 static edit_search_line_t
685 edit_get_search_line_type (mc_search_t
* search
)
687 edit_search_line_t search_line_type
= 0;
689 if (search
->search_type
!= MC_SEARCH_T_REGEX
)
690 return search_line_type
;
692 if (*search
->original
== '^')
693 search_line_type
|= AT_START_LINE
;
695 if (search
->original
[search
->original_len
- 1] == '$')
696 search_line_type
|= AT_END_LINE
;
697 return search_line_type
;
700 /* --------------------------------------------------------------------------------------------- */
702 * Calculating the start position of next line.
704 * @param edit editor object
705 * @param current_pos current position
706 * @param max_pos max position
707 * @param end_string_symbol end of line symbol
708 * @return start position of next line
712 edit_calculate_start_of_next_line (WEdit
* edit
, off_t current_pos
, off_t max_pos
,
713 char end_string_symbol
)
717 for (i
= current_pos
; i
< max_pos
; i
++)
720 if (edit_get_byte (edit
, i
) == end_string_symbol
)
727 /* --------------------------------------------------------------------------------------------- */
729 * Calculating the end position of previous line.
731 * @param edit editor object
732 * @param current_pos current position
733 * @param end_string_symbol end of line symbol
734 * @return end position of previous line
738 edit_calculate_end_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
742 for (i
= current_pos
- 1; i
>= 0; i
--)
743 if (edit_get_byte (edit
, i
) == end_string_symbol
)
749 /* --------------------------------------------------------------------------------------------- */
751 * Calculating the start position of previous line.
753 * @param edit editor object
754 * @param current_pos current position
755 * @param end_string_symbol end of line symbol
756 * @return start position of previous line
760 edit_calculate_start_of_previous_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
762 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
763 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
765 return (current_pos
+ 1);
768 /* --------------------------------------------------------------------------------------------- */
770 * Calculating the start position of current line.
772 * @param edit editor object
773 * @param current_pos current position
774 * @param end_string_symbol end of line symbol
775 * @return start position of current line
779 edit_calculate_start_of_current_line (WEdit
* edit
, off_t current_pos
, char end_string_symbol
)
781 current_pos
= edit_calculate_end_of_previous_line (edit
, current_pos
, end_string_symbol
);
783 return (current_pos
+ 1);
786 /* --------------------------------------------------------------------------------------------- */
788 * Fixing (if needed) search start position if 'only in selection' option present.
790 * @param edit editor object
794 edit_search_fix_search_start_if_selection (WEdit
* edit
)
796 off_t start_mark
= 0;
799 if (!edit_search_options
.only_in_selection
)
802 if (eval_marks (edit
, &start_mark
, &end_mark
) != 0)
805 if (edit_search_options
.backwards
)
807 if (edit
->search_start
> end_mark
|| edit
->search_start
<= start_mark
)
808 edit
->search_start
= end_mark
;
812 if (edit
->search_start
< start_mark
|| edit
->search_start
>= end_mark
)
813 edit
->search_start
= start_mark
;
817 /* --------------------------------------------------------------------------------------------- */
820 editcmd_find (WEdit
* edit
, gsize
* len
)
822 off_t search_start
= edit
->search_start
;
824 off_t start_mark
= 0;
825 off_t end_mark
= edit
->last_byte
;
827 char end_string_symbol
;
829 end_string_symbol
= edit_search_get_current_end_line_char (edit
);
831 /* prepare for search */
832 if (edit_search_options
.only_in_selection
)
834 mark_res
= eval_marks (edit
, &start_mark
, &end_mark
);
837 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
838 edit
->search
->error_str
= g_strdup (_("Search string not found"));
842 /* fix the start and the end of search block positions */
843 if ((edit
->search_line_type
& AT_START_LINE
) != 0
844 && (start_mark
!= 0 || edit_get_byte (edit
, start_mark
- 1) != end_string_symbol
))
847 edit_calculate_start_of_next_line (edit
, start_mark
, edit
->last_byte
,
850 if ((edit
->search_line_type
& AT_END_LINE
) != 0
851 && (end_mark
- 1 != edit
->last_byte
852 || edit_get_byte (edit
, end_mark
) != end_string_symbol
))
854 end_mark
= edit_calculate_end_of_previous_line (edit
, end_mark
, end_string_symbol
);
856 if (start_mark
>= end_mark
)
858 edit
->search
->error
= MC_SEARCH_E_NOTFOUND
;
859 edit
->search
->error_str
= g_strdup (_("Search string not found"));
865 if (edit_search_options
.backwards
)
866 end_mark
= max (1, edit
->curs1
) - 1;
870 if (edit_search_options
.backwards
)
872 /* backward search */
873 search_end
= end_mark
;
875 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
877 edit_calculate_start_of_current_line (edit
, search_start
, end_string_symbol
);
879 while (search_start
>= start_mark
)
881 if (search_end
> (off_t
) (search_start
+ edit
->search
->original_len
)
882 && mc_search_is_fixed_search_str (edit
->search
))
884 search_end
= search_start
+ edit
->search
->original_len
;
886 if (mc_search_run (edit
->search
, (void *) edit
, search_start
, search_end
, len
)
887 && edit
->search
->normal_offset
== search_start
)
893 if ((edit
->search_line_type
& AT_START_LINE
) != 0)
895 edit_calculate_start_of_previous_line (edit
, search_start
, end_string_symbol
);
899 edit
->search
->error_str
= g_strdup (_("Search string not found"));
904 if ((edit
->search_line_type
& AT_START_LINE
) != 0 && search_start
!= start_mark
)
906 edit_calculate_start_of_next_line (edit
, search_start
, end_mark
, end_string_symbol
);
907 return mc_search_run (edit
->search
, (void *) edit
, search_start
, end_mark
, len
);
912 /* --------------------------------------------------------------------------------------------- */
915 edit_replace_cmd__conv_to_display (char *str
)
920 tmp
= str_convert_to_display (str
);
924 return g_string_free (tmp
, FALSE
);
925 g_string_free (tmp
, TRUE
);
928 return g_strdup (str
);
931 /* --------------------------------------------------------------------------------------------- */
934 edit_replace_cmd__conv_to_input (char *str
)
939 tmp
= str_convert_to_input (str
);
943 return g_string_free (tmp
, FALSE
);
944 g_string_free (tmp
, TRUE
);
947 return g_strdup (str
);
950 /* --------------------------------------------------------------------------------------------- */
953 edit_do_search (WEdit
* edit
)
957 if (edit
->search
== NULL
)
958 edit
->search_start
= edit
->curs1
;
960 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
962 if (search_create_bookmark
)
964 int found
= 0, books
= 0;
965 long l
= 0, l_last
= -1;
968 search_create_bookmark
= FALSE
;
969 book_mark_flush (edit
, -1);
971 while (mc_search_run (edit
->search
, (void *) edit
, q
, edit
->last_byte
, &len
))
974 edit
->search_start
= edit
->search
->normal_offset
;
976 l
+= edit_count_lines (edit
, q
, edit
->search
->normal_offset
);
979 book_mark_insert (edit
, l
, BOOK_MARK_FOUND_COLOR
);
983 q
= edit
->search
->normal_offset
+ 1;
987 edit_error_dialog (_("Search"), _("Search string not found"));
989 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
993 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
+ 1
994 && edit_search_options
.backwards
)
995 edit
->search_start
--;
997 if (edit
->found_len
!= 0 && edit
->search_start
== edit
->found_start
- 1
998 && !edit_search_options
.backwards
)
999 edit
->search_start
++;
1001 if (editcmd_find (edit
, &len
))
1003 edit
->found_start
= edit
->search_start
= edit
->search
->normal_offset
;
1004 edit
->found_len
= len
;
1006 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
1007 edit_scroll_screen_over_cursor (edit
);
1008 if (edit_search_options
.backwards
)
1009 edit
->search_start
--;
1011 edit
->search_start
++;
1015 edit
->search_start
= edit
->curs1
;
1016 if (edit
->search
->error_str
!= NULL
)
1017 edit_error_dialog (_("Search"), edit
->search
->error_str
);
1021 edit
->force
|= REDRAW_COMPLETELY
;
1022 edit_scroll_screen_over_cursor (edit
);
1025 /* --------------------------------------------------------------------------------------------- */
1028 edit_search (WEdit
* edit
)
1030 if (editcmd_dialog_search_show (edit
))
1032 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
1033 edit_search_fix_search_start_if_selection (edit
);
1034 edit_do_search (edit
);
1038 /* --------------------------------------------------------------------------------------------- */
1039 /** Return a null terminated length of text. Result must be g_free'd */
1041 static unsigned char *
1042 edit_get_block (WEdit
* edit
, off_t start
, off_t finish
, off_t
* l
)
1044 unsigned char *s
, *r
;
1046 r
= s
= g_malloc0 (finish
- start
+ 1);
1047 if (edit
->column_highlight
)
1050 /* copy from buffer, excluding chars that are out of the column 'margins' */
1051 while (start
< finish
)
1056 x
= edit_move_forward3 (edit
, edit_bol (edit
, start
), 0, start
);
1057 c
= edit_get_byte (edit
, start
);
1058 if ((x
>= edit
->column1
&& x
< edit
->column2
)
1059 || (x
>= edit
->column2
&& x
< edit
->column1
) || c
== '\n')
1069 *l
= finish
- start
;
1070 while (start
< finish
)
1071 *s
++ = edit_get_byte (edit
, start
++);
1077 /* --------------------------------------------------------------------------------------------- */
1078 /** copies a block to clipboard file */
1081 edit_save_block_to_clip_file (WEdit
* edit
, off_t start
, off_t finish
)
1086 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
1087 ret
= edit_save_block (edit
, tmp
, start
, finish
);
1092 /* --------------------------------------------------------------------------------------------- */
1095 pipe_mail (WEdit
* edit
, char *to
, char *subject
, char *cc
)
1100 to
= name_quote (to
, 0);
1101 subject
= name_quote (subject
, 0);
1102 cc
= name_quote (cc
, 0);
1103 s
= g_strconcat ("mail -s ", subject
, *cc
? " -c " : "", cc
, " ", to
, (char *) NULL
);
1117 for (i
= 0; i
< edit
->last_byte
; i
++)
1118 fputc (edit_get_byte (edit
, i
), p
);
1123 /* --------------------------------------------------------------------------------------------- */
1124 /** find first character of current word */
1127 edit_find_word_start (WEdit
* edit
, off_t
* word_start
, gsize
* word_len
)
1132 /* return if at begin of file */
1133 if (edit
->curs1
<= 0)
1136 c
= edit_get_byte (edit
, edit
->curs1
- 1);
1137 /* return if not at end or in word */
1138 if (is_break_char (c
))
1141 /* search start of word to be completed */
1144 /* return if at begin of file */
1145 if (edit
->curs1
< i
)
1149 c
= edit_get_byte (edit
, edit
->curs1
- i
);
1151 if (is_break_char (c
))
1153 /* return if word starts with digit */
1157 *word_start
= edit
->curs1
- (i
- 1); /* start found */
1158 *word_len
= (gsize
) (i
- 1);
1166 /* --------------------------------------------------------------------------------------------- */
1168 * Get current word under cursor
1170 * @param edit editor object
1171 * @param srch mc_search object
1172 * @param word_start start word position
1174 * @return newly allocated string or NULL if no any words under cursor
1178 edit_collect_completions_get_current_word (WEdit
* edit
, mc_search_t
* srch
, off_t word_start
)
1184 if (!mc_search_run (srch
, (void *) edit
, word_start
, edit
->last_byte
, &len
))
1187 temp
= g_string_sized_new (len
);
1189 for (i
= 0; i
< (off_t
) len
; i
++)
1193 chr
= edit_get_byte (edit
, word_start
+ i
);
1195 g_string_append_c (temp
, chr
);
1198 return g_string_free (temp
, temp
->len
== 0);
1201 /* --------------------------------------------------------------------------------------------- */
1202 /** collect the possible completions */
1205 edit_collect_completions (WEdit
* edit
, off_t word_start
, gsize word_len
,
1206 char *match_expr
, GString
** compl, gsize
* num
)
1214 off_t last_byte
, start
= -1;
1217 srch
= mc_search_new (match_expr
, -1);
1221 if (mc_config_get_bool
1222 (mc_main_config
, CONFIG_APP_SECTION
, "editor_wordcompletion_collect_entire_file", 0))
1224 last_byte
= edit
->last_byte
;
1228 last_byte
= word_start
;
1231 srch
->search_type
= MC_SEARCH_T_REGEX
;
1232 srch
->is_case_sensitive
= TRUE
;
1233 srch
->search_fn
= edit_search_cmd_callback
;
1235 current_word
= edit_collect_completions_get_current_word (edit
, srch
, word_start
);
1237 temp
= g_string_new ("");
1239 /* collect max MAX_WORD_COMPLETIONS completions */
1240 while (mc_search_run (srch
, (void *) edit
, start
+ 1, last_byte
, &len
))
1242 g_string_set_size (temp
, 0);
1243 start
= srch
->normal_offset
;
1245 /* add matched completion if not yet added */
1246 for (i
= 0; i
< len
; i
++)
1248 skip
= edit_get_byte (edit
, start
+ i
);
1252 /* skip current word */
1253 if (start
+ (off_t
) i
== word_start
)
1256 g_string_append_c (temp
, skip
);
1262 if (current_word
!= NULL
&& strcmp (current_word
, temp
->str
) == 0)
1267 for (i
= 0; i
< *num
; i
++)
1270 ((char *) &compl[i
]->str
[word_len
],
1271 (char *) &temp
->str
[word_len
], max (len
, compl[i
]->len
) - word_len
) == 0)
1273 GString
*this = compl[i
];
1274 for (++i
; i
< *num
; i
++)
1275 compl[i
- 1] = compl[i
];
1276 compl[*num
- 1] = this;
1278 break; /* skip it, already added */
1284 if (*num
== MAX_WORD_COMPLETIONS
)
1286 g_string_free (compl[0], TRUE
);
1287 for (i
= 1; i
< *num
; i
++)
1288 compl[i
- 1] = compl[i
];
1294 recoded
= str_convert_to_display (temp
->str
);
1296 if (recoded
&& recoded
->len
)
1297 g_string_assign (temp
, recoded
->str
);
1299 g_string_free (recoded
, TRUE
);
1302 compl[(*num
)++] = g_string_new_len (temp
->str
, temp
->len
);
1305 /* note the maximal length needed for the completion dialog */
1310 mc_search_free (srch
);
1311 g_string_free (temp
, TRUE
);
1312 g_free (current_word
);
1317 /* --------------------------------------------------------------------------------------------- */
1320 edit_insert_column_of_text (WEdit
* edit
, unsigned char *data
, off_t size
, long width
,
1321 off_t
* start_pos
, off_t
* end_pos
, long *col1
, long *col2
)
1326 cursor
= edit
->curs1
;
1327 col
= edit_get_col (edit
);
1329 for (i
= 0; i
< size
; i
++)
1331 if (data
[i
] != '\n')
1332 edit_insert (edit
, data
[i
]);
1334 { /* fill in and move to next line */
1338 if (edit_get_byte (edit
, edit
->curs1
) != '\n')
1340 for (l
= width
- (edit_get_col (edit
) - col
); l
> 0; l
-= space_width
)
1341 edit_insert (edit
, ' ');
1343 for (p
= edit
->curs1
;; p
++)
1345 if (p
== edit
->last_byte
)
1347 edit_cursor_move (edit
, edit
->last_byte
- edit
->curs1
);
1348 edit_insert_ahead (edit
, '\n');
1352 if (edit_get_byte (edit
, p
) == '\n')
1358 edit_cursor_move (edit
, edit_move_forward3 (edit
, p
, col
, 0) - edit
->curs1
);
1360 for (l
= col
- edit_get_col (edit
); l
>= space_width
; l
-= space_width
)
1361 edit_insert (edit
, ' ');
1366 *col2
= col
+ width
;
1367 *start_pos
= cursor
;
1368 *end_pos
= edit
->curs1
;
1369 edit_cursor_move (edit
, cursor
- edit
->curs1
);
1372 /* --------------------------------------------------------------------------------------------- */
1375 edit_macro_comparator (gconstpointer
* macro1
, gconstpointer
* macro2
)
1377 const macros_t
*m1
= (const macros_t
*) macro1
;
1378 const macros_t
*m2
= (const macros_t
*) macro2
;
1380 return m1
->hotkey
- m2
->hotkey
;
1383 /* --------------------------------------------------------------------------------------------- */
1386 edit_macro_sort_by_hotkey (void)
1388 if (macros_list
!= NULL
&& macros_list
->len
!= 0)
1389 g_array_sort (macros_list
, (GCompareFunc
) edit_macro_comparator
);
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_get_macro (WEdit
* edit
, int hotkey
, const macros_t
** macros
, guint
* indx
)
1397 const macros_t
*array_start
= &g_array_index (macros_list
, struct macros_t
, 0);
1399 macros_t search_macro
;
1403 search_macro
.hotkey
= hotkey
;
1404 result
= bsearch (&search_macro
, macros_list
->data
, macros_list
->len
,
1405 sizeof (macros_t
), (GCompareFunc
) edit_macro_comparator
);
1407 if (result
!= NULL
&& result
->macro
!= NULL
)
1409 *indx
= (result
- array_start
);
1417 /* --------------------------------------------------------------------------------------------- */
1418 /** returns FALSE on error */
1421 edit_delete_macro (WEdit
* edit
, int hotkey
)
1423 mc_config_t
*macros_config
= NULL
;
1424 const char *section_name
= "editor";
1425 gchar
*macros_fname
;
1428 const macros_t
*macros
= NULL
;
1430 /* clear array of actions for current hotkey */
1431 while (edit_get_macro (edit
, hotkey
, ¯os
, &indx
))
1433 if (macros
->macro
!= NULL
)
1434 g_array_free (macros
->macro
, TRUE
);
1436 g_array_remove_index (macros_list
, indx
);
1437 edit_macro_sort_by_hotkey ();
1440 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1441 macros_config
= mc_config_init (macros_fname
, FALSE
);
1442 g_free (macros_fname
);
1444 if (macros_config
== NULL
)
1447 skeyname
= lookup_key_by_code (hotkey
);
1448 while (mc_config_del_key (macros_config
, section_name
, skeyname
))
1451 mc_config_save_file (macros_config
, NULL
);
1452 mc_config_deinit (macros_config
);
1456 /* --------------------------------------------------------------------------------------------- */
1458 * Callback for the iteration of objects in the 'editors' array.
1459 * Toggle syntax highlighting in editor object.
1461 * @param data probably WEdit object
1462 * @param user_data unused
1466 edit_syntax_onoff_cb (void *data
, void *user_data
)
1470 if (edit_widget_is_editor ((const Widget
*) data
))
1472 WEdit
*edit
= (WEdit
*) data
;
1474 if (option_syntax_highlighting
)
1475 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1476 edit
->force
|= REDRAW_PAGE
;
1480 /* --------------------------------------------------------------------------------------------- */
1482 * Callback for the iteration of objects in the 'editors' array.
1483 * Redraw editor object.
1485 * @param data probably WEdit object
1486 * @param user_data unused
1490 edit_redraw_page_cb (void *data
, void *user_data
)
1494 if (edit_widget_is_editor ((const Widget
*) data
))
1495 ((WEdit
*) data
)->force
|= REDRAW_PAGE
;
1498 /* --------------------------------------------------------------------------------------------- */
1499 /*** public functions ****************************************************************************/
1500 /* --------------------------------------------------------------------------------------------- */
1503 edit_refresh_cmd (void)
1510 /* --------------------------------------------------------------------------------------------- */
1512 * Toggle syntax highlighting in all editor windows.
1514 * @param h root widget for all windows
1518 edit_syntax_onoff_cmd (WDialog
* h
)
1520 option_syntax_highlighting
= !option_syntax_highlighting
;
1521 g_list_foreach (h
->widgets
, edit_syntax_onoff_cb
, NULL
);
1525 /* --------------------------------------------------------------------------------------------- */
1527 * Toggle tabs showing in all editor windows.
1529 * @param h root widget for all windows
1533 edit_show_tabs_tws_cmd (WDialog
* h
)
1535 enable_show_tabs_tws
= !enable_show_tabs_tws
;
1536 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1540 /* --------------------------------------------------------------------------------------------- */
1542 * Toggle right margin showing in all editor windows.
1544 * @param h root widget for all windows
1548 edit_show_margin_cmd (WDialog
* h
)
1550 show_right_margin
= !show_right_margin
;
1551 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1555 /* --------------------------------------------------------------------------------------------- */
1557 * Toggle line numbers showing in all editor windows.
1559 * @param h root widget for all windows
1563 edit_show_numbers_cmd (WDialog
* h
)
1565 option_line_state
= !option_line_state
;
1566 option_line_state_width
= option_line_state
? LINE_STATE_WIDTH
: 0;
1567 g_list_foreach (h
->widgets
, edit_redraw_page_cb
, NULL
);
1571 /* --------------------------------------------------------------------------------------------- */
1574 edit_save_mode_cmd (void)
1578 const char *str
[] = {
1581 N_("&Do backups with following extension:")
1584 #ifdef HAVE_ASSERT_H
1585 assert (option_backup_ext
!= NULL
);
1591 for (i
= 0; i
< 3; i
++)
1596 quick_widget_t quick_widgets
[] = {
1598 QUICK_RADIO (3, str
, &option_save_mode
, &edit_save_mode_radio_id
),
1599 QUICK_INPUT (option_backup_ext
, "edit-backup-ext", &str_result
,
1600 &edit_save_mode_input_id
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
1601 QUICK_SEPARATOR (TRUE
),
1602 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof
, NULL
),
1603 QUICK_BUTTONS_OK_CANCEL
,
1608 quick_dialog_t qdlg
= {
1610 N_("Edit Save Mode"), "[Edit Save Mode]",
1611 quick_widgets
, edit_save_mode_callback
, NULL
1614 if (quick_dialog (&qdlg
) != B_CANCEL
)
1616 g_free (option_backup_ext
);
1617 option_backup_ext
= str_result
;
1622 /* --------------------------------------------------------------------------------------------- */
1625 edit_set_filename (WEdit
* edit
, const vfs_path_t
* name_vpath
)
1627 vfs_path_free (edit
->filename_vpath
);
1628 edit
->filename_vpath
= vfs_path_clone (name_vpath
);
1630 if (edit
->dir_vpath
== NULL
)
1631 edit
->dir_vpath
= vfs_path_clone (vfs_get_raw_current_dir ());
1634 /* --------------------------------------------------------------------------------------------- */
1635 /* Here we want to warn the users of overwriting an existing file,
1636 but only if they have made a change to the filename */
1637 /* returns TRUE on success */
1639 edit_save_as_cmd (WEdit
* edit
)
1641 /* This heads the 'Save As' dialog box */
1642 vfs_path_t
*exp_vpath
;
1644 int different_filename
= 0;
1646 if (!edit_check_newline (edit
))
1649 exp_vpath
= edit_get_save_file_as (edit
);
1650 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1652 if (exp_vpath
!= NULL
)
1654 if (vfs_path_len (exp_vpath
) == 0)
1660 if (!vfs_path_equal (edit
->filename_vpath
, exp_vpath
))
1665 if (mc_stat (exp_vpath
, &sb
) == 0 && !S_ISREG (sb
.st_mode
))
1667 edit_error_dialog (_("Save as"),
1669 ("Cannot save: destination is not a regular file")));
1673 different_filename
= 1;
1674 file
= mc_open (exp_vpath
, O_RDONLY
| O_BINARY
);
1678 /* the file exists */
1680 /* Overwrite the current file or cancel the operation */
1681 if (edit_query_dialog2
1683 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1688 edit
->stat1
.st_mode
|= S_IWUSR
;
1690 save_lock
= lock_file (exp_vpath
);
1694 /* filenames equal, check if already locked */
1695 if (!edit
->locked
&& !edit
->delete_file
)
1696 save_lock
= lock_file (exp_vpath
);
1699 if (different_filename
)
1702 * Allow user to write into saved (under another name) file
1703 * even if original file had r/o user permissions.
1705 edit
->stat1
.st_mode
|= S_IWRITE
;
1708 rv
= edit_save_file (edit
, exp_vpath
);
1712 /* Succesful, so unlock both files */
1713 if (different_filename
)
1716 unlock_file (exp_vpath
);
1718 edit
->locked
= unlock_file (edit
->filename_vpath
);
1722 if (edit
->locked
|| save_lock
)
1723 edit
->locked
= unlock_file (edit
->filename_vpath
);
1726 edit_set_filename (edit
, exp_vpath
);
1727 if (edit
->lb
!= LB_ASIS
)
1728 edit_reload (edit
, exp_vpath
);
1730 edit
->delete_file
= 0;
1731 if (different_filename
)
1732 edit_load_syntax (edit
, NULL
, edit
->syntax_type
);
1733 vfs_path_free (exp_vpath
);
1734 edit
->force
|= REDRAW_COMPLETELY
;
1737 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1740 /* Failed, so maintain modify (not save) lock */
1742 unlock_file (exp_vpath
);
1749 vfs_path_free (exp_vpath
);
1750 edit
->force
|= REDRAW_COMPLETELY
;
1754 /* {{{ Macro stuff starts here */
1755 /* --------------------------------------------------------------------------------------------- */
1758 edit_delete_macro_cmd (WEdit
* edit
)
1762 hotkey
= editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE
);
1764 if (hotkey
!= 0 && !edit_delete_macro (edit
, hotkey
))
1765 message (D_ERROR
, _("Delete macro"), _("Macro not deleted"));
1768 /* --------------------------------------------------------------------------------------------- */
1770 /** returns FALSE on error */
1772 edit_execute_macro (WEdit
* edit
, int hotkey
)
1774 gboolean res
= FALSE
;
1778 const macros_t
*macros
;
1781 if (edit_get_macro (edit
, hotkey
, ¯os
, &indx
) &&
1782 macros
->macro
!= NULL
&& macros
->macro
->len
!= 0)
1786 edit
->force
|= REDRAW_PAGE
;
1788 for (i
= 0; i
< macros
->macro
->len
; i
++)
1790 const macro_action_t
*m_act
;
1792 m_act
= &g_array_index (macros
->macro
, struct macro_action_t
, i
);
1793 edit_execute_cmd (edit
, m_act
->action
, m_act
->ch
);
1802 /* --------------------------------------------------------------------------------------------- */
1804 /** returns FALSE on error */
1806 edit_store_macro_cmd (WEdit
* edit
)
1810 GString
*marcros_string
;
1811 mc_config_t
*macros_config
= NULL
;
1812 const char *section_name
= "editor";
1813 gchar
*macros_fname
;
1814 GArray
*macros
; /* current macro */
1816 gboolean have_macro
= FALSE
;
1817 char *skeyname
= NULL
;
1820 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE
);
1821 if (hotkey
== ESC_CHAR
)
1824 tmp_act
= keybind_lookup_keymap_command (editor_map
, hotkey
);
1826 /* return FALSE if try assign macro into restricted hotkeys */
1827 if (tmp_act
== CK_MacroStartRecord
1828 || tmp_act
== CK_MacroStopRecord
|| tmp_act
== CK_MacroStartStopRecord
)
1831 edit_delete_macro (edit
, hotkey
);
1833 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1834 macros_config
= mc_config_init (macros_fname
, FALSE
);
1835 g_free (macros_fname
);
1837 if (macros_config
== NULL
)
1840 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1842 marcros_string
= g_string_sized_new (250);
1843 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1845 skeyname
= lookup_key_by_code (hotkey
);
1847 for (i
= 0; i
< macro_index
; i
++)
1849 macro_action_t m_act
;
1850 const char *action_name
;
1852 action_name
= keybind_lookup_actionname (record_macro_buf
[i
].action
);
1854 if (action_name
== NULL
)
1857 m_act
.action
= record_macro_buf
[i
].action
;
1858 m_act
.ch
= record_macro_buf
[i
].ch
;
1859 g_array_append_val (macros
, m_act
);
1861 g_string_append_printf (marcros_string
, "%s:%i;", action_name
,
1862 (int) record_macro_buf
[i
].ch
);
1867 macro
.hotkey
= hotkey
;
1868 macro
.macro
= macros
;
1869 g_array_append_val (macros_list
, macro
);
1870 mc_config_set_string (macros_config
, section_name
, skeyname
, marcros_string
->str
);
1873 mc_config_del_key (macros_config
, section_name
, skeyname
);
1876 edit_macro_sort_by_hotkey ();
1878 g_string_free (marcros_string
, TRUE
);
1879 mc_config_save_file (macros_config
, NULL
);
1880 mc_config_deinit (macros_config
);
1884 /* --------------------------------------------------------------------------------------------- */
1887 edit_repeat_macro_cmd (WEdit
* edit
)
1894 f
= input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT
, NULL
,
1895 INPUT_COMPLETE_NONE
);
1896 if (f
== NULL
|| *f
== '\0')
1902 count_repeat
= strtol (f
, &error
, 0);
1912 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
1913 edit
->force
|= REDRAW_PAGE
;
1915 for (j
= 0; j
< count_repeat
; j
++)
1916 for (i
= 0; i
< macro_index
; i
++)
1917 edit_execute_cmd (edit
, record_macro_buf
[i
].action
, record_macro_buf
[i
].ch
);
1918 edit_update_screen (edit
);
1922 /* --------------------------------------------------------------------------------------------- */
1923 /** return FALSE on error */
1926 edit_load_macro_cmd (WEdit
* edit
)
1928 mc_config_t
*macros_config
= NULL
;
1929 gchar
**profile_keys
, **keys
;
1930 gchar
**values
, **curr_values
;
1931 gsize len
, values_len
;
1932 const char *section_name
= "editor";
1933 gchar
*macros_fname
;
1938 macros_fname
= mc_config_get_full_path (MC_MACRO_FILE
);
1939 macros_config
= mc_config_init (macros_fname
, TRUE
);
1940 g_free (macros_fname
);
1942 if (macros_config
== NULL
|| macros_list
== NULL
|| macros_list
->len
!= 0)
1945 profile_keys
= keys
= mc_config_get_keys (macros_config
, section_name
, &len
);
1946 while (*profile_keys
!= NULL
)
1948 gboolean have_macro
;
1952 macros
= g_array_new (TRUE
, FALSE
, sizeof (macro_action_t
));
1954 curr_values
= values
= mc_config_get_string_list (macros_config
, section_name
,
1955 *profile_keys
, &values_len
);
1956 hotkey
= lookup_key (*profile_keys
, NULL
);
1959 while (*curr_values
!= NULL
&& *curr_values
[0] != '\0')
1961 char **macro_pair
= NULL
;
1963 macro_pair
= g_strsplit (*curr_values
, ":", 2);
1965 if (macro_pair
!= NULL
)
1967 macro_action_t m_act
;
1968 if (macro_pair
[0] == NULL
|| macro_pair
[0][0] == '\0')
1972 m_act
.action
= keybind_lookup_action (macro_pair
[0]);
1973 g_free (macro_pair
[0]);
1974 macro_pair
[0] = NULL
;
1976 if (macro_pair
[1] == NULL
|| macro_pair
[1][0] == '\0')
1980 m_act
.ch
= strtol (macro_pair
[1], NULL
, 0);
1981 g_free (macro_pair
[1]);
1982 macro_pair
[1] = NULL
;
1984 if (m_act
.action
!= 0)
1986 /* a shell command */
1987 if ((m_act
.action
/ CK_PipeBlock (0)) == 1)
1989 m_act
.action
= CK_PipeBlock (0) + (m_act
.ch
> 0 ? m_act
.ch
: 0);
1992 g_array_append_val (macros
, m_act
);
1995 g_strfreev (macro_pair
);
2002 macro
.hotkey
= hotkey
;
2003 macro
.macro
= macros
;
2004 g_array_append_val (macros_list
, macro
);
2007 g_strfreev (values
);
2010 mc_config_deinit (macros_config
);
2011 edit_macro_sort_by_hotkey ();
2015 /* }}} Macro stuff end here */
2017 /* --------------------------------------------------------------------------------------------- */
2018 /** returns TRUE on success */
2021 edit_save_confirm_cmd (WEdit
* edit
)
2025 if (edit
->filename_vpath
== NULL
)
2026 return edit_save_as_cmd (edit
);
2028 if (!edit_check_newline (edit
))
2031 if (edit_confirm_save
)
2036 filename
= vfs_path_to_str (edit
->filename_vpath
);
2037 f
= g_strdup_printf (_("Confirm save file: \"%s\""), filename
);
2039 ok
= (edit_query_dialog2 (_("Save file"), f
, _("&Save"), _("&Cancel")) == 0);
2044 return edit_save_cmd (edit
);
2047 /* --------------------------------------------------------------------------------------------- */
2049 * Ask file to edit and load it.
2051 * @return TRUE on success or cancel of ask.
2055 edit_load_cmd (WDialog
* h
)
2058 gboolean ret
= TRUE
; /* possible cancel */
2060 exp
= input_expand_dialog (_("Load"), _("Enter file name:"),
2061 MC_HISTORY_EDIT_LOAD
, INPUT_LAST_TEXT
,
2062 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
);
2064 if (exp
!= NULL
&& *exp
!= '\0')
2066 vfs_path_t
*exp_vpath
;
2068 exp_vpath
= vfs_path_from_str (exp
);
2069 ret
= edit_load_file_from_filename (h
, exp_vpath
);
2070 vfs_path_free (exp_vpath
);
2078 /* --------------------------------------------------------------------------------------------- */
2080 * Load syntax file to edit.
2082 * @return TRUE on success
2086 edit_load_syntax_file (WDialog
* h
)
2088 vfs_path_t
*extdir_vpath
;
2090 gboolean ret
= FALSE
;
2092 if (geteuid () == 0)
2093 dir
= query_dialog (_("Syntax file edit"),
2094 _("Which syntax file you want to edit?"), D_NORMAL
, 2,
2095 _("&User"), _("&System wide"));
2098 vfs_path_build_filename (mc_global
.sysconfig_dir
, "syntax", "Syntax", (char *) NULL
);
2099 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
2101 vfs_path_free (extdir_vpath
);
2103 vfs_path_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
2108 vfs_path_t
*user_syntax_file_vpath
;
2110 user_syntax_file_vpath
= mc_config_get_full_vpath (EDIT_SYNTAX_FILE
);
2111 check_for_default (extdir_vpath
, user_syntax_file_vpath
);
2112 ret
= edit_load_file_from_filename (h
, user_syntax_file_vpath
);
2113 vfs_path_free (user_syntax_file_vpath
);
2116 ret
= edit_load_file_from_filename (h
, extdir_vpath
);
2118 vfs_path_free (extdir_vpath
);
2123 /* --------------------------------------------------------------------------------------------- */
2125 * Load menu file to edit.
2127 * @return TRUE on success
2131 edit_load_menu_file (WDialog
* h
)
2133 vfs_path_t
*buffer_vpath
;
2134 vfs_path_t
*menufile_vpath
;
2138 dir
= query_dialog (_("Menu edit"),
2139 _("Which menu file do you want to edit?"), D_NORMAL
,
2140 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2142 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2143 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
2145 vfs_path_free (menufile_vpath
);
2146 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2152 buffer_vpath
= vfs_path_from_str (EDIT_LOCAL_MENU
);
2153 check_for_default (menufile_vpath
, buffer_vpath
);
2154 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
2158 buffer_vpath
= mc_config_get_full_vpath (EDIT_HOME_MENU
);
2159 check_for_default (menufile_vpath
, buffer_vpath
);
2163 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, EDIT_GLOBAL_MENU
, NULL
);
2164 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
2166 vfs_path_free (buffer_vpath
);
2168 vfs_path_build_filename (mc_global
.share_data_dir
, EDIT_GLOBAL_MENU
, NULL
);
2173 vfs_path_free (menufile_vpath
);
2177 ret
= edit_load_file_from_filename (h
, buffer_vpath
);
2179 vfs_path_free (buffer_vpath
);
2180 vfs_path_free (menufile_vpath
);
2185 /* --------------------------------------------------------------------------------------------- */
2187 * Close window with opened file.
2189 * @return TRUE if file was closed.
2193 edit_close_cmd (WEdit
* edit
)
2197 ret
= (edit
!= NULL
) && edit_ok_to_exit (edit
);
2201 WDialog
*h
= WIDGET (edit
)->owner
;
2203 if (edit
->locked
!= 0)
2204 unlock_file (edit
->filename_vpath
);
2208 if (edit_widget_is_editor (WIDGET (h
->current
->data
)))
2209 edit
= (WEdit
*) h
->current
->data
;
2212 edit
= find_editor (h
);
2214 dlg_set_top_widget (edit
);
2219 edit
->force
|= REDRAW_COMPLETELY
;
2224 /* --------------------------------------------------------------------------------------------- */
2226 if mark2 is -1 then marking is from mark1 to the cursor.
2227 Otherwise its between the markers. This handles this.
2228 Returns 1 if no text is marked.
2232 eval_marks (WEdit
* edit
, off_t
* start_mark
, off_t
* end_mark
)
2234 if (edit
->mark1
!= edit
->mark2
)
2236 off_t start_bol
, start_eol
;
2237 off_t end_bol
, end_eol
;
2242 if (edit
->end_mark_curs
< 0)
2243 end_mark_curs
= edit
->curs1
;
2245 end_mark_curs
= edit
->end_mark_curs
;
2247 if (edit
->mark2
>= 0)
2249 *start_mark
= min (edit
->mark1
, edit
->mark2
);
2250 *end_mark
= max (edit
->mark1
, edit
->mark2
);
2254 *start_mark
= min (edit
->mark1
, end_mark_curs
);
2255 *end_mark
= max (edit
->mark1
, end_mark_curs
);
2256 edit
->column2
= edit
->curs_col
+ edit
->over_col
;
2259 if (edit
->column_highlight
2260 && (((edit
->mark1
> end_mark_curs
) && (edit
->column1
< edit
->column2
))
2261 || ((edit
->mark1
< end_mark_curs
) && (edit
->column1
> edit
->column2
))))
2263 start_bol
= edit_bol (edit
, *start_mark
);
2264 start_eol
= edit_eol (edit
, start_bol
- 1) + 1;
2265 end_bol
= edit_bol (edit
, *end_mark
);
2266 end_eol
= edit_eol (edit
, *end_mark
);
2267 col1
= min (edit
->column1
, edit
->column2
);
2268 col2
= max (edit
->column1
, edit
->column2
);
2270 diff1
= edit_move_forward3 (edit
, start_bol
, col2
, 0) -
2271 edit_move_forward3 (edit
, start_bol
, col1
, 0);
2272 diff2
= edit_move_forward3 (edit
, end_bol
, col2
, 0) -
2273 edit_move_forward3 (edit
, end_bol
, col1
, 0);
2275 *start_mark
-= diff1
;
2277 *start_mark
= max (*start_mark
, start_eol
);
2278 *end_mark
= min (*end_mark
, end_eol
);
2284 *start_mark
= *end_mark
= 0;
2285 edit
->column2
= edit
->column1
= 0;
2290 /* --------------------------------------------------------------------------------------------- */
2293 edit_block_copy_cmd (WEdit
* edit
)
2295 off_t start_mark
, end_mark
, current
= edit
->curs1
;
2300 unsigned char *copy_buf
;
2302 edit_update_curs_col (edit
);
2303 if (eval_marks (edit
, &start_mark
, &end_mark
))
2306 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2308 /* all that gets pushed are deletes hence little space is used on the stack */
2310 edit_push_markers (edit
);
2312 if (edit
->column_highlight
)
2314 col_delta
= abs (edit
->column2
- edit
->column1
);
2315 edit_insert_column_of_text (edit
, copy_buf
, size
, col_delta
, &mark1
, &mark2
, &c1
, &c2
);
2319 int size_orig
= size
;
2322 edit_insert_ahead (edit
, copy_buf
[size
]);
2324 /* Place cursor at the end of text selection */
2325 if (option_cursor_after_inserted_block
)
2326 edit_cursor_move (edit
, size_orig
);
2330 edit_scroll_screen_over_cursor (edit
);
2332 if (edit
->column_highlight
)
2333 edit_set_markers (edit
, edit
->curs1
, mark2
, c1
, c2
);
2334 else if (start_mark
< current
&& end_mark
> current
)
2335 edit_set_markers (edit
, start_mark
, end_mark
+ end_mark
- start_mark
, 0, 0);
2337 edit
->force
|= REDRAW_PAGE
;
2341 /* --------------------------------------------------------------------------------------------- */
2344 edit_block_move_cmd (WEdit
* edit
)
2347 unsigned char *copy_buf
= NULL
;
2348 off_t start_mark
, end_mark
;
2350 if (eval_marks (edit
, &start_mark
, &end_mark
))
2353 if (!edit
->column_highlight
&& edit
->curs1
> start_mark
&& edit
->curs1
< end_mark
)
2356 if (edit
->mark2
< 0)
2357 edit_mark_cmd (edit
, FALSE
);
2358 edit_push_markers (edit
);
2360 if (edit
->column_highlight
)
2364 long c1
, c2
, b_width
;
2367 c1
= min (edit
->column1
, edit
->column2
);
2368 c2
= max (edit
->column1
, edit
->column2
);
2371 edit_update_curs_col (edit
);
2374 x2
= x
+ edit
->over_col
;
2376 /* do nothing when cursor inside first line of selected area */
2377 if ((edit_eol (edit
, edit
->curs1
) == edit_eol (edit
, start_mark
)) && x2
> c1
&& x2
<= c2
)
2380 if (edit
->curs1
> start_mark
&& edit
->curs1
< edit_eol (edit
, end_mark
))
2384 else if (x
> c1
&& x
<= c2
)
2387 /* save current selection into buffer */
2388 copy_buf
= edit_get_block (edit
, start_mark
, end_mark
, &size
);
2390 /* remove current selection */
2391 edit_block_delete_cmd (edit
);
2393 edit
->over_col
= max (0, edit
->over_col
- b_width
);
2394 /* calculate the cursor pos after delete block */
2395 current
= edit_move_forward3 (edit
, edit_bol (edit
, edit
->curs1
), x
, 0);
2396 edit_cursor_move (edit
, current
- edit
->curs1
);
2397 edit_scroll_screen_over_cursor (edit
);
2399 /* add TWS if need before block insertion */
2400 if (option_cursor_beyond_eol
&& edit
->over_col
> 0)
2401 edit_insert_over (edit
);
2403 edit_insert_column_of_text (edit
, copy_buf
, size
, b_width
, &mark1
, &mark2
, &c1
, &c2
);
2404 edit_set_markers (edit
, mark1
, mark2
, c1
, c2
);
2408 off_t count
, count_orig
;
2410 current
= edit
->curs1
;
2411 copy_buf
= g_malloc0 (end_mark
- start_mark
);
2412 edit_cursor_move (edit
, start_mark
- edit
->curs1
);
2413 edit_scroll_screen_over_cursor (edit
);
2415 for (count
= start_mark
; count
< end_mark
; count
++)
2416 copy_buf
[end_mark
- count
- 1] = edit_delete (edit
, TRUE
);
2418 edit_scroll_screen_over_cursor (edit
);
2419 edit_cursor_move (edit
,
2420 current
- edit
->curs1
-
2421 (((current
- edit
->curs1
) > 0) ? end_mark
- start_mark
: 0));
2422 edit_scroll_screen_over_cursor (edit
);
2424 while (count
-- > start_mark
)
2425 edit_insert_ahead (edit
, copy_buf
[end_mark
- count
- 1]);
2427 edit_set_markers (edit
, edit
->curs1
, edit
->curs1
+ end_mark
- start_mark
, 0, 0);
2429 /* Place cursor at the end of text selection */
2430 if (option_cursor_after_inserted_block
)
2431 edit_cursor_move (edit
, count_orig
- start_mark
);
2434 edit_scroll_screen_over_cursor (edit
);
2436 edit
->force
|= REDRAW_PAGE
;
2439 /* --------------------------------------------------------------------------------------------- */
2440 /** returns 1 if canceelled by user */
2443 edit_block_delete_cmd (WEdit
* edit
)
2445 off_t start_mark
, end_mark
;
2446 if (eval_marks (edit
, &start_mark
, &end_mark
))
2448 edit_delete_line (edit
);
2451 return edit_block_delete (edit
);
2454 /* --------------------------------------------------------------------------------------------- */
2455 /** call with edit = 0 before shutdown to close memory leaks */
2458 edit_replace_cmd (WEdit
* edit
, int again
)
2460 /* 1 = search string, 2 = replace with */
2461 static char *saved1
= NULL
; /* saved default[123] */
2462 static char *saved2
= NULL
;
2463 char *input1
= NULL
; /* user input from the dialog */
2464 char *input2
= NULL
;
2465 GString
*input2_str
= NULL
;
2468 long times_replaced
= 0;
2469 gboolean once_found
= FALSE
;
2473 g_free (saved1
), saved1
= NULL
;
2474 g_free (saved2
), saved2
= NULL
;
2478 edit
->force
|= REDRAW_COMPLETELY
;
2480 if (again
&& !saved1
&& !saved2
)
2485 input1
= g_strdup (saved1
? saved1
: "");
2486 input2
= g_strdup (saved2
? saved2
: "");
2490 char *tmp_inp1
, *tmp_inp2
;
2492 disp1
= edit_replace_cmd__conv_to_display (saved1
? saved1
: (char *) "");
2493 disp2
= edit_replace_cmd__conv_to_display (saved2
? saved2
: (char *) "");
2495 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
2497 editcmd_dialog_replace_show (edit
, disp1
, disp2
, &input1
, &input2
);
2502 if (input1
== NULL
|| *input1
== '\0')
2504 edit
->force
= REDRAW_COMPLETELY
;
2510 input1
= edit_replace_cmd__conv_to_input (input1
);
2511 input2
= edit_replace_cmd__conv_to_input (input2
);
2515 g_free (saved1
), saved1
= g_strdup (input1
);
2516 g_free (saved2
), saved2
= g_strdup (input2
);
2518 mc_search_free (edit
->search
);
2519 edit
->search
= NULL
;
2522 input2_str
= g_string_new (input2
);
2526 edit
->search
= mc_search_new (input1
, -1);
2527 if (edit
->search
== NULL
)
2529 edit
->search_start
= edit
->curs1
;
2532 edit
->search
->search_type
= edit_search_options
.type
;
2533 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2534 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2535 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2536 edit
->search
->search_fn
= edit_search_cmd_callback
;
2537 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2538 edit_search_fix_search_start_if_selection (edit
);
2541 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
+ 1
2542 && edit_search_options
.backwards
)
2543 edit
->search_start
--;
2545 if (edit
->found_len
&& edit
->search_start
== edit
->found_start
- 1
2546 && !edit_search_options
.backwards
)
2547 edit
->search_start
++;
2553 if (!editcmd_find (edit
, &len
))
2555 if (!(edit
->search
->error
== MC_SEARCH_E_OK
||
2556 (once_found
&& edit
->search
->error
== MC_SEARCH_E_NOTFOUND
)))
2558 edit_error_dialog (_("Search"), edit
->search
->error_str
);
2564 edit
->search_start
= edit
->search
->normal_offset
;
2565 /*returns negative on not found or error in pattern */
2567 if ((edit
->search_start
>= 0) && (edit
->search_start
< edit
->last_byte
))
2572 edit
->found_start
= edit
->search_start
;
2573 i
= edit
->found_len
= len
;
2575 edit_cursor_move (edit
, edit
->search_start
- edit
->curs1
);
2576 edit_scroll_screen_over_cursor (edit
);
2578 if (edit
->replace_mode
== 0)
2583 l
= edit
->curs_row
- WIDGET (edit
)->lines
/ 3;
2585 edit_scroll_downward (edit
, l
);
2587 edit_scroll_upward (edit
, -l
);
2589 edit_scroll_screen_over_cursor (edit
);
2590 edit
->force
|= REDRAW_PAGE
;
2591 edit_render_keypress (edit
);
2593 /*so that undo stops at each query */
2594 edit_push_key_press (edit
);
2595 /* and prompt 2/3 down */
2596 disp1
= edit_replace_cmd__conv_to_display (saved1
);
2597 disp2
= edit_replace_cmd__conv_to_display (saved2
);
2598 prompt
= editcmd_dialog_replace_prompt_show (edit
, disp1
, disp2
, -1, -1);
2602 if (prompt
== B_REPLACE_ALL
)
2603 edit
->replace_mode
= 1;
2604 else if (prompt
== B_SKIP_REPLACE
)
2606 if (edit_search_options
.backwards
)
2607 edit
->search_start
--;
2609 edit
->search_start
++;
2610 continue; /* loop */
2612 else if (prompt
== B_CANCEL
)
2614 edit
->replace_mode
= -1;
2619 repl_str
= mc_search_prepare_replace_str (edit
->search
, input2_str
);
2621 if (edit
->search
->error
!= MC_SEARCH_E_OK
)
2623 edit_error_dialog (_("Replace"), edit
->search
->error_str
);
2624 g_string_free (repl_str
, TRUE
);
2628 /* delete then insert new */
2629 for (i
= 0; i
< len
; i
++)
2630 edit_delete (edit
, TRUE
);
2632 for (i
= 0; i
< repl_str
->len
; i
++)
2633 edit_insert (edit
, repl_str
->str
[i
]);
2635 edit
->found_len
= repl_str
->len
;
2636 g_string_free (repl_str
, TRUE
);
2639 /* so that we don't find the same string again */
2640 if (edit_search_options
.backwards
)
2642 edit
->search_start
--;
2646 edit
->search_start
+= edit
->found_len
+ (len
== 0 ? 1 : 0);
2648 if (edit
->search_start
>= edit
->last_byte
)
2652 edit_scroll_screen_over_cursor (edit
);
2656 /* try and find from right here for next search */
2657 edit
->search_start
= edit
->curs1
;
2658 edit_update_curs_col (edit
);
2660 edit
->force
|= REDRAW_PAGE
;
2661 edit_render_keypress (edit
);
2663 if (times_replaced
== 0)
2664 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL
, 1, _("&OK"));
2668 while (edit
->replace_mode
>= 0);
2670 edit_scroll_screen_over_cursor (edit
);
2671 edit
->force
|= REDRAW_COMPLETELY
;
2672 edit_render_keypress (edit
);
2674 if ((edit
->replace_mode
== 1) && (times_replaced
!= 0))
2675 message (D_NORMAL
, _("Replace"), _("%ld replacements made"), times_replaced
);
2680 if (input2_str
!= NULL
)
2681 g_string_free (input2_str
, TRUE
);
2684 /* --------------------------------------------------------------------------------------------- */
2687 edit_search_cmd_callback (const void *user_data
, gsize char_offset
, int *current_char
)
2689 *current_char
= edit_get_byte ((WEdit
*) user_data
, (off_t
) char_offset
);
2690 return MC_SEARCH_CB_OK
;
2693 /* --------------------------------------------------------------------------------------------- */
2696 edit_search_cmd (WEdit
* edit
, gboolean again
)
2704 else if (edit
->last_search_string
!= NULL
)
2705 edit_do_search (edit
);
2708 /* find last search string in history */
2711 history
= history_get (MC_HISTORY_SHARED_SEARCH
);
2712 if (history
!= NULL
&& history
->data
!= NULL
)
2714 edit
->last_search_string
= (char *) history
->data
;
2715 history
->data
= NULL
;
2716 history
= g_list_first (history
);
2717 g_list_foreach (history
, (GFunc
) g_free
, NULL
);
2718 g_list_free (history
);
2720 edit
->search
= mc_search_new (edit
->last_search_string
, -1);
2721 if (edit
->search
== NULL
)
2723 /* if not... then ask for an expression */
2724 g_free (edit
->last_search_string
);
2725 edit
->last_search_string
= NULL
;
2730 edit
->search
->search_type
= edit_search_options
.type
;
2731 edit
->search
->is_all_charsets
= edit_search_options
.all_codepages
;
2732 edit
->search
->is_case_sensitive
= edit_search_options
.case_sens
;
2733 edit
->search
->whole_words
= edit_search_options
.whole_words
;
2734 edit
->search
->search_fn
= edit_search_cmd_callback
;
2735 edit
->search_line_type
= edit_get_search_line_type (edit
->search
);
2736 edit_do_search (edit
);
2741 /* if not... then ask for an expression */
2742 g_free (edit
->last_search_string
);
2743 edit
->last_search_string
= NULL
;
2750 /* --------------------------------------------------------------------------------------------- */
2752 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2754 * @return TRUE if it's OK to exit, FALSE to continue editing.
2758 edit_ok_to_exit (WEdit
* edit
)
2760 char *fname
= (char *) N_("[NoName]");
2764 if (!edit
->modified
)
2767 if (edit
->filename_vpath
!= NULL
)
2768 fname
= vfs_path_to_str (edit
->filename_vpath
);
2771 fname
= g_strdup (_(fname
));
2774 fname
= g_strdup (fname
);
2777 if (!mc_global
.midnight_shutdown
)
2779 if (!edit_check_newline (edit
))
2787 msg
= g_strdup_printf (_("File %s was modified.\nSave before close?"), fname
);
2788 act
= edit_query_dialog3 (_("Close file"), msg
, _("&Yes"), _("&No"), _("&Cancel"));
2792 msg
= g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2794 act
= edit_query_dialog2 (_("Quit"), msg
, _("&Yes"), _("&No"));
2807 edit_push_markers (edit
);
2808 edit_set_markers (edit
, 0, 0, 0, 0);
2809 if (!edit_save_cmd (edit
) || mc_global
.midnight_shutdown
)
2810 return mc_global
.midnight_shutdown
;
2814 case 2: /* Cancel quit */
2822 /* --------------------------------------------------------------------------------------------- */
2823 /** save block, returns TRUE on success */
2826 edit_save_block (WEdit
* edit
, const char *filename
, off_t start
, off_t finish
)
2832 vpath
= vfs_path_from_str (filename
);
2833 file
= mc_open (vpath
, O_CREAT
| O_WRONLY
| O_TRUNC
,
2834 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
| O_BINARY
);
2835 vfs_path_free (vpath
);
2839 if (edit
->column_highlight
)
2843 r
= mc_write (file
, VERTICAL_MAGIC
, sizeof (VERTICAL_MAGIC
));
2846 unsigned char *block
, *p
;
2848 p
= block
= edit_get_block (edit
, start
, finish
, &len
);
2851 r
= mc_write (file
, p
, len
);
2866 len
= finish
- start
;
2867 buf
= g_malloc0 (TEMP_BUF_LEN
);
2868 while (start
!= finish
)
2870 end
= min (finish
, start
+ TEMP_BUF_LEN
);
2871 for (; i
< end
; i
++)
2872 buf
[i
- start
] = edit_get_byte (edit
, i
);
2873 len
-= mc_write (file
, (char *) buf
, end
- start
);
2883 /* --------------------------------------------------------------------------------------------- */
2886 edit_paste_from_history (WEdit
* edit
)
2889 edit_error_dialog (_("Error"), _("This function is not implemented"));
2892 /* --------------------------------------------------------------------------------------------- */
2895 edit_copy_to_X_buf_cmd (WEdit
* edit
)
2897 off_t start_mark
, end_mark
;
2899 if (eval_marks (edit
, &start_mark
, &end_mark
))
2901 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2903 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2906 /* try use external clipboard utility */
2907 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2912 /* --------------------------------------------------------------------------------------------- */
2915 edit_cut_to_X_buf_cmd (WEdit
* edit
)
2917 off_t start_mark
, end_mark
;
2919 if (eval_marks (edit
, &start_mark
, &end_mark
))
2921 if (!edit_save_block_to_clip_file (edit
, start_mark
, end_mark
))
2923 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2926 /* try use external clipboard utility */
2927 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
2929 edit_block_delete_cmd (edit
);
2930 edit_mark_cmd (edit
, TRUE
);
2935 /* --------------------------------------------------------------------------------------------- */
2938 edit_paste_from_X_buf_cmd (WEdit
* edit
)
2943 /* try use external clipboard utility */
2944 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
2945 tmp
= mc_config_get_full_vpath (EDIT_CLIP_FILE
);
2946 ret
= (edit_insert_file (edit
, tmp
) >= 0);
2947 vfs_path_free (tmp
);
2952 /* --------------------------------------------------------------------------------------------- */
2954 * Ask user for the line and go to that line.
2955 * Negative numbers mean line from the end (i.e. -1 is the last line).
2959 edit_goto_cmd (WEdit
* edit
)
2962 static long line
= 0; /* line as typed, saved as default */
2967 g_snprintf (s
, sizeof (s
), "%ld", line
);
2968 f
= input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE
, line
? s
: "",
2969 INPUT_COMPLETE_NONE
);
2979 l
= strtol (f
, &error
, 0);
2988 l
= edit
->total_lines
+ l
+ 2;
2989 edit_move_display (edit
, l
- WIDGET (edit
)->lines
/ 2 - 1);
2990 edit_move_to_line (edit
, l
- 1);
2991 edit
->force
|= REDRAW_COMPLETELY
;
2996 /* --------------------------------------------------------------------------------------------- */
2997 /** Return TRUE on success */
3000 edit_save_block_cmd (WEdit
* edit
)
3002 off_t start_mark
, end_mark
;
3004 gboolean ret
= FALSE
;
3006 if (eval_marks (edit
, &start_mark
, &end_mark
))
3009 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3011 input_expand_dialog (_("Save block"), _("Enter file name:"),
3012 MC_HISTORY_EDIT_SAVE_BLOCK
, tmp
, INPUT_COMPLETE_FILENAMES
);
3014 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3016 if (exp
!= NULL
&& *exp
!= '\0')
3018 if (edit_save_block (edit
, exp
, start_mark
, end_mark
))
3021 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3023 edit
->force
|= REDRAW_COMPLETELY
;
3032 /* --------------------------------------------------------------------------------------------- */
3034 /** returns TRUE on success */
3036 edit_insert_file_cmd (WEdit
* edit
)
3040 gboolean ret
= FALSE
;
3042 tmp
= mc_config_get_full_path (EDIT_CLIP_FILE
);
3043 exp
= input_expand_dialog (_("Insert file"), _("Enter file name:"),
3044 MC_HISTORY_EDIT_INSERT_FILE
, tmp
, INPUT_COMPLETE_FILENAMES
);
3047 edit_push_undo_action (edit
, KEY_PRESS
+ edit
->start_display
);
3049 if (exp
!= NULL
&& *exp
!= '\0')
3051 vfs_path_t
*exp_vpath
;
3053 exp_vpath
= vfs_path_from_str (exp
);
3054 ret
= (edit_insert_file (edit
, exp_vpath
) >= 0);
3055 vfs_path_free (exp_vpath
);
3058 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3063 edit
->force
|= REDRAW_COMPLETELY
;
3067 /* --------------------------------------------------------------------------------------------- */
3068 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3071 edit_sort_cmd (WEdit
* edit
)
3073 static char *old
= 0;
3074 char *exp
, *tmp
, *tmp_edit_block_name
, *tmp_edit_temp_name
;
3075 off_t start_mark
, end_mark
;
3078 if (eval_marks (edit
, &start_mark
, &end_mark
))
3080 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3084 tmp
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3085 edit_save_block (edit
, tmp
, start_mark
, end_mark
);
3088 exp
= input_dialog (_("Run sort"),
3089 _("Enter sort options (see manpage) separated by whitespace:"),
3090 MC_HISTORY_EDIT_SORT
, (old
!= NULL
) ? old
: "", INPUT_COMPLETE_NONE
);
3096 tmp_edit_block_name
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
3097 tmp_edit_temp_name
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3099 g_strconcat (" sort ", exp
, " ", tmp_edit_block_name
,
3100 " > ", tmp_edit_temp_name
, (char *) NULL
);
3101 g_free (tmp_edit_temp_name
);
3102 g_free (tmp_edit_block_name
);
3108 if (e
== -1 || e
== 127)
3110 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3115 sprintf (q
, "%d ", e
);
3116 tmp
= g_strdup_printf (_("Sort returned non-zero: %s"), q
);
3117 edit_error_dialog (_("Sort"), tmp
);
3123 edit
->force
|= REDRAW_COMPLETELY
;
3125 if (edit_block_delete_cmd (edit
))
3129 vfs_path_t
*tmp_vpath
;
3131 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3132 edit_insert_file (edit
, tmp_vpath
);
3133 vfs_path_free (tmp_vpath
);
3138 /* --------------------------------------------------------------------------------------------- */
3140 * Ask user for a command, execute it and paste its output back to the
3145 edit_ext_cmd (WEdit
* edit
)
3147 char *exp
, *tmp
, *tmp_edit_temp_file
;
3151 input_dialog (_("Paste output of external command"),
3152 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD
, NULL
,
3153 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
3154 | INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_COMMANDS
|
3155 INPUT_COMPLETE_SHELL_ESC
);
3160 tmp_edit_temp_file
= mc_config_get_full_path (EDIT_TEMP_FILE
);
3161 tmp
= g_strconcat (exp
, " > ", tmp_edit_temp_file
, (char *) NULL
);
3162 g_free (tmp_edit_temp_file
);
3169 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3173 edit
->force
|= REDRAW_COMPLETELY
;
3176 vfs_path_t
*tmp_vpath
;
3178 tmp_vpath
= mc_config_get_full_vpath (EDIT_TEMP_FILE
);
3179 edit_insert_file (edit
, tmp_vpath
);
3180 vfs_path_free (tmp_vpath
);
3185 /* --------------------------------------------------------------------------------------------- */
3186 /** if block is 1, a block must be highlighted and the shell command
3187 processes it. If block is 0 the shell command is a straight system
3188 command, that just produces some output which is to be inserted */
3191 edit_block_process_cmd (WEdit
* edit
, int macro_number
)
3194 char *macros_fname
= NULL
;
3196 fname
= g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE
, macro_number
);
3197 macros_fname
= g_build_filename (mc_config_get_data_path (), fname
, (char *) NULL
);
3198 user_menu (edit
, macros_fname
, 0);
3200 g_free (macros_fname
);
3201 edit
->force
|= REDRAW_COMPLETELY
;
3204 /* --------------------------------------------------------------------------------------------- */
3207 edit_mail_dialog (WEdit
* edit
)
3210 char *tmail_subject
;
3213 static char *mail_cc_last
= 0;
3214 static char *mail_subject_last
= 0;
3215 static char *mail_to_last
= 0;
3218 quick_widget_t quick_widgets
[] = {
3220 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL
),
3221 QUICK_LABELED_INPUT (N_("To"), input_label_above
,
3222 mail_to_last
!= NULL
? mail_to_last
: "", "mail-dlg-input-3",
3223 &tmail_to
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3224 QUICK_LABELED_INPUT (N_("Subject"), input_label_above
,
3225 mail_subject_last
!= NULL
? mail_subject_last
: "", "mail-dlg-input-2",
3226 &tmail_subject
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_NONE
),
3227 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above
,
3228 mail_cc_last
!= NULL
? mail_cc_last
: "", "mail-dlg-input",
3229 &tmail_cc
, NULL
, FALSE
, FALSE
, INPUT_COMPLETE_USERNAMES
),
3230 QUICK_BUTTONS_OK_CANCEL
,
3235 quick_dialog_t qdlg
= {
3237 N_("Mail"), "[Input Line Keys]",
3238 quick_widgets
, NULL
, NULL
3241 if (quick_dialog (&qdlg
) != B_CANCEL
)
3243 g_free (mail_cc_last
);
3244 g_free (mail_subject_last
);
3245 g_free (mail_to_last
);
3246 mail_cc_last
= tmail_cc
;
3247 mail_subject_last
= tmail_subject
;
3248 mail_to_last
= tmail_to
;
3249 pipe_mail (edit
, mail_to_last
, mail_subject_last
, mail_cc_last
);
3253 /* --------------------------------------------------------------------------------------------- */
3255 /*******************/
3256 /* Word Completion */
3257 /*******************/
3260 * Complete current word using regular expression search
3261 * backwards beginning at the current cursor position.
3265 edit_complete_word_cmd (WEdit
* edit
)
3267 gsize i
, max_len
, word_len
= 0, num_compl
= 0;
3268 off_t word_start
= 0;
3269 unsigned char *bufpos
;
3271 GString
*compl[MAX_WORD_COMPLETIONS
]; /* completions */
3273 /* search start of word to be completed */
3274 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3277 /* prepare match expression */
3278 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3280 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3283 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3284 (int) word_len
, bufpos
);
3286 /* collect the possible completions */
3287 /* start search from begin to end of file */
3289 edit_collect_completions (edit
, word_start
, word_len
, match_expr
, (GString
**) & compl,
3294 /* insert completed word if there is only one match */
3297 for (i
= word_len
; i
< compl[0]->len
; i
++)
3298 edit_insert (edit
, *(compl[0]->str
+ i
));
3300 /* more than one possible completion => ask the user */
3303 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3304 /* !!! pressed again the selection dialog pops up, but that !!! */
3305 /* !!! seems to require a further internal state !!! */
3308 /* let the user select the preferred completion */
3309 editcmd_dialog_completion_show (edit
, max_len
, word_len
, (GString
**) & compl,
3314 g_free (match_expr
);
3315 /* release memory before return */
3316 for (i
= 0; i
< num_compl
; i
++)
3317 g_string_free (compl[i
], TRUE
);
3320 /* --------------------------------------------------------------------------------------------- */
3324 edit_select_codepage_cmd (WEdit
* edit
)
3326 if (do_select_codepage ())
3327 edit_set_codeset (edit
);
3329 edit
->force
= REDRAW_PAGE
;
3330 widget_redraw (WIDGET (edit
));
3334 /* --------------------------------------------------------------------------------------------- */
3337 edit_insert_literal_cmd (WEdit
* edit
)
3339 int char_for_insertion
;
3341 char_for_insertion
= editcmd_dialog_raw_key_query (_("Insert literal"),
3342 _("Press any key:"), FALSE
);
3343 edit_execute_key_command (edit
, -1, ascii_alpha_to_cntrl (char_for_insertion
));
3346 /* --------------------------------------------------------------------------------------------- */
3349 edit_begin_end_macro_cmd (WEdit
* edit
)
3351 /* edit is a pointer to the widget */
3354 unsigned long command
= macro_index
< 0 ? CK_MacroStartRecord
: CK_MacroStopRecord
;
3355 edit_execute_key_command (edit
, command
, -1);
3359 /* --------------------------------------------------------------------------------------------- */
3362 edit_begin_end_repeat_cmd (WEdit
* edit
)
3364 /* edit is a pointer to the widget */
3367 unsigned long command
= macro_index
< 0 ? CK_RepeatStartRecord
: CK_RepeatStopRecord
;
3368 edit_execute_key_command (edit
, command
, -1);
3372 /* --------------------------------------------------------------------------------------------- */
3375 edit_load_forward_cmd (WEdit
* edit
)
3378 && edit_query_dialog2 (_("Warning"),
3379 _("Current text was modified without a file save.\n"
3380 "Continue discards these changes"), _("C&ontinue"),
3383 edit
->force
|= REDRAW_COMPLETELY
;
3387 if (edit_stack_iterator
+ 1 >= MAX_HISTORY_MOVETO
)
3390 if (edit_history_moveto
[edit_stack_iterator
+ 1].line
< 1)
3393 edit_stack_iterator
++;
3394 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3395 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3396 edit_history_moveto
[edit_stack_iterator
].line
);
3401 /* --------------------------------------------------------------------------------------------- */
3404 edit_load_back_cmd (WEdit
* edit
)
3407 && edit_query_dialog2 (_("Warning"),
3408 _("Current text was modified without a file save.\n"
3409 "Continue discards these changes"), _("C&ontinue"),
3412 edit
->force
|= REDRAW_COMPLETELY
;
3416 /* we are in the bottom of the stack, NO WAY! */
3417 if (edit_stack_iterator
== 0)
3420 edit_stack_iterator
--;
3421 if (edit_history_moveto
[edit_stack_iterator
].filename_vpath
!= NULL
)
3422 return edit_reload_line (edit
, edit_history_moveto
[edit_stack_iterator
].filename_vpath
,
3423 edit_history_moveto
[edit_stack_iterator
].line
);
3428 /* --------------------------------------------------------------------------------------------- */
3431 edit_get_match_keyword_cmd (WEdit
* edit
)
3433 gsize word_len
= 0, max_len
= 0;
3436 off_t word_start
= 0;
3437 unsigned char *bufpos
;
3441 char *tagfile
= NULL
;
3443 etags_hash_t def_hash
[MAX_DEFINITIONS
];
3445 for (i
= 0; i
< MAX_DEFINITIONS
; i
++)
3447 def_hash
[i
].filename
= NULL
;
3450 /* search start of word to be completed */
3451 if (!edit_find_word_start (edit
, &word_start
, &word_len
))
3454 /* prepare match expression */
3455 bufpos
= &edit
->buffers1
[word_start
>> S_EDIT_BUF_SIZE
][word_start
& M_EDIT_BUF_SIZE
];
3456 match_expr
= g_strdup_printf ("%.*s", (int) word_len
, bufpos
);
3458 ptr
= g_get_current_dir ();
3459 path
= g_strconcat (ptr
, G_DIR_SEPARATOR_S
, (char *) NULL
);
3462 /* Recursive search file 'TAGS' in parent dirs */
3465 ptr
= g_path_get_dirname (path
);
3469 tagfile
= mc_build_filename (path
, TAGS_NAME
, (char *) NULL
);
3470 if (exist_file (tagfile
))
3473 while (strcmp (path
, G_DIR_SEPARATOR_S
) != 0);
3478 etags_set_definition_hash (tagfile
, path
, match_expr
, (etags_hash_t
*) & def_hash
);
3483 max_len
= MAX_WIDTH_DEF_DIALOG
;
3487 editcmd_dialog_select_definition_show (edit
, match_expr
, max_len
, word_len
,
3488 (etags_hash_t
*) & def_hash
, num_def
);
3490 g_free (match_expr
);
3493 /* --------------------------------------------------------------------------------------------- */
3497 edit_suggest_current_word (WEdit
* edit
)
3501 off_t word_start
= 0;
3502 int retval
= B_SKIP_WORD
;
3505 /* search start of word to spell check */
3506 match_word
= edit_get_word_from_pos (edit
, edit
->curs1
, &word_start
, &word_len
, &cut_len
);
3509 if (mc_global
.source_codepage
>= 0 && (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3513 tmp_word
= str_convert_to_display (match_word
);
3514 g_free (match_word
);
3515 match_word
= g_string_free (tmp_word
, FALSE
);
3518 if (!aspell_check (match_word
, (int) word_len
))
3523 suggest
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3525 res
= aspell_suggest (suggest
, match_word
, (int) word_len
);
3528 char *new_word
= NULL
;
3530 edit
->found_start
= word_start
;
3531 edit
->found_len
= word_len
;
3532 edit
->force
|= REDRAW_PAGE
;
3533 edit_scroll_screen_over_cursor (edit
);
3534 edit_render_keypress (edit
);
3536 retval
= spell_dialog_spell_suggest_show (edit
, match_word
, &new_word
, suggest
);
3537 edit_cursor_move (edit
, word_len
- cut_len
);
3539 if (retval
== B_ENTER
&& new_word
!= NULL
)
3545 if (mc_global
.source_codepage
>= 0 &&
3546 (mc_global
.source_codepage
!= mc_global
.display_codepage
))
3550 tmp_word
= str_convert_to_input (new_word
);
3552 new_word
= g_string_free (tmp_word
, FALSE
);
3556 for (i
= 0; i
< word_len
; i
++)
3557 edit_backspace (edit
, TRUE
);
3558 for (; *new_word
; new_word
++)
3559 edit_insert (edit
, *new_word
);
3562 else if (retval
== B_ADD_WORD
&& match_word
!= NULL
)
3563 aspell_add_to_dict (match_word
, (int) word_len
);
3566 g_array_free (suggest
, TRUE
);
3567 edit
->found_start
= 0;
3568 edit
->found_len
= 0;
3570 g_free (match_word
);
3574 /* --------------------------------------------------------------------------------------------- */
3577 edit_spellcheck_file (WEdit
* edit
)
3579 if (edit
->curs_line
> 0)
3581 edit_cursor_move (edit
, -edit
->curs1
);
3582 edit_move_to_prev_col (edit
, 0);
3583 edit_update_curs_row (edit
);
3590 c2
= edit_get_byte (edit
, edit
->curs1
);
3594 if (edit
->curs1
>= edit
->last_byte
)
3598 edit_cursor_move (edit
, 1);
3599 c2
= edit_get_byte (edit
, edit
->curs1
);
3601 while (is_break_char (c1
) || is_break_char (c2
));
3603 while (edit_suggest_current_word (edit
) != B_CANCEL
);
3606 /* --------------------------------------------------------------------------------------------- */
3609 edit_set_spell_lang (void)
3613 lang_list
= g_array_new (TRUE
, FALSE
, sizeof (char *));
3614 if (aspell_get_lang_list (lang_list
) != 0)
3618 lang
= spell_dialog_lang_list_show (lang_list
);
3620 (void) aspell_set_lang (lang
);
3622 aspell_array_clean (lang_list
);
3624 #endif /* HAVE_ASPELL */
3626 /* --------------------------------------------------------------------------------------------- */