Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / editor / editcmd.c
blobf52c188e74c022e7666508bbccd3816a79427922
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2020
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor high level editing commands
30 * \author Paul Sheer
31 * \date 1996, 1997
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
36 #include <config.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <stdlib.h>
48 #include "lib/global.h"
49 #include "lib/tty/tty.h"
50 #include "lib/tty/key.h" /* XCTRL */
51 #include "lib/mcconfig.h"
52 #include "lib/skin.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/fileloc.h"
55 #include "lib/lock.h"
56 #include "lib/util.h" /* tilde_expand() */
57 #include "lib/vfs/vfs.h"
58 #include "lib/widget.h"
59 #include "lib/event.h" /* mc_event_raise() */
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h"
62 #endif
64 #include "src/history.h"
65 #include "src/file_history.h" /* show_file_history() */
66 #include "src/setup.h" /* option_tab_spacing */
67 #ifdef HAVE_CHARSET
68 #include "src/selcodepage.h"
69 #endif
70 #include "src/keybind-defaults.h"
71 #include "src/util.h" /* check_for_default() */
73 #include "edit-impl.h"
74 #include "editwidget.h"
75 #include "editcmd_dialogs.h"
76 #ifdef HAVE_ASPELL
77 #include "spell.h"
78 #include "spell_dialogs.h"
79 #endif
80 #include "etags.h"
82 /*** global variables ****************************************************************************/
84 /* search and replace: */
85 int search_create_bookmark = FALSE;
87 /* queries on a save */
88 gboolean edit_confirm_save = TRUE;
90 /* whether we need to drop selection on copy to buffer */
91 gboolean option_drop_selection_on_copy = TRUE;
93 /*** file scope macro definitions ****************************************************************/
95 #define space_width 1
97 #define TEMP_BUF_LEN 1024
99 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
101 /*** file scope type declarations ****************************************************************/
103 typedef struct
105 simple_status_msg_t status_msg; /* base class */
107 gboolean first;
108 WEdit *edit;
109 off_t offset;
110 } edit_search_status_msg_t;
112 /*** file scope variables ************************************************************************/
114 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 static int
121 edit_search_status_update_cb (status_msg_t * sm)
123 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
124 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
125 Widget *wd = WIDGET (sm->dlg);
127 if (verbose)
128 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
129 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
130 else
131 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
133 if (esm->first)
135 int wd_width;
136 Widget *lw = WIDGET (ssm->label);
138 wd_width = MAX (wd->cols, lw->cols + 6);
139 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
140 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
141 esm->first = FALSE;
144 return status_msg_common_update (sm);
147 /* --------------------------------------------------------------------------------------------- */
149 static cb_ret_t
150 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
152 switch (msg)
154 case MSG_CHANGED_FOCUS:
155 if (sender != NULL && sender->id == edit_save_mode_radio_id)
157 Widget *ww;
159 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
160 widget_disable (ww, RADIO (sender)->sel != 2);
161 return MSG_HANDLED;
164 return MSG_NOT_HANDLED;
166 default:
167 return dlg_default_callback (w, sender, msg, parm, data);
171 /* --------------------------------------------------------------------------------------------- */
173 /* If 0 (quick save) then a) create/truncate <filename> file,
174 b) save to <filename>;
175 if 1 (safe save) then a) save to <tempnam>,
176 b) rename <tempnam> to <filename>;
177 if 2 (do backups) then a) save to <tempnam>,
178 b) rename <filename> to <filename.backup_ext>,
179 c) rename <tempnam> to <filename>. */
181 /* returns 0 on error, -1 on abort */
183 static int
184 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
186 char *p;
187 gchar *tmp;
188 off_t filelen = 0;
189 int this_save_mode, rv, fd = -1;
190 vfs_path_t *real_filename_vpath;
191 vfs_path_t *savename_vpath = NULL;
192 const char *start_filename;
193 const vfs_path_element_t *vpath_element;
194 struct stat sb;
196 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
197 if (vpath_element == NULL)
198 return 0;
200 start_filename = vpath_element->path;
201 if (*start_filename == '\0')
202 return 0;
204 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
205 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
206 else
207 real_filename_vpath = vfs_path_clone (filename_vpath);
209 this_save_mode = option_save_mode;
210 if (this_save_mode != EDIT_QUICK_SAVE)
212 if (!vfs_file_is_local (real_filename_vpath)
213 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
216 * The file does not exists yet, so no safe save or
217 * backup are necessary.
219 this_save_mode = EDIT_QUICK_SAVE;
221 if (fd != -1)
222 mc_close (fd);
225 rv = mc_stat (real_filename_vpath, &sb);
226 if (rv == 0)
228 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
230 rv = edit_query_dialog3 (_("Warning"),
231 _("File has hard-links. Detach before saving?"),
232 _("&Yes"), _("&No"), _("&Cancel"));
233 switch (rv)
235 case 0:
236 this_save_mode = EDIT_SAFE_SAVE;
237 MC_FALLTHROUGH;
238 case 1:
239 edit->skip_detach_prompt = 1;
240 break;
241 default:
242 vfs_path_free (real_filename_vpath);
243 return -1;
247 /* Prevent overwriting changes from other editor sessions. */
248 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
250 /* The default action is "Cancel". */
251 query_set_sel (1);
253 rv = edit_query_dialog2 (_("Warning"),
254 _("The file has been modified in the meantime. Save anyway?"),
255 _("&Yes"), _("&Cancel"));
256 if (rv != 0)
258 vfs_path_free (real_filename_vpath);
259 return -1;
264 if (this_save_mode != EDIT_QUICK_SAVE)
266 char *savedir, *saveprefix;
268 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
269 if (savedir == NULL)
270 savedir = g_strdup (".");
272 /* Token-related function never return leading slash, so we need add it manually */
273 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", (char *) NULL);
274 g_free (savedir);
275 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
276 g_free (saveprefix);
277 if (savename_vpath == NULL)
279 vfs_path_free (real_filename_vpath);
280 return 0;
282 /* FIXME:
283 * Close for now because mc_mkstemps use pure open system call
284 * to create temporary file and it needs to be reopened by
285 * VFS-aware mc_open().
287 close (fd);
289 else
290 savename_vpath = vfs_path_clone (real_filename_vpath);
292 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
293 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
295 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
296 if (fd == -1)
297 goto error_save;
299 /* pipe save */
300 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
301 if (p != NULL)
303 FILE *file;
305 mc_close (fd);
306 file = (FILE *) popen (p, "w");
308 if (file)
310 filelen = edit_write_stream (edit, file);
311 #if 1
312 pclose (file);
313 #else
314 if (pclose (file) != 0)
316 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
317 edit_error_dialog (_("Error"), tmp);
318 g_free (tmp);
319 g_free (p);
320 goto error_save;
322 #endif
324 else
326 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
327 edit_error_dialog (_("Error"), get_sys_error (tmp));
328 g_free (p);
329 g_free (tmp);
330 goto error_save;
332 g_free (p);
334 else if (edit->lb == LB_ASIS)
335 { /* do not change line breaks */
336 filelen = edit_buffer_write_file (&edit->buffer, fd);
338 if (filelen != edit->buffer.size)
340 mc_close (fd);
341 goto error_save;
343 if (mc_close (fd) != 0)
344 goto error_save;
345 /* Update the file information, especially the mtime. */
346 if (mc_stat (savename_vpath, &edit->stat1) == -1)
347 goto error_save;
349 else
350 { /* change line breaks */
351 FILE *file;
352 const vfs_path_element_t *path_element;
354 mc_close (fd);
356 path_element = vfs_path_get_by_index (savename_vpath, -1);
357 file = (FILE *) fopen (path_element->path, "w");
358 if (file != NULL)
360 filelen = edit_write_stream (edit, file);
361 fclose (file);
363 else
365 char *msg;
367 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
368 edit_error_dialog (_("Error"), msg);
369 g_free (msg);
370 goto error_save;
374 if (filelen != edit->buffer.size)
375 goto error_save;
377 if (this_save_mode == EDIT_DO_BACKUP)
379 char *tmp_store_filename;
380 vfs_path_element_t *last_vpath_element;
381 vfs_path_t *tmp_vpath;
382 gboolean ok;
384 g_assert (option_backup_ext != NULL);
386 /* add backup extension to the path */
387 tmp_vpath = vfs_path_clone (real_filename_vpath);
388 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
389 tmp_store_filename = last_vpath_element->path;
390 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
391 g_free (tmp_store_filename);
393 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
394 vfs_path_free (tmp_vpath);
395 if (!ok)
396 goto error_save;
399 if (this_save_mode != EDIT_QUICK_SAVE)
400 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
401 goto error_save;
403 vfs_path_free (real_filename_vpath);
404 vfs_path_free (savename_vpath);
405 return 1;
406 error_save:
407 /* FIXME: Is this safe ?
408 * if (this_save_mode != EDIT_QUICK_SAVE)
409 * mc_unlink (savename);
411 vfs_path_free (real_filename_vpath);
412 vfs_path_free (savename_vpath);
413 return 0;
416 /* --------------------------------------------------------------------------------------------- */
418 static gboolean
419 edit_check_newline (const edit_buffer_t * buf)
421 return !(option_check_nl_at_eof && buf->size > 0
422 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
423 && edit_query_dialog2 (_("Warning"),
424 _("The file you are saving does not end with a newline."),
425 _("C&ontinue"), _("&Cancel")) != 0);
428 /* --------------------------------------------------------------------------------------------- */
430 static vfs_path_t *
431 edit_get_save_file_as (WEdit * edit)
433 static LineBreaks cur_lb = LB_ASIS;
434 char *filename_res;
435 vfs_path_t *ret_vpath = NULL;
437 const char *lb_names[LB_NAMES] = {
438 N_("&Do not change"),
439 N_("&Unix format (LF)"),
440 N_("&Windows/DOS format (CR LF)"),
441 N_("&Macintosh format (CR)")
444 quick_widget_t quick_widgets[] = {
445 /* *INDENT-OFF* */
446 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
447 vfs_path_as_str (edit->filename_vpath), "save-as",
448 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
449 QUICK_SEPARATOR (TRUE),
450 QUICK_LABEL (N_("Change line breaks to:"), NULL),
451 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
452 QUICK_BUTTONS_OK_CANCEL,
453 QUICK_END
454 /* *INDENT-ON* */
457 quick_dialog_t qdlg = {
458 -1, -1, 64,
459 N_("Save As"), "[Save File As]",
460 quick_widgets, NULL, NULL
463 if (quick_dialog (&qdlg) != B_CANCEL)
465 char *fname;
467 edit->lb = cur_lb;
468 fname = tilde_expand (filename_res);
469 g_free (filename_res);
470 ret_vpath = vfs_path_from_str (fname);
471 g_free (fname);
474 return ret_vpath;
477 /* --------------------------------------------------------------------------------------------- */
479 /** returns TRUE on success */
481 static gboolean
482 edit_save_cmd (WEdit * edit)
484 int res, save_lock = 0;
486 if (!edit->locked && !edit->delete_file)
487 save_lock = lock_file (edit->filename_vpath);
488 res = edit_save_file (edit, edit->filename_vpath);
490 /* Maintain modify (not save) lock on failure */
491 if ((res > 0 && edit->locked) || save_lock)
492 edit->locked = unlock_file (edit->filename_vpath);
494 /* On failure try 'save as', it does locking on its own */
495 if (res == 0)
496 return edit_save_as_cmd (edit);
497 edit->force |= REDRAW_COMPLETELY;
498 if (res > 0)
500 edit->delete_file = 0;
501 edit->modified = 0;
504 return TRUE;
507 /* --------------------------------------------------------------------------------------------- */
509 * Load file content
511 * @param h screen the owner of editor window
512 * @param vpath vfs file path
513 * @return TRUE if file content was successfully loaded, FALSE otherwise
516 static inline gboolean
517 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
519 Widget *w = WIDGET (h);
521 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
524 /* --------------------------------------------------------------------------------------------- */
526 static void
527 edit_delete_column_of_text (WEdit * edit)
529 off_t p, q, r;
530 off_t m1, m2;
531 off_t n;
532 long b, c, d;
534 eval_marks (edit, &m1, &m2);
535 n = edit_buffer_get_forward_offset (&edit->buffer, m1, 0, m2) + 1;
536 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
537 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
538 b = MAX (MIN (c, d), MIN (edit->column1, edit->column2));
539 c = MAX (c, MAX (edit->column1, edit->column2));
541 while (n--)
543 r = edit_buffer_get_current_bol (&edit->buffer);
544 p = edit_move_forward3 (edit, r, b, 0);
545 q = edit_move_forward3 (edit, r, c, 0);
546 if (p < m1)
547 p = m1;
548 if (q > m2)
549 q = m2;
550 edit_cursor_move (edit, p - edit->buffer.curs1);
551 while (q > p)
553 /* delete line between margins */
554 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
555 edit_delete (edit, TRUE);
556 q--;
558 if (n)
559 /* move to next line except on the last delete */
560 edit_cursor_move (edit,
561 edit_buffer_get_forward_offset (&edit->buffer, edit->buffer.curs1, 1,
562 0) - edit->buffer.curs1);
566 /* --------------------------------------------------------------------------------------------- */
567 /** if success return 0 */
569 static int
570 edit_block_delete (WEdit * edit)
572 off_t start_mark, end_mark;
573 off_t curs_pos;
574 long curs_line, c1, c2;
576 if (!eval_marks (edit, &start_mark, &end_mark))
577 return 0;
579 if (edit->column_highlight && edit->mark2 < 0)
580 edit_mark_cmd (edit, FALSE);
581 if ((end_mark - start_mark) > option_max_undo / 2)
583 /* Warning message with a query to continue or cancel the operation */
584 if (edit_query_dialog2
585 (_("Warning"),
587 ("Block is large, you may not be able to undo this action"),
588 _("C&ontinue"), _("&Cancel")))
590 return 1;
593 c1 = MIN (edit->column1, edit->column2);
594 c2 = MAX (edit->column1, edit->column2);
595 edit->column1 = c1;
596 edit->column2 = c2;
598 edit_push_markers (edit);
600 curs_line = edit->buffer.curs_line;
602 curs_pos = edit->curs_col + edit->over_col;
604 /* move cursor to start of selection */
605 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
606 edit_scroll_screen_over_cursor (edit);
608 if (start_mark < end_mark)
610 if (edit->column_highlight)
612 off_t line_width;
614 if (edit->mark2 < 0)
615 edit_mark_cmd (edit, FALSE);
616 edit_delete_column_of_text (edit);
617 /* move cursor to the saved position */
618 edit_move_to_line (edit, curs_line);
619 /* calculate line width and cursor position before cut */
620 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
621 edit_buffer_get_current_eol (&edit->buffer));
622 if (option_cursor_beyond_eol && curs_pos > line_width)
623 edit->over_col = curs_pos - line_width;
625 else
627 off_t count;
629 for (count = start_mark; count < end_mark; count++)
630 edit_delete (edit, TRUE);
633 edit_set_markers (edit, 0, 0, 0, 0);
634 edit->force |= REDRAW_PAGE;
635 return 0;
638 /* --------------------------------------------------------------------------------------------- */
640 * Get EOL symbol for searching.
642 * @param edit editor object
643 * @return EOL symbol
646 static inline char
647 edit_search_get_current_end_line_char (const WEdit * edit)
649 switch (edit->lb)
651 case LB_MAC:
652 return '\r';
653 default:
654 return '\n';
658 /* --------------------------------------------------------------------------------------------- */
660 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
662 * @param search search object
663 * @return result of checks.
666 static edit_search_line_t
667 edit_get_search_line_type (mc_search_t * search)
669 edit_search_line_t search_line_type = 0;
671 if (search->search_type != MC_SEARCH_T_REGEX)
672 return search_line_type;
674 if (*search->original == '^')
675 search_line_type |= AT_START_LINE;
677 if (search->original[search->original_len - 1] == '$')
678 search_line_type |= AT_END_LINE;
679 return search_line_type;
682 /* --------------------------------------------------------------------------------------------- */
684 * Calculating the start position of next line.
686 * @param buf editor buffer object
687 * @param current_pos current position
688 * @param max_pos max position
689 * @param end_string_symbol end of line symbol
690 * @return start position of next line
693 static off_t
694 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
695 char end_string_symbol)
697 off_t i;
699 for (i = current_pos; i < max_pos; i++)
701 current_pos++;
702 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
703 break;
706 return current_pos;
709 /* --------------------------------------------------------------------------------------------- */
711 * Calculating the end position of previous line.
713 * @param buf editor buffer object
714 * @param current_pos current position
715 * @param end_string_symbol end of line symbol
716 * @return end position of previous line
719 static off_t
720 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
721 char end_string_symbol)
723 off_t i;
725 for (i = current_pos - 1; i >= 0; i--)
726 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
727 break;
729 return i;
732 /* --------------------------------------------------------------------------------------------- */
734 * Calculating the start position of previous line.
736 * @param buf editor buffer object
737 * @param current_pos current position
738 * @param end_string_symbol end of line symbol
739 * @return start position of previous line
742 static inline off_t
743 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
744 char end_string_symbol)
746 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
747 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
749 return (current_pos + 1);
752 /* --------------------------------------------------------------------------------------------- */
754 * Calculating the start position of current line.
756 * @param buf editor buffer object
757 * @param current_pos current position
758 * @param end_string_symbol end of line symbol
759 * @return start position of current line
762 static inline off_t
763 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
764 char end_string_symbol)
766 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
768 return (current_pos + 1);
771 /* --------------------------------------------------------------------------------------------- */
773 * Fixing (if needed) search start position if 'only in selection' option present.
775 * @param edit editor object
778 static void
779 edit_search_fix_search_start_if_selection (WEdit * edit)
781 off_t start_mark = 0;
782 off_t end_mark = 0;
784 if (!edit_search_options.only_in_selection)
785 return;
787 if (!eval_marks (edit, &start_mark, &end_mark))
788 return;
790 if (edit_search_options.backwards)
792 if (edit->search_start > end_mark || edit->search_start <= start_mark)
793 edit->search_start = end_mark;
795 else
797 if (edit->search_start < start_mark || edit->search_start >= end_mark)
798 edit->search_start = start_mark;
802 /* --------------------------------------------------------------------------------------------- */
804 static gboolean
805 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
807 WEdit *edit = esm->edit;
808 off_t search_start = edit->search_start;
809 off_t search_end;
810 off_t start_mark = 0;
811 off_t end_mark = edit->buffer.size;
812 char end_string_symbol;
814 end_string_symbol = edit_search_get_current_end_line_char (edit);
816 /* prepare for search */
817 if (edit_search_options.only_in_selection)
819 if (!eval_marks (edit, &start_mark, &end_mark))
821 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
822 return FALSE;
825 /* fix the start and the end of search block positions */
826 if ((edit->search_line_type & AT_START_LINE) != 0
827 && (start_mark != 0
828 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
829 start_mark =
830 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
831 end_string_symbol);
833 if ((edit->search_line_type & AT_END_LINE) != 0
834 && (end_mark - 1 != edit->buffer.size
835 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
836 end_mark =
837 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
839 if (start_mark >= end_mark)
841 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
842 return FALSE;
845 else if (edit_search_options.backwards)
846 end_mark = MAX (1, edit->buffer.curs1) - 1;
848 /* search */
849 if (edit_search_options.backwards)
851 /* backward search */
852 search_end = end_mark;
854 if ((edit->search_line_type & AT_START_LINE) != 0)
855 search_start =
856 edit_calculate_start_of_current_line (&edit->buffer, search_start,
857 end_string_symbol);
859 while (search_start >= start_mark)
861 gboolean ok;
863 if (search_end > (off_t) (search_start + edit->search->original_len)
864 && mc_search_is_fixed_search_str (edit->search))
865 search_end = search_start + edit->search->original_len;
867 ok = mc_search_run (edit->search, (void *) esm, search_start, search_end, len);
869 if (ok && edit->search->normal_offset == search_start)
870 return TRUE;
872 /* We abort the search in case of a pattern error, or if the user aborts
873 the search. In other words: in all cases except "string not found". */
874 if (!ok && edit->search->error != MC_SEARCH_E_NOTFOUND)
875 return FALSE;
877 if ((edit->search_line_type & AT_START_LINE) != 0)
878 search_start =
879 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
880 end_string_symbol);
881 else
882 search_start--;
885 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
886 return FALSE;
889 /* forward search */
890 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
891 search_start =
892 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
893 end_string_symbol);
895 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
898 /* --------------------------------------------------------------------------------------------- */
900 static char *
901 edit_replace_cmd__conv_to_display (const char *str)
903 #ifdef HAVE_CHARSET
904 GString *tmp;
906 tmp = str_convert_to_display (str);
907 if (tmp != NULL)
909 if (tmp->len != 0)
910 return g_string_free (tmp, FALSE);
911 g_string_free (tmp, TRUE);
913 #endif
914 return g_strdup (str);
917 /* --------------------------------------------------------------------------------------------- */
919 static char *
920 edit_replace_cmd__conv_to_input (char *str)
922 #ifdef HAVE_CHARSET
923 GString *tmp;
925 tmp = str_convert_to_input (str);
926 if (tmp->len != 0)
927 return g_string_free (tmp, FALSE);
928 g_string_free (tmp, TRUE);
929 #endif
930 return g_strdup (str);
933 /* --------------------------------------------------------------------------------------------- */
935 static void
936 edit_show_search_error (const WEdit * edit, const char *title)
938 if (edit->search->error == MC_SEARCH_E_NOTFOUND)
939 edit_query_dialog (title, _(STR_E_NOTFOUND));
940 else if (edit->search->error_str != NULL)
941 edit_query_dialog (title, edit->search->error_str);
944 /* --------------------------------------------------------------------------------------------- */
946 static void
947 edit_do_search (WEdit * edit)
949 edit_search_status_msg_t esm;
950 gsize len = 0;
952 if (edit->search == NULL)
953 edit->search_start = edit->buffer.curs1;
955 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
957 esm.first = TRUE;
958 esm.edit = edit;
959 esm.offset = edit->search_start;
961 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
962 edit_search_status_update_cb, NULL);
964 if (search_create_bookmark)
966 gboolean found = FALSE;
967 long l = 0, l_last = -1;
968 long q = 0;
970 search_create_bookmark = FALSE;
971 book_mark_flush (edit, -1);
973 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
975 if (!found)
976 edit->search_start = edit->search->normal_offset;
977 found = TRUE;
978 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
979 if (l != l_last)
981 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
983 l_last = l;
984 q = edit->search->normal_offset + 1;
987 if (!found)
988 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
989 else
990 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
992 else
994 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
995 && edit_search_options.backwards)
996 edit->search_start--;
998 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
999 && !edit_search_options.backwards)
1000 edit->search_start++;
1002 if (editcmd_find (&esm, &len))
1004 edit->found_start = edit->search_start = edit->search->normal_offset;
1005 edit->found_len = len;
1006 edit->over_col = 0;
1007 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1008 edit_scroll_screen_over_cursor (edit);
1009 if (edit_search_options.backwards)
1010 edit->search_start--;
1011 else
1012 edit->search_start++;
1014 else
1016 edit->search_start = edit->buffer.curs1;
1017 edit_show_search_error (edit, _("Search"));
1021 status_msg_deinit (STATUS_MSG (&esm));
1023 edit->force |= REDRAW_COMPLETELY;
1024 edit_scroll_screen_over_cursor (edit);
1027 /* --------------------------------------------------------------------------------------------- */
1029 static void
1030 edit_search (WEdit * edit)
1032 if (editcmd_dialog_search_show (edit))
1034 edit->search_line_type = edit_get_search_line_type (edit->search);
1035 edit_search_fix_search_start_if_selection (edit);
1036 edit_do_search (edit);
1040 /* --------------------------------------------------------------------------------------------- */
1041 /** Return a null terminated length of text. Result must be g_free'd */
1043 static unsigned char *
1044 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1046 unsigned char *s, *r;
1048 r = s = g_malloc0 (finish - start + 1);
1049 if (edit->column_highlight)
1051 *l = 0;
1052 /* copy from buffer, excluding chars that are out of the column 'margins' */
1053 while (start < finish)
1055 int c;
1056 off_t x;
1058 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1059 c = edit_buffer_get_byte (&edit->buffer, start);
1060 if ((x >= edit->column1 && x < edit->column2)
1061 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1063 *s++ = c;
1064 (*l)++;
1066 start++;
1069 else
1071 *l = finish - start;
1072 while (start < finish)
1073 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1075 *s = '\0';
1076 return r;
1079 /* --------------------------------------------------------------------------------------------- */
1080 /** copies a block to clipboard file */
1082 static gboolean
1083 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1085 gboolean ret;
1086 gchar *tmp;
1088 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1089 ret = edit_save_block (edit, tmp, start, finish);
1090 g_free (tmp);
1091 return ret;
1094 /* --------------------------------------------------------------------------------------------- */
1096 static void
1097 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1099 FILE *p = 0;
1100 char *s;
1102 to = name_quote (to, FALSE);
1103 subject = name_quote (subject, FALSE);
1104 cc = name_quote (cc, FALSE);
1105 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1106 g_free (to);
1107 g_free (subject);
1108 g_free (cc);
1110 if (s != NULL)
1112 p = popen (s, "w");
1113 g_free (s);
1116 if (p != NULL)
1118 off_t i;
1120 for (i = 0; i < buf->size; i++)
1121 fputc (edit_buffer_get_byte (buf, i), p);
1122 pclose (p);
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** find first character of current word */
1129 static gboolean
1130 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1132 int c;
1133 off_t i;
1135 /* return if at begin of file */
1136 if (buf->curs1 <= 0)
1137 return FALSE;
1139 c = edit_buffer_get_previous_byte (buf);
1140 /* return if not at end or in word */
1141 if (is_break_char (c))
1142 return FALSE;
1144 /* search start of word to be completed */
1145 for (i = 1;; i++)
1147 int last;
1149 last = c;
1150 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1152 if (is_break_char (c))
1154 /* return if word starts with digit */
1155 if (isdigit (last))
1156 return FALSE;
1158 break;
1162 /* success */
1163 *word_start = buf->curs1 - i; /* start found */
1164 *word_len = (gsize) i;
1165 return TRUE;
1168 /* --------------------------------------------------------------------------------------------- */
1170 * Get current word under cursor
1172 * @param esm status message window
1173 * @param srch mc_search object
1174 * @param word_start start word position
1176 * @return newly allocated string or NULL if no any words under cursor
1179 static char *
1180 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1181 off_t word_start)
1183 WEdit *edit = esm->edit;
1184 gsize len = 0;
1185 off_t i;
1186 GString *temp;
1188 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1189 return NULL;
1191 temp = g_string_sized_new (len);
1193 for (i = 0; i < (off_t) len; i++)
1195 int chr;
1197 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1198 if (!isspace (chr))
1199 g_string_append_c (temp, chr);
1202 return g_string_free (temp, temp->len == 0);
1205 /* --------------------------------------------------------------------------------------------- */
1206 /** collect the possible completions */
1208 static gsize
1209 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1210 char *match_expr, GString ** compl, gsize * num)
1212 gsize len = 0;
1213 gsize max_len = 0;
1214 gsize i;
1215 int skip;
1216 GString *temp;
1217 mc_search_t *srch;
1218 off_t last_byte, start = -1;
1219 char *current_word;
1220 gboolean entire_file;
1221 edit_search_status_msg_t esm;
1223 #ifdef HAVE_CHARSET
1224 srch = mc_search_new (match_expr, cp_source);
1225 #else
1226 srch = mc_search_new (match_expr, NULL);
1227 #endif
1228 if (srch == NULL)
1229 return 0;
1231 entire_file =
1232 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
1233 "editor_wordcompletion_collect_entire_file", 0);
1235 last_byte = entire_file ? edit->buffer.size : word_start;
1237 srch->search_type = MC_SEARCH_T_REGEX;
1238 srch->is_case_sensitive = TRUE;
1239 srch->search_fn = edit_search_cmd_callback;
1240 srch->update_fn = edit_search_update_callback;
1242 esm.first = TRUE;
1243 esm.edit = edit;
1244 esm.offset = entire_file ? 0 : word_start;
1246 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1247 edit_search_status_update_cb, NULL);
1249 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1251 temp = g_string_new ("");
1253 /* collect max MAX_WORD_COMPLETIONS completions */
1254 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1256 g_string_set_size (temp, 0);
1257 start = srch->normal_offset;
1259 /* add matched completion if not yet added */
1260 for (i = 0; i < len; i++)
1262 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1263 if (isspace (skip))
1264 continue;
1266 /* skip current word */
1267 if (start + (off_t) i == word_start)
1268 break;
1270 g_string_append_c (temp, skip);
1273 if (temp->len == 0)
1274 continue;
1276 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1277 continue;
1279 skip = 0;
1281 for (i = 0; i < *num; i++)
1283 if (strncmp
1284 ((char *) &compl[i]->str[word_len],
1285 (char *) &temp->str[word_len], MAX (len, compl[i]->len) - word_len) == 0)
1287 GString *this = compl[i];
1288 for (++i; i < *num; i++)
1289 compl[i - 1] = compl[i];
1290 compl[*num - 1] = this;
1291 skip = 1;
1292 break; /* skip it, already added */
1295 if (skip != 0)
1296 continue;
1298 if (*num == MAX_WORD_COMPLETIONS)
1300 g_string_free (compl[0], TRUE);
1301 for (i = 1; i < *num; i++)
1302 compl[i - 1] = compl[i];
1303 (*num)--;
1305 #ifdef HAVE_CHARSET
1307 GString *recoded;
1309 recoded = str_convert_to_display (temp->str);
1310 if (recoded->len != 0)
1311 g_string_assign (temp, recoded->str);
1313 g_string_free (recoded, TRUE);
1315 #endif
1316 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1317 start += len;
1319 /* note the maximal length needed for the completion dialog */
1320 if (len > max_len)
1321 max_len = len;
1324 status_msg_deinit (STATUS_MSG (&esm));
1325 mc_search_free (srch);
1326 g_string_free (temp, TRUE);
1327 g_free (current_word);
1329 return max_len;
1332 /* --------------------------------------------------------------------------------------------- */
1334 static void
1335 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1336 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1338 off_t i, cursor;
1339 long col;
1341 cursor = edit->buffer.curs1;
1342 col = edit_get_col (edit);
1344 for (i = 0; i < size; i++)
1346 if (data[i] != '\n')
1347 edit_insert (edit, data[i]);
1348 else
1349 { /* fill in and move to next line */
1350 long l;
1351 off_t p;
1353 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1355 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1356 edit_insert (edit, ' ');
1358 for (p = edit->buffer.curs1;; p++)
1360 if (p == edit->buffer.size)
1362 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1363 edit_insert_ahead (edit, '\n');
1364 p++;
1365 break;
1367 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1369 p++;
1370 break;
1373 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1375 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1376 edit_insert (edit, ' ');
1380 *col1 = col;
1381 *col2 = col + width;
1382 *start_pos = cursor;
1383 *end_pos = edit->buffer.curs1;
1384 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1387 /* --------------------------------------------------------------------------------------------- */
1389 static int
1390 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1392 const macros_t *m1 = (const macros_t *) macro1;
1393 const macros_t *m2 = (const macros_t *) macro2;
1395 return m1->hotkey - m2->hotkey;
1398 /* --------------------------------------------------------------------------------------------- */
1400 static void
1401 edit_macro_sort_by_hotkey (void)
1403 if (macros_list != NULL && macros_list->len != 0)
1404 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1407 /* --------------------------------------------------------------------------------------------- */
1409 static gboolean
1410 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1412 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1413 macros_t *result;
1414 macros_t search_macro;
1416 (void) edit;
1418 search_macro.hotkey = hotkey;
1419 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1420 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1422 if (result != NULL && result->macro != NULL)
1424 *indx = (result - array_start);
1425 *macros = result;
1426 return TRUE;
1428 *indx = 0;
1429 return FALSE;
1432 /* --------------------------------------------------------------------------------------------- */
1433 /** returns FALSE on error */
1435 static gboolean
1436 edit_delete_macro (WEdit * edit, int hotkey)
1438 mc_config_t *macros_config = NULL;
1439 const char *section_name = "editor";
1440 gchar *macros_fname;
1441 guint indx;
1442 char *skeyname;
1443 const macros_t *macros = NULL;
1445 /* clear array of actions for current hotkey */
1446 while (edit_get_macro (edit, hotkey, &macros, &indx))
1448 if (macros->macro != NULL)
1449 g_array_free (macros->macro, TRUE);
1450 macros = NULL;
1451 g_array_remove_index (macros_list, indx);
1452 edit_macro_sort_by_hotkey ();
1455 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1456 macros_config = mc_config_init (macros_fname, FALSE);
1457 g_free (macros_fname);
1459 if (macros_config == NULL)
1460 return FALSE;
1462 skeyname = lookup_key_by_code (hotkey);
1463 while (mc_config_del_key (macros_config, section_name, skeyname))
1465 g_free (skeyname);
1466 mc_config_save_file (macros_config, NULL);
1467 mc_config_deinit (macros_config);
1468 return TRUE;
1471 /* --------------------------------------------------------------------------------------------- */
1473 * Callback for the iteration of objects in the 'editors' array.
1474 * Toggle syntax highlighting in editor object.
1476 * @param data probably WEdit object
1477 * @param user_data unused
1480 static void
1481 edit_syntax_onoff_cb (void *data, void *user_data)
1483 (void) user_data;
1485 if (edit_widget_is_editor (CONST_WIDGET (data)))
1487 WEdit *edit = (WEdit *) data;
1489 if (option_syntax_highlighting)
1490 edit_load_syntax (edit, NULL, edit->syntax_type);
1491 edit->force |= REDRAW_PAGE;
1495 /* --------------------------------------------------------------------------------------------- */
1497 * Callback for the iteration of objects in the 'editors' array.
1498 * Redraw editor object.
1500 * @param data probably WEdit object
1501 * @param user_data unused
1504 static void
1505 edit_redraw_page_cb (void *data, void *user_data)
1507 (void) user_data;
1509 if (edit_widget_is_editor (CONST_WIDGET (data)))
1510 ((WEdit *) data)->force |= REDRAW_PAGE;
1513 /* --------------------------------------------------------------------------------------------- */
1515 * Insert autocompleted word into editor.
1517 * @param edit editor object
1518 * @param completion word for completion
1519 * @param word_len offset from beginning for insert
1522 static void
1523 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1525 #ifdef HAVE_CHARSET
1526 GString *temp;
1528 temp = str_convert_to_input (completion);
1530 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1531 edit_insert (edit, *completion);
1532 g_string_free (temp, TRUE);
1533 #else
1534 for (completion += word_len; *completion != '\0'; completion++)
1535 edit_insert (edit, *completion);
1536 #endif
1539 /* --------------------------------------------------------------------------------------------- */
1540 /*** public functions ****************************************************************************/
1541 /* --------------------------------------------------------------------------------------------- */
1543 void
1544 edit_refresh_cmd (void)
1546 clr_scr ();
1547 repaint_screen ();
1548 tty_keypad (TRUE);
1551 /* --------------------------------------------------------------------------------------------- */
1553 * Toggle syntax highlighting in all editor windows.
1555 * @param h root widget for all windows
1558 void
1559 edit_syntax_onoff_cmd (WDialog * h)
1561 option_syntax_highlighting = !option_syntax_highlighting;
1562 g_list_foreach (GROUP (h)->widgets, edit_syntax_onoff_cb, NULL);
1563 dlg_draw (h);
1566 /* --------------------------------------------------------------------------------------------- */
1568 * Toggle tabs showing in all editor windows.
1570 * @param h root widget for all windows
1573 void
1574 edit_show_tabs_tws_cmd (WDialog * h)
1576 enable_show_tabs_tws = !enable_show_tabs_tws;
1577 g_list_foreach (GROUP (h)->widgets, edit_redraw_page_cb, NULL);
1578 dlg_draw (h);
1581 /* --------------------------------------------------------------------------------------------- */
1583 * Toggle right margin showing in all editor windows.
1585 * @param h root widget for all windows
1588 void
1589 edit_show_margin_cmd (WDialog * h)
1591 show_right_margin = !show_right_margin;
1592 g_list_foreach (GROUP (h)->widgets, edit_redraw_page_cb, NULL);
1593 dlg_draw (h);
1596 /* --------------------------------------------------------------------------------------------- */
1598 * Toggle line numbers showing in all editor windows.
1600 * @param h root widget for all windows
1603 void
1604 edit_show_numbers_cmd (WDialog * h)
1606 option_line_state = !option_line_state;
1607 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1608 g_list_foreach (GROUP (h)->widgets, edit_redraw_page_cb, NULL);
1609 dlg_draw (h);
1612 /* --------------------------------------------------------------------------------------------- */
1614 void
1615 edit_save_mode_cmd (void)
1617 char *str_result = NULL;
1619 const char *str[] = {
1620 N_("&Quick save"),
1621 N_("&Safe save"),
1622 N_("&Do backups with following extension:")
1625 #ifdef ENABLE_NLS
1626 size_t i;
1628 for (i = 0; i < 3; i++)
1629 str[i] = _(str[i]);
1630 #endif
1632 g_assert (option_backup_ext != NULL);
1635 quick_widget_t quick_widgets[] = {
1636 /* *INDENT-OFF* */
1637 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1638 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1639 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1640 QUICK_SEPARATOR (TRUE),
1641 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1642 QUICK_BUTTONS_OK_CANCEL,
1643 QUICK_END
1644 /* *INDENT-ON* */
1647 quick_dialog_t qdlg = {
1648 -1, -1, 38,
1649 N_("Edit Save Mode"), "[Edit Save Mode]",
1650 quick_widgets, edit_save_mode_callback, NULL
1653 if (quick_dialog (&qdlg) != B_CANCEL)
1655 g_free (option_backup_ext);
1656 option_backup_ext = str_result;
1661 /* --------------------------------------------------------------------------------------------- */
1663 void
1664 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1666 vfs_path_free (edit->filename_vpath);
1667 edit->filename_vpath = vfs_path_clone (name_vpath);
1669 if (edit->dir_vpath == NULL)
1670 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1673 /* --------------------------------------------------------------------------------------------- */
1674 /* Here we want to warn the users of overwriting an existing file,
1675 but only if they have made a change to the filename */
1676 /* returns TRUE on success */
1677 gboolean
1678 edit_save_as_cmd (WEdit * edit)
1680 /* This heads the 'Save As' dialog box */
1681 vfs_path_t *exp_vpath;
1682 int save_lock = 0;
1683 int different_filename = 0;
1685 if (!edit_check_newline (&edit->buffer))
1686 return FALSE;
1688 exp_vpath = edit_get_save_file_as (edit);
1689 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1691 if (exp_vpath != NULL)
1693 if (vfs_path_len (exp_vpath) == 0)
1694 goto ret;
1695 else
1697 int rv;
1699 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1701 int file;
1702 struct stat sb;
1704 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1706 edit_error_dialog (_("Save as"),
1707 get_sys_error (_
1708 ("Cannot save: destination is not a regular file")));
1709 goto ret;
1712 different_filename = 1;
1713 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1715 if (file != -1)
1717 /* the file exists */
1718 mc_close (file);
1719 /* Overwrite the current file or cancel the operation */
1720 if (edit_query_dialog2
1721 (_("Warning"),
1722 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1723 goto ret;
1725 else
1727 edit->stat1.st_mode |= S_IWUSR;
1729 save_lock = lock_file (exp_vpath);
1731 else
1733 /* filenames equal, check if already locked */
1734 if (!edit->locked && !edit->delete_file)
1735 save_lock = lock_file (exp_vpath);
1738 if (different_filename)
1741 * Allow user to write into saved (under another name) file
1742 * even if original file had r/o user permissions.
1744 edit->stat1.st_mode |= S_IWRITE;
1747 rv = edit_save_file (edit, exp_vpath);
1748 switch (rv)
1750 case 1:
1751 /* Successful, so unlock both files */
1752 if (different_filename)
1754 if (save_lock)
1755 unlock_file (exp_vpath);
1756 if (edit->locked)
1757 edit->locked = unlock_file (edit->filename_vpath);
1759 else
1761 if (edit->locked || save_lock)
1762 edit->locked = unlock_file (edit->filename_vpath);
1765 edit_set_filename (edit, exp_vpath);
1766 if (edit->lb != LB_ASIS)
1767 edit_reload (edit, exp_vpath);
1768 edit->modified = 0;
1769 edit->delete_file = 0;
1770 if (different_filename)
1771 edit_load_syntax (edit, NULL, edit->syntax_type);
1772 vfs_path_free (exp_vpath);
1773 edit->force |= REDRAW_COMPLETELY;
1774 return TRUE;
1775 default:
1776 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1777 MC_FALLTHROUGH;
1778 case -1:
1779 /* Failed, so maintain modify (not save) lock */
1780 if (save_lock)
1781 unlock_file (exp_vpath);
1782 break;
1787 ret:
1788 vfs_path_free (exp_vpath);
1789 edit->force |= REDRAW_COMPLETELY;
1790 return FALSE;
1793 /* {{{ Macro stuff starts here */
1794 /* --------------------------------------------------------------------------------------------- */
1796 void
1797 edit_delete_macro_cmd (WEdit * edit)
1799 int hotkey;
1801 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1803 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1804 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1807 /* --------------------------------------------------------------------------------------------- */
1809 /** returns FALSE on error */
1810 gboolean
1811 edit_execute_macro (WEdit * edit, int hotkey)
1813 gboolean res = FALSE;
1815 if (hotkey != 0)
1817 const macros_t *macros;
1818 guint indx;
1820 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1821 macros->macro != NULL && macros->macro->len != 0)
1823 guint i;
1825 edit->force |= REDRAW_PAGE;
1827 for (i = 0; i < macros->macro->len; i++)
1829 const macro_action_t *m_act;
1831 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1832 edit_execute_cmd (edit, m_act->action, m_act->ch);
1833 res = TRUE;
1838 return res;
1841 /* --------------------------------------------------------------------------------------------- */
1843 /** returns FALSE on error */
1844 gboolean
1845 edit_store_macro_cmd (WEdit * edit)
1847 int i;
1848 int hotkey;
1849 GString *marcros_string;
1850 mc_config_t *macros_config = NULL;
1851 const char *section_name = "editor";
1852 gchar *macros_fname;
1853 GArray *macros; /* current macro */
1854 int tmp_act;
1855 gboolean have_macro = FALSE;
1856 char *skeyname = NULL;
1858 hotkey =
1859 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1860 if (hotkey == ESC_CHAR)
1861 return FALSE;
1863 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1865 /* return FALSE if try assign macro into restricted hotkeys */
1866 if (tmp_act == CK_MacroStartRecord
1867 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1868 return FALSE;
1870 edit_delete_macro (edit, hotkey);
1872 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1873 macros_config = mc_config_init (macros_fname, FALSE);
1874 g_free (macros_fname);
1876 if (macros_config == NULL)
1877 return FALSE;
1879 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1881 marcros_string = g_string_sized_new (250);
1882 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1884 skeyname = lookup_key_by_code (hotkey);
1886 for (i = 0; i < macro_index; i++)
1888 macro_action_t m_act;
1889 const char *action_name;
1891 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1893 if (action_name == NULL)
1894 break;
1896 m_act.action = record_macro_buf[i].action;
1897 m_act.ch = record_macro_buf[i].ch;
1898 g_array_append_val (macros, m_act);
1899 have_macro = TRUE;
1900 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1901 (int) record_macro_buf[i].ch);
1903 if (have_macro)
1905 macros_t macro;
1906 macro.hotkey = hotkey;
1907 macro.macro = macros;
1908 g_array_append_val (macros_list, macro);
1909 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1911 else
1912 mc_config_del_key (macros_config, section_name, skeyname);
1914 g_free (skeyname);
1915 edit_macro_sort_by_hotkey ();
1917 g_string_free (marcros_string, TRUE);
1918 mc_config_save_file (macros_config, NULL);
1919 mc_config_deinit (macros_config);
1920 return TRUE;
1923 /* --------------------------------------------------------------------------------------------- */
1925 gboolean
1926 edit_repeat_macro_cmd (WEdit * edit)
1928 int i, j;
1929 char *f;
1930 long count_repeat;
1931 char *error = NULL;
1933 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1934 INPUT_COMPLETE_NONE);
1935 if (f == NULL || *f == '\0')
1937 g_free (f);
1938 return FALSE;
1941 count_repeat = strtol (f, &error, 0);
1943 if (*error != '\0')
1945 g_free (f);
1946 return FALSE;
1949 g_free (f);
1951 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1952 edit->force |= REDRAW_PAGE;
1954 for (j = 0; j < count_repeat; j++)
1955 for (i = 0; i < macro_index; i++)
1956 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1957 edit_update_screen (edit);
1958 return TRUE;
1961 /* --------------------------------------------------------------------------------------------- */
1962 /** return FALSE on error */
1964 gboolean
1965 edit_load_macro_cmd (WEdit * edit)
1967 mc_config_t *macros_config = NULL;
1968 gchar **profile_keys, **keys;
1969 gchar **values, **curr_values;
1970 const char *section_name = "editor";
1971 gchar *macros_fname;
1973 (void) edit;
1975 if (macros_list == NULL || macros_list->len != 0)
1976 return FALSE;
1978 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1979 macros_config = mc_config_init (macros_fname, TRUE);
1980 g_free (macros_fname);
1982 if (macros_config == NULL)
1983 return FALSE;
1985 keys = mc_config_get_keys (macros_config, section_name, NULL);
1987 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1989 int hotkey;
1990 gboolean have_macro = FALSE;
1991 GArray *macros;
1992 macros_t macro;
1994 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1995 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
1996 hotkey = lookup_key (*profile_keys, NULL);
1998 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2000 char **macro_pair = NULL;
2002 macro_pair = g_strsplit (*curr_values, ":", 2);
2003 if (macro_pair != NULL)
2005 macro_action_t m_act;
2006 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2007 m_act.action = 0;
2008 else
2010 m_act.action = keybind_lookup_action (macro_pair[0]);
2011 MC_PTR_FREE (macro_pair[0]);
2013 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2014 m_act.ch = -1;
2015 else
2017 m_act.ch = strtol (macro_pair[1], NULL, 0);
2018 MC_PTR_FREE (macro_pair[1]);
2020 if (m_act.action != 0)
2022 /* a shell command */
2023 if ((m_act.action / CK_PipeBlock (0)) == 1)
2025 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2026 m_act.ch = -1;
2028 g_array_append_val (macros, m_act);
2029 have_macro = TRUE;
2031 g_strfreev (macro_pair);
2032 macro_pair = NULL;
2035 if (have_macro)
2037 macro.hotkey = hotkey;
2038 macro.macro = macros;
2039 g_array_append_val (macros_list, macro);
2041 g_strfreev (values);
2043 g_strfreev (keys);
2044 mc_config_deinit (macros_config);
2045 edit_macro_sort_by_hotkey ();
2046 return TRUE;
2049 /* }}} Macro stuff end here */
2051 /* --------------------------------------------------------------------------------------------- */
2052 /** returns TRUE on success */
2054 gboolean
2055 edit_save_confirm_cmd (WEdit * edit)
2057 if (edit->filename_vpath == NULL)
2058 return edit_save_as_cmd (edit);
2060 if (!edit_check_newline (&edit->buffer))
2061 return FALSE;
2063 if (edit_confirm_save)
2065 char *f;
2066 gboolean ok;
2068 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2069 vfs_path_as_str (edit->filename_vpath));
2070 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2071 g_free (f);
2072 if (!ok)
2073 return FALSE;
2075 return edit_save_cmd (edit);
2078 /* --------------------------------------------------------------------------------------------- */
2080 * Ask file to edit and load it.
2082 * @return TRUE on success or cancel of ask.
2085 gboolean
2086 edit_load_cmd (WDialog * h)
2088 char *exp;
2089 gboolean ret = TRUE; /* possible cancel */
2091 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2092 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2093 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2095 if (exp != NULL && *exp != '\0')
2097 vfs_path_t *exp_vpath;
2099 exp_vpath = vfs_path_from_str (exp);
2100 ret = edit_load_file_from_filename (h, exp_vpath);
2101 vfs_path_free (exp_vpath);
2104 g_free (exp);
2106 return ret;
2109 /* --------------------------------------------------------------------------------------------- */
2111 * Show history od edited or viewed files and open selected file.
2113 * @return TRUE on success, FALSE otherwise.
2116 gboolean
2117 edit_load_file_from_history (WDialog * h)
2119 char *exp;
2120 int action;
2121 gboolean ret = TRUE; /* possible cancel */
2123 exp = show_file_history (CONST_WIDGET (h), &action);
2124 if (exp != NULL && (action == CK_Edit || action == CK_Enter))
2126 vfs_path_t *exp_vpath;
2128 exp_vpath = vfs_path_from_str (exp);
2129 ret = edit_load_file_from_filename (h, exp_vpath);
2130 vfs_path_free (exp_vpath);
2133 g_free (exp);
2135 return ret;
2138 /* --------------------------------------------------------------------------------------------- */
2140 * Load syntax file to edit.
2142 * @return TRUE on success
2145 gboolean
2146 edit_load_syntax_file (WDialog * h)
2148 vfs_path_t *extdir_vpath;
2149 int dir = 0;
2150 gboolean ret = FALSE;
2152 if (geteuid () == 0)
2153 dir = query_dialog (_("Syntax file edit"),
2154 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2155 _("&User"), _("&System wide"));
2157 extdir_vpath =
2158 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2159 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2161 vfs_path_free (extdir_vpath);
2162 extdir_vpath =
2163 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2166 if (dir == 0)
2168 vfs_path_t *user_syntax_file_vpath;
2170 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2171 check_for_default (extdir_vpath, user_syntax_file_vpath);
2172 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2173 vfs_path_free (user_syntax_file_vpath);
2175 else if (dir == 1)
2176 ret = edit_load_file_from_filename (h, extdir_vpath);
2178 vfs_path_free (extdir_vpath);
2180 return ret;
2183 /* --------------------------------------------------------------------------------------------- */
2185 * Load menu file to edit.
2187 * @return TRUE on success
2190 gboolean
2191 edit_load_menu_file (WDialog * h)
2193 vfs_path_t *buffer_vpath;
2194 vfs_path_t *menufile_vpath;
2195 int dir;
2196 gboolean ret;
2198 query_set_sel (1);
2199 dir = query_dialog (_("Menu edit"),
2200 _("Which menu file do you want to edit?"), D_NORMAL,
2201 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2203 menufile_vpath =
2204 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2205 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2207 vfs_path_free (menufile_vpath);
2208 menufile_vpath =
2209 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2212 switch (dir)
2214 case 0:
2215 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2216 check_for_default (menufile_vpath, buffer_vpath);
2217 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2218 break;
2220 case 1:
2221 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2222 check_for_default (menufile_vpath, buffer_vpath);
2223 break;
2225 case 2:
2226 buffer_vpath =
2227 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2228 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2230 vfs_path_free (buffer_vpath);
2231 buffer_vpath =
2232 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2234 break;
2236 default:
2237 vfs_path_free (menufile_vpath);
2238 return FALSE;
2241 ret = edit_load_file_from_filename (h, buffer_vpath);
2243 vfs_path_free (buffer_vpath);
2244 vfs_path_free (menufile_vpath);
2246 return ret;
2249 /* --------------------------------------------------------------------------------------------- */
2251 * Close window with opened file.
2253 * @return TRUE if file was closed.
2256 gboolean
2257 edit_close_cmd (WEdit * edit)
2259 gboolean ret;
2261 ret = (edit != NULL) && edit_ok_to_exit (edit);
2263 if (ret)
2265 WGroup *g = WIDGET (edit)->owner;
2266 WDialog *h = DIALOG (g);
2268 if (edit->locked != 0)
2269 unlock_file (edit->filename_vpath);
2271 group_del_widget (edit);
2272 widget_destroy (WIDGET (edit));
2274 if (edit_widget_is_editor (CONST_WIDGET (g->current->data)))
2275 edit = (WEdit *) (g->current->data);
2276 else
2278 edit = find_editor (h);
2279 if (edit != NULL)
2280 widget_select (WIDGET (edit));
2284 if (edit != NULL)
2285 edit->force |= REDRAW_COMPLETELY;
2287 return ret;
2290 /* --------------------------------------------------------------------------------------------- */
2292 if mark2 is -1 then marking is from mark1 to the cursor.
2293 Otherwise its between the markers. This handles this.
2294 Returns FALSE if no text is marked.
2297 gboolean
2298 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2300 long end_mark_curs;
2302 if (edit->mark1 == edit->mark2)
2304 *start_mark = *end_mark = 0;
2305 edit->column2 = edit->column1 = 0;
2306 return FALSE;
2309 if (edit->end_mark_curs < 0)
2310 end_mark_curs = edit->buffer.curs1;
2311 else
2312 end_mark_curs = edit->end_mark_curs;
2314 if (edit->mark2 >= 0)
2316 *start_mark = MIN (edit->mark1, edit->mark2);
2317 *end_mark = MAX (edit->mark1, edit->mark2);
2319 else
2321 *start_mark = MIN (edit->mark1, end_mark_curs);
2322 *end_mark = MAX (edit->mark1, end_mark_curs);
2323 edit->column2 = edit->curs_col + edit->over_col;
2326 if (edit->column_highlight
2327 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2328 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2330 off_t start_bol, start_eol;
2331 off_t end_bol, end_eol;
2332 long col1, col2;
2333 off_t diff1, diff2;
2335 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2336 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2337 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2338 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2339 col1 = MIN (edit->column1, edit->column2);
2340 col2 = MAX (edit->column1, edit->column2);
2342 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2343 edit_move_forward3 (edit, start_bol, col1, 0);
2344 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2345 edit_move_forward3 (edit, end_bol, col1, 0);
2347 *start_mark -= diff1;
2348 *end_mark += diff2;
2349 *start_mark = MAX (*start_mark, start_eol);
2350 *end_mark = MIN (*end_mark, end_eol);
2352 return TRUE;
2355 /* --------------------------------------------------------------------------------------------- */
2357 void
2358 edit_block_copy_cmd (WEdit * edit)
2360 off_t start_mark, end_mark, current = edit->buffer.curs1;
2361 off_t mark1 = 0, mark2 = 0;
2362 long c1 = 0, c2 = 0;
2363 off_t size;
2364 unsigned char *copy_buf;
2366 edit_update_curs_col (edit);
2367 if (!eval_marks (edit, &start_mark, &end_mark))
2368 return;
2370 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2372 /* all that gets pushed are deletes hence little space is used on the stack */
2374 edit_push_markers (edit);
2376 if (edit->column_highlight)
2378 long col_delta;
2380 col_delta = labs (edit->column2 - edit->column1);
2381 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2383 else
2385 int size_orig = size;
2387 while (size-- != 0)
2388 edit_insert_ahead (edit, copy_buf[size]);
2390 /* Place cursor at the end of text selection */
2391 if (option_cursor_after_inserted_block)
2392 edit_cursor_move (edit, size_orig);
2395 g_free (copy_buf);
2396 edit_scroll_screen_over_cursor (edit);
2398 if (edit->column_highlight)
2399 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2400 else if (start_mark < current && end_mark > current)
2401 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2403 edit->force |= REDRAW_PAGE;
2407 /* --------------------------------------------------------------------------------------------- */
2409 void
2410 edit_block_move_cmd (WEdit * edit)
2412 off_t current;
2413 unsigned char *copy_buf = NULL;
2414 off_t start_mark, end_mark;
2416 if (!eval_marks (edit, &start_mark, &end_mark))
2417 return;
2419 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2420 return;
2422 if (edit->mark2 < 0)
2423 edit_mark_cmd (edit, FALSE);
2424 edit_push_markers (edit);
2426 if (edit->column_highlight)
2428 off_t mark1, mark2;
2429 off_t size;
2430 long c1, c2, b_width;
2431 long x, x2;
2433 c1 = MIN (edit->column1, edit->column2);
2434 c2 = MAX (edit->column1, edit->column2);
2435 b_width = c2 - c1;
2437 edit_update_curs_col (edit);
2439 x = edit->curs_col;
2440 x2 = x + edit->over_col;
2442 /* do nothing when cursor inside first line of selected area */
2443 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2444 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2445 return;
2447 if (edit->buffer.curs1 > start_mark
2448 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2450 if (x > c2)
2451 x -= b_width;
2452 else if (x > c1 && x <= c2)
2453 x = c1;
2455 /* save current selection into buffer */
2456 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2458 /* remove current selection */
2459 edit_block_delete_cmd (edit);
2461 edit->over_col = MAX (0, edit->over_col - b_width);
2462 /* calculate the cursor pos after delete block */
2463 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2464 edit_cursor_move (edit, current - edit->buffer.curs1);
2465 edit_scroll_screen_over_cursor (edit);
2467 /* add TWS if need before block insertion */
2468 if (option_cursor_beyond_eol && edit->over_col > 0)
2469 edit_insert_over (edit);
2471 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2472 edit_set_markers (edit, mark1, mark2, c1, c2);
2474 else
2476 off_t count, count_orig;
2478 current = edit->buffer.curs1;
2479 copy_buf = g_malloc0 (end_mark - start_mark);
2480 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2481 edit_scroll_screen_over_cursor (edit);
2483 for (count = start_mark; count < end_mark; count++)
2484 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2486 edit_scroll_screen_over_cursor (edit);
2487 edit_cursor_move (edit,
2488 current - edit->buffer.curs1 -
2489 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2490 edit_scroll_screen_over_cursor (edit);
2491 count_orig = count;
2492 while (count-- > start_mark)
2493 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2495 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2498 /* Place cursor at the end of text selection */
2499 if (option_cursor_after_inserted_block)
2500 edit_cursor_move (edit, count_orig - start_mark);
2503 edit_scroll_screen_over_cursor (edit);
2504 g_free (copy_buf);
2505 edit->force |= REDRAW_PAGE;
2508 /* --------------------------------------------------------------------------------------------- */
2509 /** returns 1 if canceelled by user */
2512 edit_block_delete_cmd (WEdit * edit)
2514 off_t start_mark, end_mark;
2516 if (eval_marks (edit, &start_mark, &end_mark))
2517 return edit_block_delete (edit);
2519 edit_delete_line (edit);
2520 return 0;
2523 /* --------------------------------------------------------------------------------------------- */
2524 /** call with edit = 0 before shutdown to close memory leaks */
2526 void
2527 edit_replace_cmd (WEdit * edit, gboolean again)
2529 /* 1 = search string, 2 = replace with */
2530 static char *saved1 = NULL; /* saved default[123] */
2531 static char *saved2 = NULL;
2532 char *input1 = NULL; /* user input from the dialog */
2533 char *input2 = NULL;
2534 GString *input2_str = NULL;
2535 char *disp1 = NULL;
2536 char *disp2 = NULL;
2537 long times_replaced = 0;
2538 gboolean once_found = FALSE;
2539 edit_search_status_msg_t esm;
2541 if (edit == NULL)
2543 MC_PTR_FREE (saved1);
2544 MC_PTR_FREE (saved2);
2545 return;
2548 edit->force |= REDRAW_COMPLETELY;
2550 if (again && !saved1 && !saved2)
2551 again = FALSE;
2553 if (again)
2555 input1 = g_strdup (saved1 ? saved1 : "");
2556 input2 = g_strdup (saved2 ? saved2 : "");
2558 else
2560 char *tmp_inp1, *tmp_inp2;
2562 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : "");
2563 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : "");
2565 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2567 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2569 g_free (disp1);
2570 g_free (disp2);
2572 if (input1 == NULL || *input1 == '\0')
2574 edit->force = REDRAW_COMPLETELY;
2575 goto cleanup;
2578 tmp_inp1 = input1;
2579 tmp_inp2 = input2;
2580 input1 = edit_replace_cmd__conv_to_input (input1);
2581 input2 = edit_replace_cmd__conv_to_input (input2);
2582 g_free (tmp_inp1);
2583 g_free (tmp_inp2);
2585 g_free (saved1);
2586 saved1 = g_strdup (input1);
2587 g_free (saved2);
2588 saved2 = g_strdup (input2);
2590 mc_search_free (edit->search);
2591 edit->search = NULL;
2594 input2_str = g_string_new (input2);
2596 if (edit->search == NULL)
2598 #ifdef HAVE_CHARSET
2599 edit->search = mc_search_new (input1, cp_source);
2600 #else
2601 edit->search = mc_search_new (input1, NULL);
2602 #endif
2603 if (edit->search == NULL)
2605 edit->search_start = edit->buffer.curs1;
2606 goto cleanup;
2608 edit->search->search_type = edit_search_options.type;
2609 #ifdef HAVE_CHARSET
2610 edit->search->is_all_charsets = edit_search_options.all_codepages;
2611 #endif
2612 edit->search->is_case_sensitive = edit_search_options.case_sens;
2613 edit->search->whole_words = edit_search_options.whole_words;
2614 edit->search->search_fn = edit_search_cmd_callback;
2615 edit->search->update_fn = edit_search_update_callback;
2616 edit->search_line_type = edit_get_search_line_type (edit->search);
2617 edit_search_fix_search_start_if_selection (edit);
2620 if (edit->found_len && edit->search_start == edit->found_start + 1
2621 && edit_search_options.backwards)
2622 edit->search_start--;
2624 if (edit->found_len && edit->search_start == edit->found_start - 1
2625 && !edit_search_options.backwards)
2626 edit->search_start++;
2628 esm.first = TRUE;
2629 esm.edit = edit;
2630 esm.offset = edit->search_start;
2632 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2633 edit_search_status_update_cb, NULL);
2637 gsize len = 0;
2639 if (!editcmd_find (&esm, &len))
2641 if (!(edit->search->error == MC_SEARCH_E_OK ||
2642 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2643 edit_show_search_error (edit, _("Search"));
2645 break;
2647 once_found = TRUE;
2649 edit->search_start = edit->search->normal_offset;
2650 /*returns negative on not found or error in pattern */
2652 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2654 gsize i;
2655 GString *repl_str;
2657 edit->found_start = edit->search_start;
2658 i = edit->found_len = len;
2660 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2661 edit_scroll_screen_over_cursor (edit);
2663 if (edit->replace_mode == 0)
2665 long l;
2666 int prompt;
2668 l = edit->curs_row - WIDGET (edit)->lines / 3;
2669 if (l > 0)
2670 edit_scroll_downward (edit, l);
2671 if (l < 0)
2672 edit_scroll_upward (edit, -l);
2674 edit_scroll_screen_over_cursor (edit);
2675 edit->force |= REDRAW_PAGE;
2676 edit_render_keypress (edit);
2678 /*so that undo stops at each query */
2679 edit_push_key_press (edit);
2680 /* and prompt 2/3 down */
2681 disp1 = edit_replace_cmd__conv_to_display (saved1);
2682 disp2 = edit_replace_cmd__conv_to_display (saved2);
2683 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2684 g_free (disp1);
2685 g_free (disp2);
2687 if (prompt == B_REPLACE_ALL)
2688 edit->replace_mode = 1;
2689 else if (prompt == B_SKIP_REPLACE)
2691 if (edit_search_options.backwards)
2692 edit->search_start--;
2693 else
2694 edit->search_start++;
2695 continue; /* loop */
2697 else if (prompt == B_CANCEL)
2699 edit->replace_mode = -1;
2700 break; /* loop */
2704 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2706 if (edit->search->error != MC_SEARCH_E_OK)
2708 edit_show_search_error (edit, _("Replace"));
2709 g_string_free (repl_str, TRUE);
2710 break;
2713 /* delete then insert new */
2714 for (i = 0; i < len; i++)
2715 edit_delete (edit, TRUE);
2717 for (i = 0; i < repl_str->len; i++)
2718 edit_insert (edit, repl_str->str[i]);
2720 edit->found_len = repl_str->len;
2721 g_string_free (repl_str, TRUE);
2722 times_replaced++;
2724 /* so that we don't find the same string again */
2725 if (edit_search_options.backwards)
2727 edit->search_start--;
2729 else
2731 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2733 if (edit->search_start >= edit->buffer.size)
2734 break;
2737 edit_scroll_screen_over_cursor (edit);
2739 else
2741 /* try and find from right here for next search */
2742 edit->search_start = edit->buffer.curs1;
2743 edit_update_curs_col (edit);
2745 edit->force |= REDRAW_PAGE;
2746 edit_render_keypress (edit);
2748 if (times_replaced == 0)
2749 query_dialog (_("Replace"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&OK"));
2750 break;
2753 while (edit->replace_mode >= 0);
2755 status_msg_deinit (STATUS_MSG (&esm));
2756 edit_scroll_screen_over_cursor (edit);
2757 edit->force |= REDRAW_COMPLETELY;
2758 edit_render_keypress (edit);
2760 if ((edit->replace_mode == 1) && (times_replaced != 0))
2761 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2763 cleanup:
2764 g_free (input1);
2765 g_free (input2);
2766 if (input2_str != NULL)
2767 g_string_free (input2_str, TRUE);
2770 /* --------------------------------------------------------------------------------------------- */
2772 mc_search_cbret_t
2773 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2775 WEdit *edit = ((const edit_search_status_msg_t *) user_data)->edit;
2777 *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2778 return MC_SEARCH_CB_OK;
2781 /* --------------------------------------------------------------------------------------------- */
2783 mc_search_cbret_t
2784 edit_search_update_callback (const void *user_data, gsize char_offset)
2786 status_msg_t *sm = STATUS_MSG (user_data);
2788 ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2790 return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2793 /* --------------------------------------------------------------------------------------------- */
2795 void
2796 edit_search_cmd (WEdit * edit, gboolean again)
2799 if (edit == NULL)
2800 return;
2802 if (!again)
2803 edit_search (edit);
2804 else if (edit->last_search_string != NULL)
2805 edit_do_search (edit);
2806 else
2808 /* find last search string in history */
2809 GList *history;
2811 history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH);
2812 if (history != NULL && history->data != NULL)
2814 edit->last_search_string = (char *) history->data;
2815 history->data = NULL;
2816 history = g_list_first (history);
2817 g_list_free_full (history, g_free);
2819 #ifdef HAVE_CHARSET
2820 edit->search = mc_search_new (edit->last_search_string, cp_source);
2821 #else
2822 edit->search = mc_search_new (edit->last_search_string, NULL);
2823 #endif
2824 if (edit->search == NULL)
2826 /* if not... then ask for an expression */
2827 MC_PTR_FREE (edit->last_search_string);
2828 edit_search (edit);
2830 else
2832 edit->search->search_type = edit_search_options.type;
2833 #ifdef HAVE_CHARSET
2834 edit->search->is_all_charsets = edit_search_options.all_codepages;
2835 #endif
2836 edit->search->is_case_sensitive = edit_search_options.case_sens;
2837 edit->search->whole_words = edit_search_options.whole_words;
2838 edit->search->search_fn = edit_search_cmd_callback;
2839 edit->search->update_fn = edit_search_update_callback;
2840 edit->search_line_type = edit_get_search_line_type (edit->search);
2841 edit_do_search (edit);
2844 else
2846 /* if not... then ask for an expression */
2847 MC_PTR_FREE (edit->last_search_string);
2848 edit_search (edit);
2854 /* --------------------------------------------------------------------------------------------- */
2856 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2858 * @return TRUE if it's OK to exit, FALSE to continue editing.
2861 gboolean
2862 edit_ok_to_exit (WEdit * edit)
2864 const char *fname = N_("[NoName]");
2865 char *msg;
2866 int act;
2868 if (!edit->modified)
2869 return TRUE;
2871 if (edit->filename_vpath != NULL)
2872 fname = vfs_path_as_str (edit->filename_vpath);
2873 #ifdef ENABLE_NLS
2874 else
2875 fname = _(fname);
2876 #endif
2878 if (!mc_global.midnight_shutdown)
2880 query_set_sel (2);
2882 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2883 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2885 else
2887 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2888 fname);
2889 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2891 /* Esc is No */
2892 if (act == -1)
2893 act = 1;
2896 g_free (msg);
2898 switch (act)
2900 case 0: /* Yes */
2901 if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2902 return FALSE;
2903 edit_push_markers (edit);
2904 edit_set_markers (edit, 0, 0, 0, 0);
2905 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2906 return mc_global.midnight_shutdown;
2907 break;
2908 case 1: /* No */
2909 default:
2910 break;
2911 case 2: /* Cancel quit */
2912 case -1: /* Esc */
2913 return FALSE;
2916 return TRUE;
2919 /* --------------------------------------------------------------------------------------------- */
2920 /** save block, returns TRUE on success */
2922 gboolean
2923 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2925 int file;
2926 off_t len = 1;
2927 vfs_path_t *vpath;
2929 vpath = vfs_path_from_str (filename);
2930 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2931 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2932 vfs_path_free (vpath);
2933 if (file == -1)
2934 return FALSE;
2936 if (edit->column_highlight)
2938 int r;
2940 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2941 if (r > 0)
2943 unsigned char *block, *p;
2945 p = block = edit_get_block (edit, start, finish, &len);
2946 while (len)
2948 r = mc_write (file, p, len);
2949 if (r < 0)
2950 break;
2951 p += r;
2952 len -= r;
2954 g_free (block);
2957 else
2959 unsigned char *buf;
2960 off_t i = start;
2961 off_t end;
2963 len = finish - start;
2964 buf = g_malloc0 (TEMP_BUF_LEN);
2965 while (start != finish)
2967 end = MIN (finish, start + TEMP_BUF_LEN);
2968 for (; i < end; i++)
2969 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2970 len -= mc_write (file, (char *) buf, end - start);
2971 start = end;
2973 g_free (buf);
2975 mc_close (file);
2977 return (len == 0);
2980 /* --------------------------------------------------------------------------------------------- */
2982 void
2983 edit_paste_from_history (WEdit * edit)
2985 (void) edit;
2986 edit_error_dialog (_("Error"), _("This function is not implemented"));
2989 /* --------------------------------------------------------------------------------------------- */
2991 gboolean
2992 edit_copy_to_X_buf_cmd (WEdit * edit)
2994 off_t start_mark, end_mark;
2996 if (!eval_marks (edit, &start_mark, &end_mark))
2997 return TRUE;
2999 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3001 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
3002 return FALSE;
3004 /* try use external clipboard utility */
3005 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3007 if (option_drop_selection_on_copy)
3008 edit_mark_cmd (edit, TRUE);
3010 return TRUE;
3013 /* --------------------------------------------------------------------------------------------- */
3015 gboolean
3016 edit_cut_to_X_buf_cmd (WEdit * edit)
3018 off_t start_mark, end_mark;
3020 if (!eval_marks (edit, &start_mark, &end_mark))
3021 return TRUE;
3023 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3025 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3026 return FALSE;
3028 /* try use external clipboard utility */
3029 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3031 edit_block_delete_cmd (edit);
3032 edit_mark_cmd (edit, TRUE);
3034 return TRUE;
3037 /* --------------------------------------------------------------------------------------------- */
3039 gboolean
3040 edit_paste_from_X_buf_cmd (WEdit * edit)
3042 vfs_path_t *tmp;
3043 gboolean ret;
3045 /* try use external clipboard utility */
3046 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3047 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3048 ret = (edit_insert_file (edit, tmp) >= 0);
3049 vfs_path_free (tmp);
3051 return ret;
3054 /* --------------------------------------------------------------------------------------------- */
3056 * Ask user for the line and go to that line.
3057 * Negative numbers mean line from the end (i.e. -1 is the last line).
3060 void
3061 edit_goto_cmd (WEdit * edit)
3063 static gboolean first_run = TRUE;
3065 char *f;
3066 long l;
3067 char *error;
3069 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3070 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3071 if (f == NULL || *f == '\0')
3073 g_free (f);
3074 return;
3077 l = strtol (f, &error, 0);
3078 if (*error != '\0')
3080 g_free (f);
3081 return;
3084 if (l < 0)
3085 l = edit->buffer.lines + l + 2;
3086 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3087 edit_move_to_line (edit, l - 1);
3088 edit->force |= REDRAW_COMPLETELY;
3089 g_free (f);
3091 first_run = FALSE;
3095 /* --------------------------------------------------------------------------------------------- */
3096 /** Return TRUE on success */
3098 gboolean
3099 edit_save_block_cmd (WEdit * edit)
3101 off_t start_mark, end_mark;
3102 char *exp, *tmp;
3103 gboolean ret = FALSE;
3105 if (!eval_marks (edit, &start_mark, &end_mark))
3106 return TRUE;
3108 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3109 exp =
3110 input_expand_dialog (_("Save block"), _("Enter file name:"),
3111 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3112 g_free (tmp);
3113 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3115 if (exp != NULL && *exp != '\0')
3117 if (edit_save_block (edit, exp, start_mark, end_mark))
3118 ret = TRUE;
3119 else
3120 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3122 edit->force |= REDRAW_COMPLETELY;
3125 g_free (exp);
3127 return ret;
3131 /* --------------------------------------------------------------------------------------------- */
3133 /** returns TRUE on success */
3134 gboolean
3135 edit_insert_file_cmd (WEdit * edit)
3137 char *tmp;
3138 char *exp;
3139 gboolean ret = FALSE;
3141 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3142 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3143 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3144 g_free (tmp);
3146 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3148 if (exp != NULL && *exp != '\0')
3150 vfs_path_t *exp_vpath;
3152 exp_vpath = vfs_path_from_str (exp);
3153 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3154 vfs_path_free (exp_vpath);
3156 if (!ret)
3157 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3160 g_free (exp);
3162 edit->force |= REDRAW_COMPLETELY;
3163 return ret;
3166 /* --------------------------------------------------------------------------------------------- */
3167 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3170 edit_sort_cmd (WEdit * edit)
3172 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3173 off_t start_mark, end_mark;
3174 int e;
3176 if (!eval_marks (edit, &start_mark, &end_mark))
3178 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3179 return 0;
3182 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3183 edit_save_block (edit, tmp, start_mark, end_mark);
3184 g_free (tmp);
3186 exp = input_dialog (_("Run sort"),
3187 _("Enter sort options (see manpage) separated by whitespace:"),
3188 MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3190 if (exp == NULL)
3191 return 1;
3193 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3194 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3195 tmp =
3196 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3197 " > ", tmp_edit_temp_name, (char *) NULL);
3198 g_free (tmp_edit_temp_name);
3199 g_free (tmp_edit_block_name);
3200 g_free (exp);
3202 e = system (tmp);
3203 g_free (tmp);
3204 if (e != 0)
3206 if (e == -1 || e == 127)
3207 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3208 else
3210 char q[8];
3212 sprintf (q, "%d ", e);
3213 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3214 edit_error_dialog (_("Sort"), tmp);
3215 g_free (tmp);
3217 return -1;
3220 edit->force |= REDRAW_COMPLETELY;
3222 if (edit_block_delete_cmd (edit))
3223 return 1;
3226 vfs_path_t *tmp_vpath;
3228 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3229 edit_insert_file (edit, tmp_vpath);
3230 vfs_path_free (tmp_vpath);
3232 return 0;
3235 /* --------------------------------------------------------------------------------------------- */
3237 * Ask user for a command, execute it and paste its output back to the
3238 * editor.
3242 edit_ext_cmd (WEdit * edit)
3244 char *exp, *tmp, *tmp_edit_temp_file;
3245 int e;
3247 exp =
3248 input_dialog (_("Paste output of external command"),
3249 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3250 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3251 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3252 INPUT_COMPLETE_SHELL_ESC);
3254 if (!exp)
3255 return 1;
3257 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3258 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3259 g_free (tmp_edit_temp_file);
3260 e = system (tmp);
3261 g_free (tmp);
3262 g_free (exp);
3264 if (e)
3266 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3267 return -1;
3270 edit->force |= REDRAW_COMPLETELY;
3273 vfs_path_t *tmp_vpath;
3275 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3276 edit_insert_file (edit, tmp_vpath);
3277 vfs_path_free (tmp_vpath);
3279 return 0;
3282 /* --------------------------------------------------------------------------------------------- */
3283 /** if block is 1, a block must be highlighted and the shell command
3284 processes it. If block is 0 the shell command is a straight system
3285 command, that just produces some output which is to be inserted */
3287 void
3288 edit_block_process_cmd (WEdit * edit, int macro_number)
3290 char *fname;
3291 char *macros_fname = NULL;
3293 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3294 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3295 user_menu (edit, macros_fname, 0);
3296 g_free (fname);
3297 g_free (macros_fname);
3298 edit->force |= REDRAW_COMPLETELY;
3301 /* --------------------------------------------------------------------------------------------- */
3303 void
3304 edit_mail_dialog (WEdit * edit)
3306 char *mail_to, *mail_subject, *mail_cc;
3308 quick_widget_t quick_widgets[] = {
3309 /* *INDENT-OFF* */
3310 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3311 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3312 INPUT_LAST_TEXT, "mail-dlg-input-3",
3313 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3314 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3315 INPUT_LAST_TEXT, "mail-dlg-input-2",
3316 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3317 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3318 INPUT_LAST_TEXT, "mail-dlg-input",
3319 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3320 QUICK_BUTTONS_OK_CANCEL,
3321 QUICK_END
3322 /* *INDENT-ON* */
3325 quick_dialog_t qdlg = {
3326 -1, -1, 50,
3327 N_("Mail"), "[Input Line Keys]",
3328 quick_widgets, NULL, NULL
3331 if (quick_dialog (&qdlg) != B_CANCEL)
3333 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3334 g_free (mail_to);
3335 g_free (mail_subject);
3336 g_free (mail_cc);
3340 /* --------------------------------------------------------------------------------------------- */
3342 /*******************/
3343 /* Word Completion */
3344 /*******************/
3347 * Complete current word using regular expression search
3348 * backwards beginning at the current cursor position.
3351 void
3352 edit_complete_word_cmd (WEdit * edit)
3354 gsize i, max_len, word_len = 0, num_compl = 0;
3355 off_t word_start = 0;
3356 GString *match_expr;
3357 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3359 /* search start of word to be completed */
3360 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3361 return;
3363 /* prepare match expression */
3364 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3365 match_expr = g_string_new ("(^|\\s+|\\b)");
3366 for (i = 0; i < word_len; i++)
3367 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3368 g_string_append (match_expr,
3369 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3371 /* collect the possible completions */
3372 /* start search from begin to end of file */
3373 max_len =
3374 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3375 &num_compl);
3377 if (num_compl > 0)
3379 /* insert completed word if there is only one match */
3380 if (num_compl == 1)
3381 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3382 /* more than one possible completion => ask the user */
3383 else
3385 char *curr_compl;
3387 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3388 /* !!! pressed again the selection dialog pops up, but that !!! */
3389 /* !!! seems to require a further internal state !!! */
3390 /*tty_beep (); */
3392 /* let the user select the preferred completion */
3393 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3394 (GString **) & compl, num_compl);
3396 if (curr_compl != NULL)
3398 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3399 g_free (curr_compl);
3404 g_string_free (match_expr, TRUE);
3405 /* release memory before return */
3406 for (i = 0; i < num_compl; i++)
3407 g_string_free (compl[i], TRUE);
3410 /* --------------------------------------------------------------------------------------------- */
3412 #ifdef HAVE_CHARSET
3413 void
3414 edit_select_codepage_cmd (WEdit * edit)
3416 if (do_select_codepage ())
3417 edit_set_codeset (edit);
3419 edit->force = REDRAW_PAGE;
3420 widget_draw (WIDGET (edit));
3422 #endif
3424 /* --------------------------------------------------------------------------------------------- */
3426 void
3427 edit_insert_literal_cmd (WEdit * edit)
3429 int char_for_insertion;
3431 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3432 _("Press any key:"), FALSE);
3433 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3436 /* --------------------------------------------------------------------------------------------- */
3438 void
3439 edit_begin_end_macro_cmd (WEdit * edit)
3441 /* edit is a pointer to the widget */
3442 if (edit != NULL)
3444 long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3445 edit_execute_key_command (edit, command, -1);
3449 /* --------------------------------------------------------------------------------------------- */
3451 void
3452 edit_begin_end_repeat_cmd (WEdit * edit)
3454 /* edit is a pointer to the widget */
3455 if (edit != NULL)
3457 long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3458 edit_execute_key_command (edit, command, -1);
3462 /* --------------------------------------------------------------------------------------------- */
3464 gboolean
3465 edit_load_forward_cmd (WEdit * edit)
3467 if (edit->modified
3468 && edit_query_dialog2 (_("Warning"),
3469 _("Current text was modified without a file save.\n"
3470 "Continue discards these changes"), _("C&ontinue"),
3471 _("&Cancel")) == 1)
3473 edit->force |= REDRAW_COMPLETELY;
3474 return TRUE;
3477 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3478 return FALSE;
3480 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3481 return FALSE;
3483 edit_stack_iterator++;
3484 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3485 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3486 edit_history_moveto[edit_stack_iterator].line);
3488 return FALSE;
3491 /* --------------------------------------------------------------------------------------------- */
3493 gboolean
3494 edit_load_back_cmd (WEdit * edit)
3496 if (edit->modified
3497 && edit_query_dialog2 (_("Warning"),
3498 _("Current text was modified without a file save.\n"
3499 "Continue discards these changes"), _("C&ontinue"),
3500 _("&Cancel")) == 1)
3502 edit->force |= REDRAW_COMPLETELY;
3503 return TRUE;
3506 /* we are in the bottom of the stack, NO WAY! */
3507 if (edit_stack_iterator == 0)
3508 return FALSE;
3510 edit_stack_iterator--;
3511 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3512 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3513 edit_history_moveto[edit_stack_iterator].line);
3515 return FALSE;
3518 /* --------------------------------------------------------------------------------------------- */
3520 void
3521 edit_get_match_keyword_cmd (WEdit * edit)
3523 gsize word_len = 0, max_len = 0;
3524 int num_def = 0;
3525 gsize i;
3526 off_t word_start = 0;
3527 GString *match_expr;
3528 char *path = NULL;
3529 char *ptr = NULL;
3530 char *tagfile = NULL;
3532 etags_hash_t def_hash[MAX_DEFINITIONS];
3534 for (i = 0; i < MAX_DEFINITIONS; i++)
3536 def_hash[i].filename = NULL;
3539 /* search start of word to be completed */
3540 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3541 return;
3543 /* prepare match expression */
3544 match_expr = g_string_sized_new (word_len);
3545 for (i = 0; i < word_len; i++)
3546 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3548 ptr = g_get_current_dir ();
3549 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3550 g_free (ptr);
3552 /* Recursive search file 'TAGS' in parent dirs */
3555 ptr = g_path_get_dirname (path);
3556 g_free (path);
3557 path = ptr;
3558 g_free (tagfile);
3559 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3560 if (exist_file (tagfile))
3561 break;
3563 while (strcmp (path, PATH_SEP_STR) != 0);
3565 if (tagfile)
3567 num_def =
3568 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3569 g_free (tagfile);
3571 g_free (path);
3573 max_len = MAX_WIDTH_DEF_DIALOG;
3574 word_len = 0;
3575 if (num_def > 0)
3577 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3578 (etags_hash_t *) & def_hash, num_def);
3580 g_string_free (match_expr, TRUE);
3583 /* --------------------------------------------------------------------------------------------- */
3585 #ifdef HAVE_ASPELL
3587 edit_suggest_current_word (WEdit * edit)
3589 gsize cut_len = 0;
3590 gsize word_len = 0;
3591 off_t word_start = 0;
3592 int retval = B_SKIP_WORD;
3593 GString *match_word;
3595 /* search start of word to spell check */
3596 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3597 &cut_len);
3598 word_len = match_word->len;
3600 #ifdef HAVE_CHARSET
3601 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3603 GString *tmp_word;
3605 tmp_word = str_convert_to_display (match_word->str);
3606 g_string_free (match_word, TRUE);
3607 match_word = tmp_word;
3609 #endif
3610 if (!aspell_check (match_word->str, (int) word_len))
3612 GArray *suggest;
3613 unsigned int res;
3614 guint i;
3616 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3618 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3619 if (res != 0)
3621 char *new_word = NULL;
3623 edit->found_start = word_start;
3624 edit->found_len = word_len;
3625 edit->force |= REDRAW_PAGE;
3626 edit_scroll_screen_over_cursor (edit);
3627 edit_render_keypress (edit);
3629 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3630 edit_cursor_move (edit, word_len - cut_len);
3632 if (retval == B_ENTER && new_word != NULL)
3634 char *cp_word;
3636 #ifdef HAVE_CHARSET
3637 if (mc_global.source_codepage >= 0 &&
3638 (mc_global.source_codepage != mc_global.display_codepage))
3640 GString *tmp_word;
3642 tmp_word = str_convert_to_input (new_word);
3643 g_free (new_word);
3644 new_word = g_string_free (tmp_word, FALSE);
3646 #endif
3647 cp_word = new_word;
3648 for (i = 0; i < word_len; i++)
3649 edit_backspace (edit, TRUE);
3650 for (; *new_word; new_word++)
3651 edit_insert (edit, *new_word);
3652 g_free (cp_word);
3654 else if (retval == B_ADD_WORD && match_word != NULL)
3655 aspell_add_to_dict (match_word->str, (int) word_len);
3659 for (i = 0; i < suggest->len; i++)
3661 char *cur_sugg_word;
3663 cur_sugg_word = g_array_index (suggest, char *, i);
3664 g_free (cur_sugg_word);
3666 g_array_free (suggest, TRUE);
3667 edit->found_start = 0;
3668 edit->found_len = 0;
3670 g_string_free (match_word, TRUE);
3671 return retval;
3674 /* --------------------------------------------------------------------------------------------- */
3676 void
3677 edit_spellcheck_file (WEdit * edit)
3679 if (edit->buffer.curs_line > 0)
3681 edit_cursor_move (edit, -edit->buffer.curs1);
3682 edit_move_to_prev_col (edit, 0);
3683 edit_update_curs_row (edit);
3688 int c1, c2;
3690 c2 = edit_buffer_get_current_byte (&edit->buffer);
3694 if (edit->buffer.curs1 >= edit->buffer.size)
3695 return;
3697 c1 = c2;
3698 edit_cursor_move (edit, 1);
3699 c2 = edit_buffer_get_current_byte (&edit->buffer);
3701 while (is_break_char (c1) || is_break_char (c2));
3703 while (edit_suggest_current_word (edit) != B_CANCEL);
3706 /* --------------------------------------------------------------------------------------------- */
3708 void
3709 edit_set_spell_lang (void)
3711 GArray *lang_list;
3713 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3714 if (aspell_get_lang_list (lang_list) != 0)
3716 char *lang;
3718 lang = spell_dialog_lang_list_show (lang_list);
3719 if (lang != NULL)
3721 (void) aspell_set_lang (lang);
3722 g_free (lang);
3725 aspell_array_clean (lang_list);
3727 #endif /* HAVE_ASPELL */
3729 /* --------------------------------------------------------------------------------------------- */