mcedit: make backward search interruptible.
[midnight-commander.git] / src / editor / editcmd.c
blob632e6f1719cd11d4525305e964812a9ceffd392a
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2016
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 #ifdef HAVE_ASSERT_H
39 #include <assert.h>
40 #endif
41 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 #include <stdlib.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/fileloc.h"
59 #include "lib/lock.h"
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/event.h" /* mc_event_raise() */
64 #ifdef HAVE_CHARSET
65 #include "lib/charsets.h"
66 #endif
68 #include "src/history.h"
69 #include "src/setup.h" /* option_tab_spacing */
70 #ifdef HAVE_CHARSET
71 #include "src/selcodepage.h"
72 #endif
73 #include "src/keybind-defaults.h"
74 #include "src/util.h" /* check_for_default() */
76 #include "edit-impl.h"
77 #include "editwidget.h"
78 #include "editcmd_dialogs.h"
79 #ifdef HAVE_ASPELL
80 #include "spell.h"
81 #include "spell_dialogs.h"
82 #endif
83 #include "etags.h"
85 /*** global variables ****************************************************************************/
87 /* search and replace: */
88 int search_create_bookmark = FALSE;
90 /* queries on a save */
91 int edit_confirm_save = 1;
93 /* whether we need to drop selection on copy to buffer */
94 int option_drop_selection_on_copy = 1;
96 /*** file scope macro definitions ****************************************************************/
98 #define space_width 1
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
111 /*** file scope type declarations ****************************************************************/
113 typedef struct
115 simple_status_msg_t status_msg; /* base class */
117 gboolean first;
118 WEdit *edit;
119 off_t offset;
120 } edit_search_status_msg_t;
122 /*** file scope variables ************************************************************************/
124 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
126 /* --------------------------------------------------------------------------------------------- */
127 /*** file scope functions ************************************************************************/
128 /* --------------------------------------------------------------------------------------------- */
130 static int
131 edit_search_status_update_cb (status_msg_t * sm)
133 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
134 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
135 Widget *wd = WIDGET (sm->dlg);
137 if (verbose)
138 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
139 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
140 else
141 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
143 if (esm->first)
145 int wd_width;
146 Widget *lw = WIDGET (ssm->label);
148 wd_width = MAX (wd->cols, lw->cols + 6);
149 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
150 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
151 esm->first = FALSE;
154 return status_msg_common_update (sm);
157 /* --------------------------------------------------------------------------------------------- */
159 static cb_ret_t
160 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
162 switch (msg)
164 case MSG_NOTIFY:
165 if (sender != NULL && sender->id == edit_save_mode_radio_id)
167 Widget *ww;
169 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
170 widget_disable (ww, RADIO (sender)->sel != 2);
171 return MSG_HANDLED;
174 return MSG_NOT_HANDLED;
176 default:
177 return dlg_default_callback (w, sender, msg, parm, data);
181 /* --------------------------------------------------------------------------------------------- */
183 /* If 0 (quick save) then a) create/truncate <filename> file,
184 b) save to <filename>;
185 if 1 (safe save) then a) save to <tempnam>,
186 b) rename <tempnam> to <filename>;
187 if 2 (do backups) then a) save to <tempnam>,
188 b) rename <filename> to <filename.backup_ext>,
189 c) rename <tempnam> to <filename>. */
191 /* returns 0 on error, -1 on abort */
193 static int
194 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
196 char *p;
197 gchar *tmp;
198 off_t filelen = 0;
199 int this_save_mode, rv, fd = -1;
200 vfs_path_t *real_filename_vpath;
201 vfs_path_t *savename_vpath = NULL;
202 const char *start_filename;
203 const vfs_path_element_t *vpath_element;
204 struct stat sb;
206 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
207 if (vpath_element == NULL)
208 return 0;
210 start_filename = vpath_element->path;
211 if (*start_filename == '\0')
212 return 0;
214 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
215 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
216 else
217 real_filename_vpath = vfs_path_clone (filename_vpath);
219 this_save_mode = option_save_mode;
220 if (this_save_mode != EDIT_QUICK_SAVE)
222 if (!vfs_file_is_local (real_filename_vpath)
223 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
226 * The file does not exists yet, so no safe save or
227 * backup are necessary.
229 this_save_mode = EDIT_QUICK_SAVE;
231 if (fd != -1)
232 mc_close (fd);
235 rv = mc_stat (real_filename_vpath, &sb);
236 if (rv == 0)
238 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
240 rv = edit_query_dialog3 (_("Warning"),
241 _("File has hard-links. Detach before saving?"),
242 _("&Yes"), _("&No"), _("&Cancel"));
243 switch (rv)
245 case 0:
246 this_save_mode = EDIT_SAFE_SAVE;
247 /* fallthrough */
248 case 1:
249 edit->skip_detach_prompt = 1;
250 break;
251 default:
252 vfs_path_free (real_filename_vpath);
253 return -1;
257 /* Prevent overwriting changes from other editor sessions. */
258 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
260 /* The default action is "Cancel". */
261 query_set_sel (1);
263 rv = edit_query_dialog2 (_("Warning"),
264 _("The file has been modified in the meantime. Save anyway?"),
265 _("&Yes"), _("&Cancel"));
266 if (rv != 0)
268 vfs_path_free (real_filename_vpath);
269 return -1;
274 if (this_save_mode != EDIT_QUICK_SAVE)
276 char *savedir, *saveprefix;
278 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
279 if (savedir == NULL)
280 savedir = g_strdup (".");
282 /* Token-related function never return leading slash, so we need add it manually */
283 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", (char *) NULL);
284 g_free (savedir);
285 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
286 g_free (saveprefix);
287 if (savename_vpath == NULL)
289 vfs_path_free (real_filename_vpath);
290 return 0;
292 /* FIXME:
293 * Close for now because mc_mkstemps use pure open system call
294 * to create temporary file and it needs to be reopened by
295 * VFS-aware mc_open().
297 close (fd);
299 else
300 savename_vpath = vfs_path_clone (real_filename_vpath);
302 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
303 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
305 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
306 if (fd == -1)
307 goto error_save;
309 /* pipe save */
310 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
311 if (p != NULL)
313 FILE *file;
315 mc_close (fd);
316 file = (FILE *) popen (p, "w");
318 if (file)
320 filelen = edit_write_stream (edit, file);
321 #if 1
322 pclose (file);
323 #else
324 if (pclose (file) != 0)
326 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
327 edit_error_dialog (_("Error"), tmp);
328 g_free (tmp);
329 g_free (p);
330 goto error_save;
332 #endif
334 else
336 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
337 edit_error_dialog (_("Error"), get_sys_error (tmp));
338 g_free (p);
339 g_free (tmp);
340 goto error_save;
342 g_free (p);
344 else if (edit->lb == LB_ASIS)
345 { /* do not change line breaks */
346 filelen = edit_buffer_write_file (&edit->buffer, fd);
348 if (filelen != edit->buffer.size)
350 mc_close (fd);
351 goto error_save;
353 if (mc_close (fd) != 0)
354 goto error_save;
355 /* Update the file information, especially the mtime. */
356 if (mc_stat (savename_vpath, &edit->stat1) == -1)
357 goto error_save;
359 else
360 { /* change line breaks */
361 FILE *file;
362 const vfs_path_element_t *path_element;
364 mc_close (fd);
366 path_element = vfs_path_get_by_index (savename_vpath, -1);
367 file = (FILE *) fopen (path_element->path, "w");
368 if (file != NULL)
370 filelen = edit_write_stream (edit, file);
371 fclose (file);
373 else
375 char *msg;
377 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
378 edit_error_dialog (_("Error"), msg);
379 g_free (msg);
380 goto error_save;
384 if (filelen != edit->buffer.size)
385 goto error_save;
387 if (this_save_mode == EDIT_DO_BACKUP)
389 char *tmp_store_filename;
390 vfs_path_element_t *last_vpath_element;
391 vfs_path_t *tmp_vpath;
392 gboolean ok;
394 #ifdef HAVE_ASSERT_H
395 assert (option_backup_ext != NULL);
396 #endif
397 /* add backup extension to the path */
398 tmp_vpath = vfs_path_clone (real_filename_vpath);
399 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
400 tmp_store_filename = last_vpath_element->path;
401 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
402 g_free (tmp_store_filename);
404 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
405 vfs_path_free (tmp_vpath);
406 if (!ok)
407 goto error_save;
410 if (this_save_mode != EDIT_QUICK_SAVE)
411 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
412 goto error_save;
414 vfs_path_free (real_filename_vpath);
415 vfs_path_free (savename_vpath);
416 return 1;
417 error_save:
418 /* FIXME: Is this safe ?
419 * if (this_save_mode != EDIT_QUICK_SAVE)
420 * mc_unlink (savename);
422 vfs_path_free (real_filename_vpath);
423 vfs_path_free (savename_vpath);
424 return 0;
427 /* --------------------------------------------------------------------------------------------- */
429 static gboolean
430 edit_check_newline (const edit_buffer_t * buf)
432 return !(option_check_nl_at_eof && buf->size > 0
433 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
434 && edit_query_dialog2 (_("Warning"),
435 _("The file you are saving does not end with a newline."),
436 _("C&ontinue"), _("&Cancel")) != 0);
439 /* --------------------------------------------------------------------------------------------- */
441 static vfs_path_t *
442 edit_get_save_file_as (WEdit * edit)
444 static LineBreaks cur_lb = LB_ASIS;
445 char *filename_res;
446 vfs_path_t *ret_vpath = NULL;
448 const char *lb_names[LB_NAMES] = {
449 N_("&Do not change"),
450 N_("&Unix format (LF)"),
451 N_("&Windows/DOS format (CR LF)"),
452 N_("&Macintosh format (CR)")
455 quick_widget_t quick_widgets[] = {
456 /* *INDENT-OFF* */
457 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
458 vfs_path_as_str (edit->filename_vpath), "save-as",
459 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
460 QUICK_SEPARATOR (TRUE),
461 QUICK_LABEL (N_("Change line breaks to:"), NULL),
462 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
463 QUICK_BUTTONS_OK_CANCEL,
464 QUICK_END
465 /* *INDENT-ON* */
468 quick_dialog_t qdlg = {
469 -1, -1, 64,
470 N_("Save As"), "[Save File As]",
471 quick_widgets, NULL, NULL
474 if (quick_dialog (&qdlg) != B_CANCEL)
476 char *fname;
478 edit->lb = cur_lb;
479 fname = tilde_expand (filename_res);
480 g_free (filename_res);
481 ret_vpath = vfs_path_from_str (fname);
482 g_free (fname);
485 return ret_vpath;
488 /* --------------------------------------------------------------------------------------------- */
490 /** returns TRUE on success */
492 static gboolean
493 edit_save_cmd (WEdit * edit)
495 int res, save_lock = 0;
497 if (!edit->locked && !edit->delete_file)
498 save_lock = lock_file (edit->filename_vpath);
499 res = edit_save_file (edit, edit->filename_vpath);
501 /* Maintain modify (not save) lock on failure */
502 if ((res > 0 && edit->locked) || save_lock)
503 edit->locked = unlock_file (edit->filename_vpath);
505 /* On failure try 'save as', it does locking on its own */
506 if (res == 0)
507 return edit_save_as_cmd (edit);
508 edit->force |= REDRAW_COMPLETELY;
509 if (res > 0)
511 edit->delete_file = 0;
512 edit->modified = 0;
515 return TRUE;
518 /* --------------------------------------------------------------------------------------------- */
520 * Load file content
522 * @param h screen the owner of editor window
523 * @param vpath vfs file path
524 * @return TRUE if file content was successfully loaded, FALSE otherwise
527 static inline gboolean
528 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
530 Widget *w = WIDGET (h);
532 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
535 /* --------------------------------------------------------------------------------------------- */
537 static void
538 edit_delete_column_of_text (WEdit * edit)
540 off_t p, q, r;
541 off_t m1, m2;
542 off_t n;
543 long b, c, d;
545 eval_marks (edit, &m1, &m2);
546 n = edit_buffer_get_forward_offset (&edit->buffer, m1, 0, m2) + 1;
547 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
548 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
549 b = MAX (MIN (c, d), MIN (edit->column1, edit->column2));
550 c = MAX (c, MAX (edit->column1, edit->column2));
552 while (n--)
554 r = edit_buffer_get_current_bol (&edit->buffer);
555 p = edit_move_forward3 (edit, r, b, 0);
556 q = edit_move_forward3 (edit, r, c, 0);
557 if (p < m1)
558 p = m1;
559 if (q > m2)
560 q = m2;
561 edit_cursor_move (edit, p - edit->buffer.curs1);
562 while (q > p)
564 /* delete line between margins */
565 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
566 edit_delete (edit, TRUE);
567 q--;
569 if (n)
570 /* move to next line except on the last delete */
571 edit_cursor_move (edit,
572 edit_buffer_get_forward_offset (&edit->buffer, edit->buffer.curs1, 1,
573 0) - edit->buffer.curs1);
577 /* --------------------------------------------------------------------------------------------- */
578 /** if success return 0 */
580 static int
581 edit_block_delete (WEdit * edit)
583 off_t start_mark, end_mark;
584 off_t curs_pos;
585 long curs_line, c1, c2;
587 if (!eval_marks (edit, &start_mark, &end_mark))
588 return 0;
590 if (edit->column_highlight && edit->mark2 < 0)
591 edit_mark_cmd (edit, FALSE);
592 if ((end_mark - start_mark) > option_max_undo / 2)
594 /* Warning message with a query to continue or cancel the operation */
595 if (edit_query_dialog2
596 (_("Warning"),
598 ("Block is large, you may not be able to undo this action"),
599 _("C&ontinue"), _("&Cancel")))
601 return 1;
604 c1 = MIN (edit->column1, edit->column2);
605 c2 = MAX (edit->column1, edit->column2);
606 edit->column1 = c1;
607 edit->column2 = c2;
609 edit_push_markers (edit);
611 curs_line = edit->buffer.curs_line;
613 curs_pos = edit->curs_col + edit->over_col;
615 /* move cursor to start of selection */
616 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
617 edit_scroll_screen_over_cursor (edit);
619 if (start_mark < end_mark)
621 if (edit->column_highlight)
623 off_t line_width;
625 if (edit->mark2 < 0)
626 edit_mark_cmd (edit, FALSE);
627 edit_delete_column_of_text (edit);
628 /* move cursor to the saved position */
629 edit_move_to_line (edit, curs_line);
630 /* calculate line width and cursor position before cut */
631 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
632 edit_buffer_get_current_eol (&edit->buffer));
633 if (option_cursor_beyond_eol && curs_pos > line_width)
634 edit->over_col = curs_pos - line_width;
636 else
638 off_t count;
640 for (count = start_mark; count < end_mark; count++)
641 edit_delete (edit, TRUE);
644 edit_set_markers (edit, 0, 0, 0, 0);
645 edit->force |= REDRAW_PAGE;
646 return 0;
649 /* --------------------------------------------------------------------------------------------- */
651 * Get EOL symbol for searching.
653 * @param edit editor object
654 * @return EOL symbol
657 static inline char
658 edit_search_get_current_end_line_char (const WEdit * edit)
660 switch (edit->lb)
662 case LB_MAC:
663 return '\r';
664 default:
665 return '\n';
669 /* --------------------------------------------------------------------------------------------- */
671 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
673 * @param search search object
674 * @return result of checks.
677 static edit_search_line_t
678 edit_get_search_line_type (mc_search_t * search)
680 edit_search_line_t search_line_type = 0;
682 if (search->search_type != MC_SEARCH_T_REGEX)
683 return search_line_type;
685 if (*search->original == '^')
686 search_line_type |= AT_START_LINE;
688 if (search->original[search->original_len - 1] == '$')
689 search_line_type |= AT_END_LINE;
690 return search_line_type;
693 /* --------------------------------------------------------------------------------------------- */
695 * Calculating the start position of next line.
697 * @param buf editor buffer object
698 * @param current_pos current position
699 * @param max_pos max position
700 * @param end_string_symbol end of line symbol
701 * @return start position of next line
704 static off_t
705 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
706 char end_string_symbol)
708 off_t i;
710 for (i = current_pos; i < max_pos; i++)
712 current_pos++;
713 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
714 break;
717 return current_pos;
720 /* --------------------------------------------------------------------------------------------- */
722 * Calculating the end position of previous line.
724 * @param buf editor buffer object
725 * @param current_pos current position
726 * @param end_string_symbol end of line symbol
727 * @return end position of previous line
730 static off_t
731 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
732 char end_string_symbol)
734 off_t i;
736 for (i = current_pos - 1; i >= 0; i--)
737 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
738 break;
740 return i;
743 /* --------------------------------------------------------------------------------------------- */
745 * Calculating the start position of previous line.
747 * @param buf editor buffer object
748 * @param current_pos current position
749 * @param end_string_symbol end of line symbol
750 * @return start position of previous line
753 static inline off_t
754 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
755 char end_string_symbol)
757 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
758 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
760 return (current_pos + 1);
763 /* --------------------------------------------------------------------------------------------- */
765 * Calculating the start position of current line.
767 * @param buf editor buffer object
768 * @param current_pos current position
769 * @param end_string_symbol end of line symbol
770 * @return start position of current line
773 static inline off_t
774 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
775 char end_string_symbol)
777 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
779 return (current_pos + 1);
782 /* --------------------------------------------------------------------------------------------- */
784 * Fixing (if needed) search start position if 'only in selection' option present.
786 * @param edit editor object
789 static void
790 edit_search_fix_search_start_if_selection (WEdit * edit)
792 off_t start_mark = 0;
793 off_t end_mark = 0;
795 if (!edit_search_options.only_in_selection)
796 return;
798 if (!eval_marks (edit, &start_mark, &end_mark))
799 return;
801 if (edit_search_options.backwards)
803 if (edit->search_start > end_mark || edit->search_start <= start_mark)
804 edit->search_start = end_mark;
806 else
808 if (edit->search_start < start_mark || edit->search_start >= end_mark)
809 edit->search_start = start_mark;
813 /* --------------------------------------------------------------------------------------------- */
815 static gboolean
816 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
818 WEdit *edit = esm->edit;
819 off_t search_start = edit->search_start;
820 off_t search_end;
821 off_t start_mark = 0;
822 off_t end_mark = edit->buffer.size;
823 char end_string_symbol;
825 end_string_symbol = edit_search_get_current_end_line_char (edit);
827 /* prepare for search */
828 if (edit_search_options.only_in_selection)
830 if (!eval_marks (edit, &start_mark, &end_mark))
832 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
833 return FALSE;
836 /* fix the start and the end of search block positions */
837 if ((edit->search_line_type & AT_START_LINE) != 0
838 && (start_mark != 0
839 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
840 start_mark =
841 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
842 end_string_symbol);
844 if ((edit->search_line_type & AT_END_LINE) != 0
845 && (end_mark - 1 != edit->buffer.size
846 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
847 end_mark =
848 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
850 if (start_mark >= end_mark)
852 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
853 return FALSE;
856 else if (edit_search_options.backwards)
857 end_mark = MAX (1, edit->buffer.curs1) - 1;
859 /* search */
860 if (edit_search_options.backwards)
862 /* backward search */
863 search_end = end_mark;
865 if ((edit->search_line_type & AT_START_LINE) != 0)
866 search_start =
867 edit_calculate_start_of_current_line (&edit->buffer, search_start,
868 end_string_symbol);
870 while (search_start >= start_mark)
872 gboolean ok;
874 if (search_end > (off_t) (search_start + edit->search->original_len)
875 && mc_search_is_fixed_search_str (edit->search))
876 search_end = search_start + edit->search->original_len;
878 ok = mc_search_run (edit->search, (void *) esm, search_start, search_end, len);
880 if (ok && edit->search->normal_offset == search_start)
881 return TRUE;
883 /* Abort search. */
884 if (!ok && edit->search->error == MC_SEARCH_E_ABORT)
885 return FALSE;
887 if ((edit->search_line_type & AT_START_LINE) != 0)
888 search_start =
889 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
890 end_string_symbol);
891 else
892 search_start--;
895 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
896 return FALSE;
899 /* forward search */
900 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
901 search_start =
902 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
903 end_string_symbol);
905 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
908 /* --------------------------------------------------------------------------------------------- */
910 static char *
911 edit_replace_cmd__conv_to_display (const char *str)
913 #ifdef HAVE_CHARSET
914 GString *tmp;
916 tmp = str_convert_to_display (str);
917 if (tmp != NULL)
919 if (tmp->len != 0)
920 return g_string_free (tmp, FALSE);
921 g_string_free (tmp, TRUE);
923 #endif
924 return g_strdup (str);
927 /* --------------------------------------------------------------------------------------------- */
929 static char *
930 edit_replace_cmd__conv_to_input (char *str)
932 #ifdef HAVE_CHARSET
933 GString *tmp;
935 tmp = str_convert_to_input (str);
936 if (tmp != NULL)
938 if (tmp->len != 0)
939 return g_string_free (tmp, FALSE);
940 g_string_free (tmp, TRUE);
942 #endif
943 return g_strdup (str);
946 /* --------------------------------------------------------------------------------------------- */
948 static void
949 edit_do_search (WEdit * edit)
951 edit_search_status_msg_t esm;
952 gsize len = 0;
954 if (edit->search == NULL)
955 edit->search_start = edit->buffer.curs1;
957 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
959 esm.first = TRUE;
960 esm.edit = edit;
961 esm.offset = edit->search_start;
963 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
964 edit_search_status_update_cb, NULL);
966 if (search_create_bookmark)
968 int found = 0, books = 0;
969 long l = 0, l_last = -1;
970 long q = 0;
972 search_create_bookmark = FALSE;
973 book_mark_flush (edit, -1);
975 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
977 if (found == 0)
978 edit->search_start = edit->search->normal_offset;
979 found++;
980 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
981 if (l != l_last)
983 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
984 books++;
986 l_last = l;
987 q = edit->search->normal_offset + 1;
990 if (found == 0)
991 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
992 else
993 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
995 else
997 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
998 && edit_search_options.backwards)
999 edit->search_start--;
1001 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1002 && !edit_search_options.backwards)
1003 edit->search_start++;
1005 if (editcmd_find (&esm, &len))
1007 edit->found_start = edit->search_start = edit->search->normal_offset;
1008 edit->found_len = len;
1009 edit->over_col = 0;
1010 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1011 edit_scroll_screen_over_cursor (edit);
1012 if (edit_search_options.backwards)
1013 edit->search_start--;
1014 else
1015 edit->search_start++;
1017 else
1019 edit->search_start = edit->buffer.curs1;
1020 if (edit->search->error_str != NULL)
1021 edit_query_dialog (_("Search"), edit->search->error_str);
1025 status_msg_deinit (STATUS_MSG (&esm));
1027 edit->force |= REDRAW_COMPLETELY;
1028 edit_scroll_screen_over_cursor (edit);
1031 /* --------------------------------------------------------------------------------------------- */
1033 static void
1034 edit_search (WEdit * edit)
1036 if (editcmd_dialog_search_show (edit))
1038 edit->search_line_type = edit_get_search_line_type (edit->search);
1039 edit_search_fix_search_start_if_selection (edit);
1040 edit_do_search (edit);
1044 /* --------------------------------------------------------------------------------------------- */
1045 /** Return a null terminated length of text. Result must be g_free'd */
1047 static unsigned char *
1048 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1050 unsigned char *s, *r;
1052 r = s = g_malloc0 (finish - start + 1);
1053 if (edit->column_highlight)
1055 *l = 0;
1056 /* copy from buffer, excluding chars that are out of the column 'margins' */
1057 while (start < finish)
1059 int c;
1060 off_t x;
1062 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1063 c = edit_buffer_get_byte (&edit->buffer, start);
1064 if ((x >= edit->column1 && x < edit->column2)
1065 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1067 *s++ = c;
1068 (*l)++;
1070 start++;
1073 else
1075 *l = finish - start;
1076 while (start < finish)
1077 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1079 *s = '\0';
1080 return r;
1083 /* --------------------------------------------------------------------------------------------- */
1084 /** copies a block to clipboard file */
1086 static gboolean
1087 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1089 gboolean ret;
1090 gchar *tmp;
1092 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1093 ret = edit_save_block (edit, tmp, start, finish);
1094 g_free (tmp);
1095 return ret;
1098 /* --------------------------------------------------------------------------------------------- */
1100 static void
1101 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1103 FILE *p = 0;
1104 char *s;
1106 to = name_quote (to, FALSE);
1107 subject = name_quote (subject, FALSE);
1108 cc = name_quote (cc, FALSE);
1109 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1110 g_free (to);
1111 g_free (subject);
1112 g_free (cc);
1114 if (s != NULL)
1116 p = popen (s, "w");
1117 g_free (s);
1120 if (p != NULL)
1122 off_t i;
1124 for (i = 0; i < buf->size; i++)
1125 fputc (edit_buffer_get_byte (buf, i), p);
1126 pclose (p);
1130 /* --------------------------------------------------------------------------------------------- */
1131 /** find first character of current word */
1133 static gboolean
1134 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1136 int c;
1137 off_t i;
1139 /* return if at begin of file */
1140 if (buf->curs1 <= 0)
1141 return FALSE;
1143 c = edit_buffer_get_previous_byte (buf);
1144 /* return if not at end or in word */
1145 if (is_break_char (c))
1146 return FALSE;
1148 /* search start of word to be completed */
1149 for (i = 1;; i++)
1151 int last;
1153 last = c;
1154 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1156 if (is_break_char (c))
1158 /* return if word starts with digit */
1159 if (isdigit (last))
1160 return FALSE;
1162 break;
1166 /* success */
1167 *word_start = buf->curs1 - i; /* start found */
1168 *word_len = (gsize) i;
1169 return TRUE;
1172 /* --------------------------------------------------------------------------------------------- */
1174 * Get current word under cursor
1176 * @param esm status message window
1177 * @param srch mc_search object
1178 * @param word_start start word position
1180 * @return newly allocated string or NULL if no any words under cursor
1183 static char *
1184 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1185 off_t word_start)
1187 WEdit *edit = esm->edit;
1188 gsize len = 0;
1189 off_t i;
1190 GString *temp;
1192 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1193 return NULL;
1195 temp = g_string_sized_new (len);
1197 for (i = 0; i < (off_t) len; i++)
1199 int chr;
1201 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1202 if (!isspace (chr))
1203 g_string_append_c (temp, chr);
1206 return g_string_free (temp, temp->len == 0);
1209 /* --------------------------------------------------------------------------------------------- */
1210 /** collect the possible completions */
1212 static gsize
1213 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1214 char *match_expr, GString ** compl, gsize * num)
1216 gsize len = 0;
1217 gsize max_len = 0;
1218 gsize i;
1219 int skip;
1220 GString *temp;
1221 mc_search_t *srch;
1222 off_t last_byte, start = -1;
1223 char *current_word;
1224 gboolean entire_file;
1225 edit_search_status_msg_t esm;
1227 #ifdef HAVE_CHARSET
1228 srch = mc_search_new (match_expr, cp_source);
1229 #else
1230 srch = mc_search_new (match_expr, NULL);
1231 #endif
1232 if (srch == NULL)
1233 return 0;
1235 entire_file =
1236 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
1237 "editor_wordcompletion_collect_entire_file", 0);
1239 last_byte = entire_file ? edit->buffer.size : word_start;
1241 srch->search_type = MC_SEARCH_T_REGEX;
1242 srch->is_case_sensitive = TRUE;
1243 srch->search_fn = edit_search_cmd_callback;
1244 srch->update_fn = edit_search_update_callback;
1246 esm.first = TRUE;
1247 esm.edit = edit;
1248 esm.offset = entire_file ? 0 : word_start;
1250 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1251 edit_search_status_update_cb, NULL);
1253 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1255 temp = g_string_new ("");
1257 /* collect max MAX_WORD_COMPLETIONS completions */
1258 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1260 g_string_set_size (temp, 0);
1261 start = srch->normal_offset;
1263 /* add matched completion if not yet added */
1264 for (i = 0; i < len; i++)
1266 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1267 if (isspace (skip))
1268 continue;
1270 /* skip current word */
1271 if (start + (off_t) i == word_start)
1272 break;
1274 g_string_append_c (temp, skip);
1277 if (temp->len == 0)
1278 continue;
1280 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1281 continue;
1283 skip = 0;
1285 for (i = 0; i < *num; i++)
1287 if (strncmp
1288 ((char *) &compl[i]->str[word_len],
1289 (char *) &temp->str[word_len], MAX (len, compl[i]->len) - word_len) == 0)
1291 GString *this = compl[i];
1292 for (++i; i < *num; i++)
1293 compl[i - 1] = compl[i];
1294 compl[*num - 1] = this;
1295 skip = 1;
1296 break; /* skip it, already added */
1299 if (skip != 0)
1300 continue;
1302 if (*num == MAX_WORD_COMPLETIONS)
1304 g_string_free (compl[0], TRUE);
1305 for (i = 1; i < *num; i++)
1306 compl[i - 1] = compl[i];
1307 (*num)--;
1309 #ifdef HAVE_CHARSET
1311 GString *recoded;
1312 recoded = str_convert_to_display (temp->str);
1314 if (recoded && recoded->len)
1315 g_string_assign (temp, recoded->str);
1317 g_string_free (recoded, TRUE);
1319 #endif
1320 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1321 start += len;
1323 /* note the maximal length needed for the completion dialog */
1324 if (len > max_len)
1325 max_len = len;
1328 status_msg_deinit (STATUS_MSG (&esm));
1329 mc_search_free (srch);
1330 g_string_free (temp, TRUE);
1331 g_free (current_word);
1333 return max_len;
1336 /* --------------------------------------------------------------------------------------------- */
1338 static void
1339 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1340 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1342 off_t i, cursor;
1343 long col;
1345 cursor = edit->buffer.curs1;
1346 col = edit_get_col (edit);
1348 for (i = 0; i < size; i++)
1350 if (data[i] != '\n')
1351 edit_insert (edit, data[i]);
1352 else
1353 { /* fill in and move to next line */
1354 long l;
1355 off_t p;
1357 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1359 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1360 edit_insert (edit, ' ');
1362 for (p = edit->buffer.curs1;; p++)
1364 if (p == edit->buffer.size)
1366 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1367 edit_insert_ahead (edit, '\n');
1368 p++;
1369 break;
1371 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1373 p++;
1374 break;
1377 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1379 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1380 edit_insert (edit, ' ');
1384 *col1 = col;
1385 *col2 = col + width;
1386 *start_pos = cursor;
1387 *end_pos = edit->buffer.curs1;
1388 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1391 /* --------------------------------------------------------------------------------------------- */
1393 static int
1394 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1396 const macros_t *m1 = (const macros_t *) macro1;
1397 const macros_t *m2 = (const macros_t *) macro2;
1399 return m1->hotkey - m2->hotkey;
1402 /* --------------------------------------------------------------------------------------------- */
1404 static void
1405 edit_macro_sort_by_hotkey (void)
1407 if (macros_list != NULL && macros_list->len != 0)
1408 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1411 /* --------------------------------------------------------------------------------------------- */
1413 static gboolean
1414 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1416 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1417 macros_t *result;
1418 macros_t search_macro;
1420 (void) edit;
1422 search_macro.hotkey = hotkey;
1423 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1424 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1426 if (result != NULL && result->macro != NULL)
1428 *indx = (result - array_start);
1429 *macros = result;
1430 return TRUE;
1432 *indx = 0;
1433 return FALSE;
1436 /* --------------------------------------------------------------------------------------------- */
1437 /** returns FALSE on error */
1439 static gboolean
1440 edit_delete_macro (WEdit * edit, int hotkey)
1442 mc_config_t *macros_config = NULL;
1443 const char *section_name = "editor";
1444 gchar *macros_fname;
1445 guint indx;
1446 char *skeyname;
1447 const macros_t *macros = NULL;
1449 /* clear array of actions for current hotkey */
1450 while (edit_get_macro (edit, hotkey, &macros, &indx))
1452 if (macros->macro != NULL)
1453 g_array_free (macros->macro, TRUE);
1454 macros = NULL;
1455 g_array_remove_index (macros_list, indx);
1456 edit_macro_sort_by_hotkey ();
1459 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1460 macros_config = mc_config_init (macros_fname, FALSE);
1461 g_free (macros_fname);
1463 if (macros_config == NULL)
1464 return FALSE;
1466 skeyname = lookup_key_by_code (hotkey);
1467 while (mc_config_del_key (macros_config, section_name, skeyname))
1469 g_free (skeyname);
1470 mc_config_save_file (macros_config, NULL);
1471 mc_config_deinit (macros_config);
1472 return TRUE;
1475 /* --------------------------------------------------------------------------------------------- */
1477 * Callback for the iteration of objects in the 'editors' array.
1478 * Toggle syntax highlighting in editor object.
1480 * @param data probably WEdit object
1481 * @param user_data unused
1484 static void
1485 edit_syntax_onoff_cb (void *data, void *user_data)
1487 (void) user_data;
1489 if (edit_widget_is_editor (CONST_WIDGET (data)))
1491 WEdit *edit = (WEdit *) data;
1493 if (option_syntax_highlighting)
1494 edit_load_syntax (edit, NULL, edit->syntax_type);
1495 edit->force |= REDRAW_PAGE;
1499 /* --------------------------------------------------------------------------------------------- */
1501 * Callback for the iteration of objects in the 'editors' array.
1502 * Redraw editor object.
1504 * @param data probably WEdit object
1505 * @param user_data unused
1508 static void
1509 edit_redraw_page_cb (void *data, void *user_data)
1511 (void) user_data;
1513 if (edit_widget_is_editor (CONST_WIDGET (data)))
1514 ((WEdit *) data)->force |= REDRAW_PAGE;
1517 /* --------------------------------------------------------------------------------------------- */
1519 * Insert autocompleted word into editor.
1521 * @param edit editor object
1522 * @param completion word for completion
1523 * @param word_len offset from beginning for insert
1526 static void
1527 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1529 #ifdef HAVE_CHARSET
1530 GString *temp;
1532 temp = str_convert_to_input (completion);
1534 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1535 edit_insert (edit, *completion);
1536 g_string_free (temp, TRUE);
1537 #else
1538 for (completion += word_len; *completion != '\0'; completion++)
1539 edit_insert (edit, *completion);
1540 #endif
1543 /* --------------------------------------------------------------------------------------------- */
1544 /*** public functions ****************************************************************************/
1545 /* --------------------------------------------------------------------------------------------- */
1547 void
1548 edit_refresh_cmd (void)
1550 clr_scr ();
1551 repaint_screen ();
1552 tty_keypad (TRUE);
1555 /* --------------------------------------------------------------------------------------------- */
1557 * Toggle syntax highlighting in all editor windows.
1559 * @param h root widget for all windows
1562 void
1563 edit_syntax_onoff_cmd (WDialog * h)
1565 option_syntax_highlighting = !option_syntax_highlighting;
1566 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1567 dlg_redraw (h);
1570 /* --------------------------------------------------------------------------------------------- */
1572 * Toggle tabs showing in all editor windows.
1574 * @param h root widget for all windows
1577 void
1578 edit_show_tabs_tws_cmd (WDialog * h)
1580 enable_show_tabs_tws = !enable_show_tabs_tws;
1581 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1582 dlg_redraw (h);
1585 /* --------------------------------------------------------------------------------------------- */
1587 * Toggle right margin showing in all editor windows.
1589 * @param h root widget for all windows
1592 void
1593 edit_show_margin_cmd (WDialog * h)
1595 show_right_margin = !show_right_margin;
1596 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1597 dlg_redraw (h);
1600 /* --------------------------------------------------------------------------------------------- */
1602 * Toggle line numbers showing in all editor windows.
1604 * @param h root widget for all windows
1607 void
1608 edit_show_numbers_cmd (WDialog * h)
1610 option_line_state = !option_line_state;
1611 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1612 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1613 dlg_redraw (h);
1616 /* --------------------------------------------------------------------------------------------- */
1618 void
1619 edit_save_mode_cmd (void)
1621 char *str_result = NULL;
1623 const char *str[] = {
1624 N_("&Quick save"),
1625 N_("&Safe save"),
1626 N_("&Do backups with following extension:")
1629 #ifdef HAVE_ASSERT_H
1630 assert (option_backup_ext != NULL);
1631 #endif
1633 #ifdef ENABLE_NLS
1634 size_t i;
1636 for (i = 0; i < 3; i++)
1637 str[i] = _(str[i]);
1638 #endif
1641 quick_widget_t quick_widgets[] = {
1642 /* *INDENT-OFF* */
1643 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1644 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1645 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1646 QUICK_SEPARATOR (TRUE),
1647 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1648 QUICK_BUTTONS_OK_CANCEL,
1649 QUICK_END
1650 /* *INDENT-ON* */
1653 quick_dialog_t qdlg = {
1654 -1, -1, 38,
1655 N_("Edit Save Mode"), "[Edit Save Mode]",
1656 quick_widgets, edit_save_mode_callback, NULL
1659 if (quick_dialog (&qdlg) != B_CANCEL)
1661 g_free (option_backup_ext);
1662 option_backup_ext = str_result;
1667 /* --------------------------------------------------------------------------------------------- */
1669 void
1670 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1672 vfs_path_free (edit->filename_vpath);
1673 edit->filename_vpath = vfs_path_clone (name_vpath);
1675 if (edit->dir_vpath == NULL)
1676 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1679 /* --------------------------------------------------------------------------------------------- */
1680 /* Here we want to warn the users of overwriting an existing file,
1681 but only if they have made a change to the filename */
1682 /* returns TRUE on success */
1683 gboolean
1684 edit_save_as_cmd (WEdit * edit)
1686 /* This heads the 'Save As' dialog box */
1687 vfs_path_t *exp_vpath;
1688 int save_lock = 0;
1689 int different_filename = 0;
1691 if (!edit_check_newline (&edit->buffer))
1692 return FALSE;
1694 exp_vpath = edit_get_save_file_as (edit);
1695 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1697 if (exp_vpath != NULL)
1699 if (vfs_path_len (exp_vpath) == 0)
1700 goto ret;
1701 else
1703 int rv;
1705 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1707 int file;
1708 struct stat sb;
1710 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1712 edit_error_dialog (_("Save as"),
1713 get_sys_error (_
1714 ("Cannot save: destination is not a regular file")));
1715 goto ret;
1718 different_filename = 1;
1719 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1721 if (file != -1)
1723 /* the file exists */
1724 mc_close (file);
1725 /* Overwrite the current file or cancel the operation */
1726 if (edit_query_dialog2
1727 (_("Warning"),
1728 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1729 goto ret;
1731 else
1733 edit->stat1.st_mode |= S_IWUSR;
1735 save_lock = lock_file (exp_vpath);
1737 else
1739 /* filenames equal, check if already locked */
1740 if (!edit->locked && !edit->delete_file)
1741 save_lock = lock_file (exp_vpath);
1744 if (different_filename)
1747 * Allow user to write into saved (under another name) file
1748 * even if original file had r/o user permissions.
1750 edit->stat1.st_mode |= S_IWRITE;
1753 rv = edit_save_file (edit, exp_vpath);
1754 switch (rv)
1756 case 1:
1757 /* Successful, so unlock both files */
1758 if (different_filename)
1760 if (save_lock)
1761 unlock_file (exp_vpath);
1762 if (edit->locked)
1763 edit->locked = unlock_file (edit->filename_vpath);
1765 else
1767 if (edit->locked || save_lock)
1768 edit->locked = unlock_file (edit->filename_vpath);
1771 edit_set_filename (edit, exp_vpath);
1772 if (edit->lb != LB_ASIS)
1773 edit_reload (edit, exp_vpath);
1774 edit->modified = 0;
1775 edit->delete_file = 0;
1776 if (different_filename)
1777 edit_load_syntax (edit, NULL, edit->syntax_type);
1778 vfs_path_free (exp_vpath);
1779 edit->force |= REDRAW_COMPLETELY;
1780 return TRUE;
1781 default:
1782 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1783 /* fallthrough */
1784 case -1:
1785 /* Failed, so maintain modify (not save) lock */
1786 if (save_lock)
1787 unlock_file (exp_vpath);
1788 break;
1793 ret:
1794 vfs_path_free (exp_vpath);
1795 edit->force |= REDRAW_COMPLETELY;
1796 return FALSE;
1799 /* {{{ Macro stuff starts here */
1800 /* --------------------------------------------------------------------------------------------- */
1802 void
1803 edit_delete_macro_cmd (WEdit * edit)
1805 int hotkey;
1807 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1809 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1810 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1813 /* --------------------------------------------------------------------------------------------- */
1815 /** returns FALSE on error */
1816 gboolean
1817 edit_execute_macro (WEdit * edit, int hotkey)
1819 gboolean res = FALSE;
1821 if (hotkey != 0)
1823 const macros_t *macros;
1824 guint indx;
1826 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1827 macros->macro != NULL && macros->macro->len != 0)
1829 guint i;
1831 edit->force |= REDRAW_PAGE;
1833 for (i = 0; i < macros->macro->len; i++)
1835 const macro_action_t *m_act;
1837 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1838 edit_execute_cmd (edit, m_act->action, m_act->ch);
1839 res = TRUE;
1844 return res;
1847 /* --------------------------------------------------------------------------------------------- */
1849 /** returns FALSE on error */
1850 gboolean
1851 edit_store_macro_cmd (WEdit * edit)
1853 int i;
1854 int hotkey;
1855 GString *marcros_string;
1856 mc_config_t *macros_config = NULL;
1857 const char *section_name = "editor";
1858 gchar *macros_fname;
1859 GArray *macros; /* current macro */
1860 int tmp_act;
1861 gboolean have_macro = FALSE;
1862 char *skeyname = NULL;
1864 hotkey =
1865 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1866 if (hotkey == ESC_CHAR)
1867 return FALSE;
1869 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1871 /* return FALSE if try assign macro into restricted hotkeys */
1872 if (tmp_act == CK_MacroStartRecord
1873 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1874 return FALSE;
1876 edit_delete_macro (edit, hotkey);
1878 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1879 macros_config = mc_config_init (macros_fname, FALSE);
1880 g_free (macros_fname);
1882 if (macros_config == NULL)
1883 return FALSE;
1885 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1887 marcros_string = g_string_sized_new (250);
1888 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1890 skeyname = lookup_key_by_code (hotkey);
1892 for (i = 0; i < macro_index; i++)
1894 macro_action_t m_act;
1895 const char *action_name;
1897 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1899 if (action_name == NULL)
1900 break;
1902 m_act.action = record_macro_buf[i].action;
1903 m_act.ch = record_macro_buf[i].ch;
1904 g_array_append_val (macros, m_act);
1905 have_macro = TRUE;
1906 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1907 (int) record_macro_buf[i].ch);
1909 if (have_macro)
1911 macros_t macro;
1912 macro.hotkey = hotkey;
1913 macro.macro = macros;
1914 g_array_append_val (macros_list, macro);
1915 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1917 else
1918 mc_config_del_key (macros_config, section_name, skeyname);
1920 g_free (skeyname);
1921 edit_macro_sort_by_hotkey ();
1923 g_string_free (marcros_string, TRUE);
1924 mc_config_save_file (macros_config, NULL);
1925 mc_config_deinit (macros_config);
1926 return TRUE;
1929 /* --------------------------------------------------------------------------------------------- */
1931 gboolean
1932 edit_repeat_macro_cmd (WEdit * edit)
1934 int i, j;
1935 char *f;
1936 long count_repeat;
1937 char *error = NULL;
1939 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1940 INPUT_COMPLETE_NONE);
1941 if (f == NULL || *f == '\0')
1943 g_free (f);
1944 return FALSE;
1947 count_repeat = strtol (f, &error, 0);
1949 if (*error != '\0')
1951 g_free (f);
1952 return FALSE;
1955 g_free (f);
1957 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1958 edit->force |= REDRAW_PAGE;
1960 for (j = 0; j < count_repeat; j++)
1961 for (i = 0; i < macro_index; i++)
1962 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1963 edit_update_screen (edit);
1964 return TRUE;
1967 /* --------------------------------------------------------------------------------------------- */
1968 /** return FALSE on error */
1970 gboolean
1971 edit_load_macro_cmd (WEdit * edit)
1973 mc_config_t *macros_config = NULL;
1974 gchar **profile_keys, **keys;
1975 gchar **values, **curr_values;
1976 const char *section_name = "editor";
1977 gchar *macros_fname;
1979 (void) edit;
1981 if (macros_list == NULL || macros_list->len != 0)
1982 return FALSE;
1984 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1985 macros_config = mc_config_init (macros_fname, TRUE);
1986 g_free (macros_fname);
1988 if (macros_config == NULL)
1989 return FALSE;
1991 keys = mc_config_get_keys (macros_config, section_name, NULL);
1993 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1995 int hotkey;
1996 gboolean have_macro = FALSE;
1997 GArray *macros;
1998 macros_t macro;
2000 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
2001 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
2002 hotkey = lookup_key (*profile_keys, NULL);
2004 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2006 char **macro_pair = NULL;
2008 macro_pair = g_strsplit (*curr_values, ":", 2);
2009 if (macro_pair != NULL)
2011 macro_action_t m_act;
2012 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2013 m_act.action = 0;
2014 else
2016 m_act.action = keybind_lookup_action (macro_pair[0]);
2017 MC_PTR_FREE (macro_pair[0]);
2019 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2020 m_act.ch = -1;
2021 else
2023 m_act.ch = strtol (macro_pair[1], NULL, 0);
2024 MC_PTR_FREE (macro_pair[1]);
2026 if (m_act.action != 0)
2028 /* a shell command */
2029 if ((m_act.action / CK_PipeBlock (0)) == 1)
2031 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2032 m_act.ch = -1;
2034 g_array_append_val (macros, m_act);
2035 have_macro = TRUE;
2037 g_strfreev (macro_pair);
2038 macro_pair = NULL;
2041 if (have_macro)
2043 macro.hotkey = hotkey;
2044 macro.macro = macros;
2045 g_array_append_val (macros_list, macro);
2047 g_strfreev (values);
2049 g_strfreev (keys);
2050 mc_config_deinit (macros_config);
2051 edit_macro_sort_by_hotkey ();
2052 return TRUE;
2055 /* }}} Macro stuff end here */
2057 /* --------------------------------------------------------------------------------------------- */
2058 /** returns TRUE on success */
2060 gboolean
2061 edit_save_confirm_cmd (WEdit * edit)
2063 if (edit->filename_vpath == NULL)
2064 return edit_save_as_cmd (edit);
2066 if (!edit_check_newline (&edit->buffer))
2067 return FALSE;
2069 if (edit_confirm_save)
2071 char *f;
2072 gboolean ok;
2074 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2075 vfs_path_as_str (edit->filename_vpath));
2076 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2077 g_free (f);
2078 if (!ok)
2079 return FALSE;
2081 return edit_save_cmd (edit);
2084 /* --------------------------------------------------------------------------------------------- */
2086 * Ask file to edit and load it.
2088 * @return TRUE on success or cancel of ask.
2091 gboolean
2092 edit_load_cmd (WDialog * h)
2094 char *exp;
2095 gboolean ret = TRUE; /* possible cancel */
2097 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2098 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2099 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2101 if (exp != NULL && *exp != '\0')
2103 vfs_path_t *exp_vpath;
2105 exp_vpath = vfs_path_from_str (exp);
2106 ret = edit_load_file_from_filename (h, exp_vpath);
2107 vfs_path_free (exp_vpath);
2110 g_free (exp);
2112 return ret;
2115 /* --------------------------------------------------------------------------------------------- */
2117 * Load syntax file to edit.
2119 * @return TRUE on success
2122 gboolean
2123 edit_load_syntax_file (WDialog * h)
2125 vfs_path_t *extdir_vpath;
2126 int dir = 0;
2127 gboolean ret = FALSE;
2129 if (geteuid () == 0)
2130 dir = query_dialog (_("Syntax file edit"),
2131 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2132 _("&User"), _("&System wide"));
2134 extdir_vpath =
2135 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2136 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2138 vfs_path_free (extdir_vpath);
2139 extdir_vpath =
2140 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2143 if (dir == 0)
2145 vfs_path_t *user_syntax_file_vpath;
2147 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2148 check_for_default (extdir_vpath, user_syntax_file_vpath);
2149 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2150 vfs_path_free (user_syntax_file_vpath);
2152 else if (dir == 1)
2153 ret = edit_load_file_from_filename (h, extdir_vpath);
2155 vfs_path_free (extdir_vpath);
2157 return ret;
2160 /* --------------------------------------------------------------------------------------------- */
2162 * Load menu file to edit.
2164 * @return TRUE on success
2167 gboolean
2168 edit_load_menu_file (WDialog * h)
2170 vfs_path_t *buffer_vpath;
2171 vfs_path_t *menufile_vpath;
2172 int dir;
2173 gboolean ret;
2175 query_set_sel (1);
2176 dir = query_dialog (_("Menu edit"),
2177 _("Which menu file do you want to edit?"), D_NORMAL,
2178 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2180 menufile_vpath =
2181 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2182 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2184 vfs_path_free (menufile_vpath);
2185 menufile_vpath =
2186 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2189 switch (dir)
2191 case 0:
2192 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2193 check_for_default (menufile_vpath, buffer_vpath);
2194 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2195 break;
2197 case 1:
2198 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2199 check_for_default (menufile_vpath, buffer_vpath);
2200 break;
2202 case 2:
2203 buffer_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 (buffer_vpath)))
2207 vfs_path_free (buffer_vpath);
2208 buffer_vpath =
2209 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2211 break;
2213 default:
2214 vfs_path_free (menufile_vpath);
2215 return FALSE;
2218 ret = edit_load_file_from_filename (h, buffer_vpath);
2220 vfs_path_free (buffer_vpath);
2221 vfs_path_free (menufile_vpath);
2223 return ret;
2226 /* --------------------------------------------------------------------------------------------- */
2228 * Close window with opened file.
2230 * @return TRUE if file was closed.
2233 gboolean
2234 edit_close_cmd (WEdit * edit)
2236 gboolean ret;
2238 ret = (edit != NULL) && edit_ok_to_exit (edit);
2240 if (ret)
2242 WDialog *h = WIDGET (edit)->owner;
2244 if (edit->locked != 0)
2245 unlock_file (edit->filename_vpath);
2247 del_widget (edit);
2249 if (edit_widget_is_editor (CONST_WIDGET (h->current->data)))
2250 edit = (WEdit *) h->current->data;
2251 else
2253 edit = find_editor (h);
2254 if (edit != NULL)
2255 dlg_set_top_widget (edit);
2259 if (edit != NULL)
2260 edit->force |= REDRAW_COMPLETELY;
2262 return ret;
2265 /* --------------------------------------------------------------------------------------------- */
2267 if mark2 is -1 then marking is from mark1 to the cursor.
2268 Otherwise its between the markers. This handles this.
2269 Returns FALSE if no text is marked.
2272 gboolean
2273 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2275 long end_mark_curs;
2277 if (edit->mark1 == edit->mark2)
2279 *start_mark = *end_mark = 0;
2280 edit->column2 = edit->column1 = 0;
2281 return FALSE;
2284 if (edit->end_mark_curs < 0)
2285 end_mark_curs = edit->buffer.curs1;
2286 else
2287 end_mark_curs = edit->end_mark_curs;
2289 if (edit->mark2 >= 0)
2291 *start_mark = MIN (edit->mark1, edit->mark2);
2292 *end_mark = MAX (edit->mark1, edit->mark2);
2294 else
2296 *start_mark = MIN (edit->mark1, end_mark_curs);
2297 *end_mark = MAX (edit->mark1, end_mark_curs);
2298 edit->column2 = edit->curs_col + edit->over_col;
2301 if (edit->column_highlight
2302 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2303 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2305 off_t start_bol, start_eol;
2306 off_t end_bol, end_eol;
2307 long col1, col2;
2308 off_t diff1, diff2;
2310 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2311 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2312 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2313 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2314 col1 = MIN (edit->column1, edit->column2);
2315 col2 = MAX (edit->column1, edit->column2);
2317 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2318 edit_move_forward3 (edit, start_bol, col1, 0);
2319 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2320 edit_move_forward3 (edit, end_bol, col1, 0);
2322 *start_mark -= diff1;
2323 *end_mark += diff2;
2324 *start_mark = MAX (*start_mark, start_eol);
2325 *end_mark = MIN (*end_mark, end_eol);
2327 return TRUE;
2330 /* --------------------------------------------------------------------------------------------- */
2332 void
2333 edit_block_copy_cmd (WEdit * edit)
2335 off_t start_mark, end_mark, current = edit->buffer.curs1;
2336 off_t mark1 = 0, mark2 = 0;
2337 long c1 = 0, c2 = 0;
2338 off_t size;
2339 unsigned char *copy_buf;
2341 edit_update_curs_col (edit);
2342 if (!eval_marks (edit, &start_mark, &end_mark))
2343 return;
2345 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2347 /* all that gets pushed are deletes hence little space is used on the stack */
2349 edit_push_markers (edit);
2351 if (edit->column_highlight)
2353 long col_delta;
2355 col_delta = labs (edit->column2 - edit->column1);
2356 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2358 else
2360 int size_orig = size;
2362 while (size-- != 0)
2363 edit_insert_ahead (edit, copy_buf[size]);
2365 /* Place cursor at the end of text selection */
2366 if (option_cursor_after_inserted_block)
2367 edit_cursor_move (edit, size_orig);
2370 g_free (copy_buf);
2371 edit_scroll_screen_over_cursor (edit);
2373 if (edit->column_highlight)
2374 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2375 else if (start_mark < current && end_mark > current)
2376 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2378 edit->force |= REDRAW_PAGE;
2382 /* --------------------------------------------------------------------------------------------- */
2384 void
2385 edit_block_move_cmd (WEdit * edit)
2387 off_t current;
2388 unsigned char *copy_buf = NULL;
2389 off_t start_mark, end_mark;
2391 if (!eval_marks (edit, &start_mark, &end_mark))
2392 return;
2394 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2395 return;
2397 if (edit->mark2 < 0)
2398 edit_mark_cmd (edit, FALSE);
2399 edit_push_markers (edit);
2401 if (edit->column_highlight)
2403 off_t mark1, mark2;
2404 off_t size;
2405 long c1, c2, b_width;
2406 long x, x2;
2408 c1 = MIN (edit->column1, edit->column2);
2409 c2 = MAX (edit->column1, edit->column2);
2410 b_width = c2 - c1;
2412 edit_update_curs_col (edit);
2414 x = edit->curs_col;
2415 x2 = x + edit->over_col;
2417 /* do nothing when cursor inside first line of selected area */
2418 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2419 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2420 return;
2422 if (edit->buffer.curs1 > start_mark
2423 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2425 if (x > c2)
2426 x -= b_width;
2427 else if (x > c1 && x <= c2)
2428 x = c1;
2430 /* save current selection into buffer */
2431 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2433 /* remove current selection */
2434 edit_block_delete_cmd (edit);
2436 edit->over_col = MAX (0, edit->over_col - b_width);
2437 /* calculate the cursor pos after delete block */
2438 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2439 edit_cursor_move (edit, current - edit->buffer.curs1);
2440 edit_scroll_screen_over_cursor (edit);
2442 /* add TWS if need before block insertion */
2443 if (option_cursor_beyond_eol && edit->over_col > 0)
2444 edit_insert_over (edit);
2446 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2447 edit_set_markers (edit, mark1, mark2, c1, c2);
2449 else
2451 off_t count, count_orig;
2453 current = edit->buffer.curs1;
2454 copy_buf = g_malloc0 (end_mark - start_mark);
2455 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2456 edit_scroll_screen_over_cursor (edit);
2458 for (count = start_mark; count < end_mark; count++)
2459 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2461 edit_scroll_screen_over_cursor (edit);
2462 edit_cursor_move (edit,
2463 current - edit->buffer.curs1 -
2464 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2465 edit_scroll_screen_over_cursor (edit);
2466 count_orig = count;
2467 while (count-- > start_mark)
2468 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2470 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2473 /* Place cursor at the end of text selection */
2474 if (option_cursor_after_inserted_block)
2475 edit_cursor_move (edit, count_orig - start_mark);
2478 edit_scroll_screen_over_cursor (edit);
2479 g_free (copy_buf);
2480 edit->force |= REDRAW_PAGE;
2483 /* --------------------------------------------------------------------------------------------- */
2484 /** returns 1 if canceelled by user */
2487 edit_block_delete_cmd (WEdit * edit)
2489 off_t start_mark, end_mark;
2491 if (eval_marks (edit, &start_mark, &end_mark))
2492 return edit_block_delete (edit);
2494 edit_delete_line (edit);
2495 return 0;
2498 /* --------------------------------------------------------------------------------------------- */
2499 /** call with edit = 0 before shutdown to close memory leaks */
2501 void
2502 edit_replace_cmd (WEdit * edit, gboolean again)
2504 /* 1 = search string, 2 = replace with */
2505 static char *saved1 = NULL; /* saved default[123] */
2506 static char *saved2 = NULL;
2507 char *input1 = NULL; /* user input from the dialog */
2508 char *input2 = NULL;
2509 GString *input2_str = NULL;
2510 char *disp1 = NULL;
2511 char *disp2 = NULL;
2512 long times_replaced = 0;
2513 gboolean once_found = FALSE;
2514 edit_search_status_msg_t esm;
2516 if (edit == NULL)
2518 MC_PTR_FREE (saved1);
2519 MC_PTR_FREE (saved2);
2520 return;
2523 edit->force |= REDRAW_COMPLETELY;
2525 if (again && !saved1 && !saved2)
2526 again = FALSE;
2528 if (again)
2530 input1 = g_strdup (saved1 ? saved1 : "");
2531 input2 = g_strdup (saved2 ? saved2 : "");
2533 else
2535 char *tmp_inp1, *tmp_inp2;
2537 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : "");
2538 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : "");
2540 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2542 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2544 g_free (disp1);
2545 g_free (disp2);
2547 if (input1 == NULL || *input1 == '\0')
2549 edit->force = REDRAW_COMPLETELY;
2550 goto cleanup;
2553 tmp_inp1 = input1;
2554 tmp_inp2 = input2;
2555 input1 = edit_replace_cmd__conv_to_input (input1);
2556 input2 = edit_replace_cmd__conv_to_input (input2);
2557 g_free (tmp_inp1);
2558 g_free (tmp_inp2);
2560 g_free (saved1), saved1 = g_strdup (input1);
2561 g_free (saved2), saved2 = g_strdup (input2);
2563 mc_search_free (edit->search);
2564 edit->search = NULL;
2567 input2_str = g_string_new (input2);
2569 if (edit->search == NULL)
2571 #ifdef HAVE_CHARSET
2572 edit->search = mc_search_new (input1, cp_source);
2573 #else
2574 edit->search = mc_search_new (input1, NULL);
2575 #endif
2576 if (edit->search == NULL)
2578 edit->search_start = edit->buffer.curs1;
2579 goto cleanup;
2581 edit->search->search_type = edit_search_options.type;
2582 #ifdef HAVE_CHARSET
2583 edit->search->is_all_charsets = edit_search_options.all_codepages;
2584 #endif
2585 edit->search->is_case_sensitive = edit_search_options.case_sens;
2586 edit->search->whole_words = edit_search_options.whole_words;
2587 edit->search->search_fn = edit_search_cmd_callback;
2588 edit->search->update_fn = edit_search_update_callback;
2589 edit->search_line_type = edit_get_search_line_type (edit->search);
2590 edit_search_fix_search_start_if_selection (edit);
2593 if (edit->found_len && edit->search_start == edit->found_start + 1
2594 && edit_search_options.backwards)
2595 edit->search_start--;
2597 if (edit->found_len && edit->search_start == edit->found_start - 1
2598 && !edit_search_options.backwards)
2599 edit->search_start++;
2601 esm.first = TRUE;
2602 esm.edit = edit;
2603 esm.offset = edit->search_start;
2605 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2606 edit_search_status_update_cb, NULL);
2610 gsize len = 0;
2612 if (!editcmd_find (&esm, &len))
2614 if (!(edit->search->error == MC_SEARCH_E_OK ||
2615 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND))
2616 && edit->search->error_str != NULL)
2617 edit_query_dialog (_("Search"), edit->search->error_str);
2619 break;
2621 once_found = TRUE;
2623 edit->search_start = edit->search->normal_offset;
2624 /*returns negative on not found or error in pattern */
2626 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2628 gsize i;
2629 GString *repl_str;
2631 edit->found_start = edit->search_start;
2632 i = edit->found_len = len;
2634 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2635 edit_scroll_screen_over_cursor (edit);
2637 if (edit->replace_mode == 0)
2639 long l;
2640 int prompt;
2642 l = edit->curs_row - WIDGET (edit)->lines / 3;
2643 if (l > 0)
2644 edit_scroll_downward (edit, l);
2645 if (l < 0)
2646 edit_scroll_upward (edit, -l);
2648 edit_scroll_screen_over_cursor (edit);
2649 edit->force |= REDRAW_PAGE;
2650 edit_render_keypress (edit);
2652 /*so that undo stops at each query */
2653 edit_push_key_press (edit);
2654 /* and prompt 2/3 down */
2655 disp1 = edit_replace_cmd__conv_to_display (saved1);
2656 disp2 = edit_replace_cmd__conv_to_display (saved2);
2657 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2658 g_free (disp1);
2659 g_free (disp2);
2661 if (prompt == B_REPLACE_ALL)
2662 edit->replace_mode = 1;
2663 else if (prompt == B_SKIP_REPLACE)
2665 if (edit_search_options.backwards)
2666 edit->search_start--;
2667 else
2668 edit->search_start++;
2669 continue; /* loop */
2671 else if (prompt == B_CANCEL)
2673 edit->replace_mode = -1;
2674 break; /* loop */
2678 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2680 if (edit->search->error != MC_SEARCH_E_OK)
2682 if (edit->search->error_str != NULL)
2683 edit_error_dialog (_("Replace"), edit->search->error_str);
2685 g_string_free (repl_str, TRUE);
2686 break;
2689 /* delete then insert new */
2690 for (i = 0; i < len; i++)
2691 edit_delete (edit, TRUE);
2693 for (i = 0; i < repl_str->len; i++)
2694 edit_insert (edit, repl_str->str[i]);
2696 edit->found_len = repl_str->len;
2697 g_string_free (repl_str, TRUE);
2698 times_replaced++;
2700 /* so that we don't find the same string again */
2701 if (edit_search_options.backwards)
2703 edit->search_start--;
2705 else
2707 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2709 if (edit->search_start >= edit->buffer.size)
2710 break;
2713 edit_scroll_screen_over_cursor (edit);
2715 else
2717 /* try and find from right here for next search */
2718 edit->search_start = edit->buffer.curs1;
2719 edit_update_curs_col (edit);
2721 edit->force |= REDRAW_PAGE;
2722 edit_render_keypress (edit);
2724 if (times_replaced == 0)
2725 query_dialog (_("Replace"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&OK"));
2726 break;
2729 while (edit->replace_mode >= 0);
2731 status_msg_deinit (STATUS_MSG (&esm));
2732 edit_scroll_screen_over_cursor (edit);
2733 edit->force |= REDRAW_COMPLETELY;
2734 edit_render_keypress (edit);
2736 if ((edit->replace_mode == 1) && (times_replaced != 0))
2737 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2739 cleanup:
2740 g_free (input1);
2741 g_free (input2);
2742 if (input2_str != NULL)
2743 g_string_free (input2_str, TRUE);
2746 /* --------------------------------------------------------------------------------------------- */
2748 mc_search_cbret_t
2749 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2751 WEdit *edit = ((const edit_search_status_msg_t *) user_data)->edit;
2753 *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2754 return MC_SEARCH_CB_OK;
2757 /* --------------------------------------------------------------------------------------------- */
2759 mc_search_cbret_t
2760 edit_search_update_callback (const void *user_data, gsize char_offset)
2762 status_msg_t *sm = STATUS_MSG (user_data);
2764 ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2766 return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2769 /* --------------------------------------------------------------------------------------------- */
2771 void
2772 edit_search_cmd (WEdit * edit, gboolean again)
2775 if (edit == NULL)
2776 return;
2778 if (!again)
2779 edit_search (edit);
2780 else if (edit->last_search_string != NULL)
2781 edit_do_search (edit);
2782 else
2784 /* find last search string in history */
2785 GList *history;
2787 history = history_get (MC_HISTORY_SHARED_SEARCH);
2788 if (history != NULL && history->data != NULL)
2790 edit->last_search_string = (char *) history->data;
2791 history->data = NULL;
2792 history = g_list_first (history);
2793 g_list_free_full (history, g_free);
2795 #ifdef HAVE_CHARSET
2796 edit->search = mc_search_new (edit->last_search_string, cp_source);
2797 #else
2798 edit->search = mc_search_new (edit->last_search_string, NULL);
2799 #endif
2800 if (edit->search == NULL)
2802 /* if not... then ask for an expression */
2803 MC_PTR_FREE (edit->last_search_string);
2804 edit_search (edit);
2806 else
2808 edit->search->search_type = edit_search_options.type;
2809 #ifdef HAVE_CHARSET
2810 edit->search->is_all_charsets = edit_search_options.all_codepages;
2811 #endif
2812 edit->search->is_case_sensitive = edit_search_options.case_sens;
2813 edit->search->whole_words = edit_search_options.whole_words;
2814 edit->search->search_fn = edit_search_cmd_callback;
2815 edit->search->update_fn = edit_search_update_callback;
2816 edit->search_line_type = edit_get_search_line_type (edit->search);
2817 edit_do_search (edit);
2820 else
2822 /* if not... then ask for an expression */
2823 MC_PTR_FREE (edit->last_search_string);
2824 edit_search (edit);
2830 /* --------------------------------------------------------------------------------------------- */
2832 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2834 * @return TRUE if it's OK to exit, FALSE to continue editing.
2837 gboolean
2838 edit_ok_to_exit (WEdit * edit)
2840 const char *fname = N_("[NoName]");
2841 char *msg;
2842 int act;
2844 if (!edit->modified)
2845 return TRUE;
2847 if (edit->filename_vpath != NULL)
2848 fname = vfs_path_as_str (edit->filename_vpath);
2849 #ifdef ENABLE_NLS
2850 else
2851 fname = _(fname);
2852 #endif
2854 if (!mc_global.midnight_shutdown)
2856 query_set_sel (2);
2858 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2859 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2861 else
2863 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2864 fname);
2865 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2867 /* Esc is No */
2868 if (act == -1)
2869 act = 1;
2872 g_free (msg);
2874 switch (act)
2876 case 0: /* Yes */
2877 if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2878 return FALSE;
2879 edit_push_markers (edit);
2880 edit_set_markers (edit, 0, 0, 0, 0);
2881 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2882 return mc_global.midnight_shutdown;
2883 break;
2884 case 1: /* No */
2885 default:
2886 break;
2887 case 2: /* Cancel quit */
2888 case -1: /* Esc */
2889 return FALSE;
2892 return TRUE;
2895 /* --------------------------------------------------------------------------------------------- */
2896 /** save block, returns TRUE on success */
2898 gboolean
2899 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2901 int file;
2902 off_t len = 1;
2903 vfs_path_t *vpath;
2905 vpath = vfs_path_from_str (filename);
2906 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2907 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2908 vfs_path_free (vpath);
2909 if (file == -1)
2910 return FALSE;
2912 if (edit->column_highlight)
2914 int r;
2916 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2917 if (r > 0)
2919 unsigned char *block, *p;
2921 p = block = edit_get_block (edit, start, finish, &len);
2922 while (len)
2924 r = mc_write (file, p, len);
2925 if (r < 0)
2926 break;
2927 p += r;
2928 len -= r;
2930 g_free (block);
2933 else
2935 unsigned char *buf;
2936 off_t i = start;
2937 off_t end;
2939 len = finish - start;
2940 buf = g_malloc0 (TEMP_BUF_LEN);
2941 while (start != finish)
2943 end = MIN (finish, start + TEMP_BUF_LEN);
2944 for (; i < end; i++)
2945 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2946 len -= mc_write (file, (char *) buf, end - start);
2947 start = end;
2949 g_free (buf);
2951 mc_close (file);
2953 return (len == 0);
2956 /* --------------------------------------------------------------------------------------------- */
2958 void
2959 edit_paste_from_history (WEdit * edit)
2961 (void) edit;
2962 edit_error_dialog (_("Error"), _("This function is not implemented"));
2965 /* --------------------------------------------------------------------------------------------- */
2967 gboolean
2968 edit_copy_to_X_buf_cmd (WEdit * edit)
2970 off_t start_mark, end_mark;
2972 if (!eval_marks (edit, &start_mark, &end_mark))
2973 return TRUE;
2975 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2977 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2978 return FALSE;
2980 /* try use external clipboard utility */
2981 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2983 if (option_drop_selection_on_copy)
2984 edit_mark_cmd (edit, TRUE);
2986 return TRUE;
2989 /* --------------------------------------------------------------------------------------------- */
2991 gboolean
2992 edit_cut_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 (_("Cut to clipboard"), _("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 edit_block_delete_cmd (edit);
3008 edit_mark_cmd (edit, TRUE);
3010 return TRUE;
3013 /* --------------------------------------------------------------------------------------------- */
3015 gboolean
3016 edit_paste_from_X_buf_cmd (WEdit * edit)
3018 vfs_path_t *tmp;
3019 gboolean ret;
3021 /* try use external clipboard utility */
3022 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3023 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3024 ret = (edit_insert_file (edit, tmp) >= 0);
3025 vfs_path_free (tmp);
3027 return ret;
3030 /* --------------------------------------------------------------------------------------------- */
3032 * Ask user for the line and go to that line.
3033 * Negative numbers mean line from the end (i.e. -1 is the last line).
3036 void
3037 edit_goto_cmd (WEdit * edit)
3039 static gboolean first_run = TRUE;
3041 char *f;
3042 long l;
3043 char *error;
3045 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3046 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3047 if (f == NULL || *f == '\0')
3049 g_free (f);
3050 return;
3053 l = strtol (f, &error, 0);
3054 if (*error != '\0')
3056 g_free (f);
3057 return;
3060 if (l < 0)
3061 l = edit->buffer.lines + l + 2;
3062 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3063 edit_move_to_line (edit, l - 1);
3064 edit->force |= REDRAW_COMPLETELY;
3065 g_free (f);
3067 first_run = FALSE;
3071 /* --------------------------------------------------------------------------------------------- */
3072 /** Return TRUE on success */
3074 gboolean
3075 edit_save_block_cmd (WEdit * edit)
3077 off_t start_mark, end_mark;
3078 char *exp, *tmp;
3079 gboolean ret = FALSE;
3081 if (!eval_marks (edit, &start_mark, &end_mark))
3082 return TRUE;
3084 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3085 exp =
3086 input_expand_dialog (_("Save block"), _("Enter file name:"),
3087 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3088 g_free (tmp);
3089 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3091 if (exp != NULL && *exp != '\0')
3093 if (edit_save_block (edit, exp, start_mark, end_mark))
3094 ret = TRUE;
3095 else
3096 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3098 edit->force |= REDRAW_COMPLETELY;
3101 g_free (exp);
3103 return ret;
3107 /* --------------------------------------------------------------------------------------------- */
3109 /** returns TRUE on success */
3110 gboolean
3111 edit_insert_file_cmd (WEdit * edit)
3113 char *tmp;
3114 char *exp;
3115 gboolean ret = FALSE;
3117 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3118 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3119 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3120 g_free (tmp);
3122 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3124 if (exp != NULL && *exp != '\0')
3126 vfs_path_t *exp_vpath;
3128 exp_vpath = vfs_path_from_str (exp);
3129 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3130 vfs_path_free (exp_vpath);
3132 if (!ret)
3133 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3136 g_free (exp);
3138 edit->force |= REDRAW_COMPLETELY;
3139 return ret;
3142 /* --------------------------------------------------------------------------------------------- */
3143 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3146 edit_sort_cmd (WEdit * edit)
3148 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3149 off_t start_mark, end_mark;
3150 int e;
3152 if (!eval_marks (edit, &start_mark, &end_mark))
3154 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3155 return 0;
3158 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3159 edit_save_block (edit, tmp, start_mark, end_mark);
3160 g_free (tmp);
3162 exp = input_dialog (_("Run sort"),
3163 _("Enter sort options (see manpage) separated by whitespace:"),
3164 MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3166 if (exp == NULL)
3167 return 1;
3169 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3170 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3171 tmp =
3172 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3173 " > ", tmp_edit_temp_name, (char *) NULL);
3174 g_free (tmp_edit_temp_name);
3175 g_free (tmp_edit_block_name);
3176 g_free (exp);
3178 e = system (tmp);
3179 g_free (tmp);
3180 if (e != 0)
3182 if (e == -1 || e == 127)
3183 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3184 else
3186 char q[8];
3188 sprintf (q, "%d ", e);
3189 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3190 edit_error_dialog (_("Sort"), tmp);
3191 g_free (tmp);
3193 return -1;
3196 edit->force |= REDRAW_COMPLETELY;
3198 if (edit_block_delete_cmd (edit))
3199 return 1;
3202 vfs_path_t *tmp_vpath;
3204 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3205 edit_insert_file (edit, tmp_vpath);
3206 vfs_path_free (tmp_vpath);
3208 return 0;
3211 /* --------------------------------------------------------------------------------------------- */
3213 * Ask user for a command, execute it and paste its output back to the
3214 * editor.
3218 edit_ext_cmd (WEdit * edit)
3220 char *exp, *tmp, *tmp_edit_temp_file;
3221 int e;
3223 exp =
3224 input_dialog (_("Paste output of external command"),
3225 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3226 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3227 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3228 INPUT_COMPLETE_SHELL_ESC);
3230 if (!exp)
3231 return 1;
3233 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3234 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3235 g_free (tmp_edit_temp_file);
3236 e = system (tmp);
3237 g_free (tmp);
3238 g_free (exp);
3240 if (e)
3242 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3243 return -1;
3246 edit->force |= REDRAW_COMPLETELY;
3249 vfs_path_t *tmp_vpath;
3251 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3252 edit_insert_file (edit, tmp_vpath);
3253 vfs_path_free (tmp_vpath);
3255 return 0;
3258 /* --------------------------------------------------------------------------------------------- */
3259 /** if block is 1, a block must be highlighted and the shell command
3260 processes it. If block is 0 the shell command is a straight system
3261 command, that just produces some output which is to be inserted */
3263 void
3264 edit_block_process_cmd (WEdit * edit, int macro_number)
3266 char *fname;
3267 char *macros_fname = NULL;
3269 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3270 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3271 user_menu (edit, macros_fname, 0);
3272 g_free (fname);
3273 g_free (macros_fname);
3274 edit->force |= REDRAW_COMPLETELY;
3277 /* --------------------------------------------------------------------------------------------- */
3279 void
3280 edit_mail_dialog (WEdit * edit)
3282 char *mail_to, *mail_subject, *mail_cc;
3284 quick_widget_t quick_widgets[] = {
3285 /* *INDENT-OFF* */
3286 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3287 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3288 INPUT_LAST_TEXT, "mail-dlg-input-3",
3289 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3290 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3291 INPUT_LAST_TEXT, "mail-dlg-input-2",
3292 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3293 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3294 INPUT_LAST_TEXT, "mail-dlg-input",
3295 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3296 QUICK_BUTTONS_OK_CANCEL,
3297 QUICK_END
3298 /* *INDENT-ON* */
3301 quick_dialog_t qdlg = {
3302 -1, -1, 50,
3303 N_("Mail"), "[Input Line Keys]",
3304 quick_widgets, NULL, NULL
3307 if (quick_dialog (&qdlg) != B_CANCEL)
3309 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3310 g_free (mail_to);
3311 g_free (mail_subject);
3312 g_free (mail_cc);
3316 /* --------------------------------------------------------------------------------------------- */
3318 /*******************/
3319 /* Word Completion */
3320 /*******************/
3323 * Complete current word using regular expression search
3324 * backwards beginning at the current cursor position.
3327 void
3328 edit_complete_word_cmd (WEdit * edit)
3330 gsize i, max_len, word_len = 0, num_compl = 0;
3331 off_t word_start = 0;
3332 GString *match_expr;
3333 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3335 /* search start of word to be completed */
3336 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3337 return;
3339 /* prepare match expression */
3340 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3341 match_expr = g_string_new ("(^|\\s+|\\b)");
3342 for (i = 0; i < word_len; i++)
3343 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3344 g_string_append (match_expr,
3345 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3347 /* collect the possible completions */
3348 /* start search from begin to end of file */
3349 max_len =
3350 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3351 &num_compl);
3353 if (num_compl > 0)
3355 /* insert completed word if there is only one match */
3356 if (num_compl == 1)
3357 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3358 /* more than one possible completion => ask the user */
3359 else
3361 char *curr_compl;
3363 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3364 /* !!! pressed again the selection dialog pops up, but that !!! */
3365 /* !!! seems to require a further internal state !!! */
3366 /*tty_beep (); */
3368 /* let the user select the preferred completion */
3369 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3370 (GString **) & compl, num_compl);
3372 if (curr_compl != NULL)
3374 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3375 g_free (curr_compl);
3380 g_string_free (match_expr, TRUE);
3381 /* release memory before return */
3382 for (i = 0; i < num_compl; i++)
3383 g_string_free (compl[i], TRUE);
3386 /* --------------------------------------------------------------------------------------------- */
3388 #ifdef HAVE_CHARSET
3389 void
3390 edit_select_codepage_cmd (WEdit * edit)
3392 if (do_select_codepage ())
3393 edit_set_codeset (edit);
3395 edit->force = REDRAW_PAGE;
3396 widget_redraw (WIDGET (edit));
3398 #endif
3400 /* --------------------------------------------------------------------------------------------- */
3402 void
3403 edit_insert_literal_cmd (WEdit * edit)
3405 int char_for_insertion;
3407 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3408 _("Press any key:"), FALSE);
3409 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3412 /* --------------------------------------------------------------------------------------------- */
3414 void
3415 edit_begin_end_macro_cmd (WEdit * edit)
3417 /* edit is a pointer to the widget */
3418 if (edit != NULL)
3420 long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3421 edit_execute_key_command (edit, command, -1);
3425 /* --------------------------------------------------------------------------------------------- */
3427 void
3428 edit_begin_end_repeat_cmd (WEdit * edit)
3430 /* edit is a pointer to the widget */
3431 if (edit != NULL)
3433 long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3434 edit_execute_key_command (edit, command, -1);
3438 /* --------------------------------------------------------------------------------------------- */
3440 gboolean
3441 edit_load_forward_cmd (WEdit * edit)
3443 if (edit->modified
3444 && edit_query_dialog2 (_("Warning"),
3445 _("Current text was modified without a file save.\n"
3446 "Continue discards these changes"), _("C&ontinue"),
3447 _("&Cancel")) == 1)
3449 edit->force |= REDRAW_COMPLETELY;
3450 return TRUE;
3453 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3454 return FALSE;
3456 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3457 return FALSE;
3459 edit_stack_iterator++;
3460 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3461 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3462 edit_history_moveto[edit_stack_iterator].line);
3464 return FALSE;
3467 /* --------------------------------------------------------------------------------------------- */
3469 gboolean
3470 edit_load_back_cmd (WEdit * edit)
3472 if (edit->modified
3473 && edit_query_dialog2 (_("Warning"),
3474 _("Current text was modified without a file save.\n"
3475 "Continue discards these changes"), _("C&ontinue"),
3476 _("&Cancel")) == 1)
3478 edit->force |= REDRAW_COMPLETELY;
3479 return TRUE;
3482 /* we are in the bottom of the stack, NO WAY! */
3483 if (edit_stack_iterator == 0)
3484 return FALSE;
3486 edit_stack_iterator--;
3487 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3488 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3489 edit_history_moveto[edit_stack_iterator].line);
3491 return FALSE;
3494 /* --------------------------------------------------------------------------------------------- */
3496 void
3497 edit_get_match_keyword_cmd (WEdit * edit)
3499 gsize word_len = 0, max_len = 0;
3500 int num_def = 0;
3501 gsize i;
3502 off_t word_start = 0;
3503 GString *match_expr;
3504 char *path = NULL;
3505 char *ptr = NULL;
3506 char *tagfile = NULL;
3508 etags_hash_t def_hash[MAX_DEFINITIONS];
3510 for (i = 0; i < MAX_DEFINITIONS; i++)
3512 def_hash[i].filename = NULL;
3515 /* search start of word to be completed */
3516 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3517 return;
3519 /* prepare match expression */
3520 match_expr = g_string_sized_new (word_len);
3521 for (i = 0; i < word_len; i++)
3522 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3524 ptr = g_get_current_dir ();
3525 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3526 g_free (ptr);
3528 /* Recursive search file 'TAGS' in parent dirs */
3531 ptr = g_path_get_dirname (path);
3532 g_free (path);
3533 path = ptr;
3534 g_free (tagfile);
3535 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3536 if (exist_file (tagfile))
3537 break;
3539 while (strcmp (path, PATH_SEP_STR) != 0);
3541 if (tagfile)
3543 num_def =
3544 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3545 g_free (tagfile);
3547 g_free (path);
3549 max_len = MAX_WIDTH_DEF_DIALOG;
3550 word_len = 0;
3551 if (num_def > 0)
3553 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3554 (etags_hash_t *) & def_hash, num_def);
3556 g_string_free (match_expr, TRUE);
3559 /* --------------------------------------------------------------------------------------------- */
3561 #ifdef HAVE_ASPELL
3563 edit_suggest_current_word (WEdit * edit)
3565 gsize cut_len = 0;
3566 gsize word_len = 0;
3567 off_t word_start = 0;
3568 int retval = B_SKIP_WORD;
3569 GString *match_word;
3571 /* search start of word to spell check */
3572 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3573 &cut_len);
3574 word_len = match_word->len;
3576 #ifdef HAVE_CHARSET
3577 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3579 GString *tmp_word;
3581 tmp_word = str_convert_to_display (match_word->str);
3582 g_string_free (match_word, TRUE);
3583 match_word = tmp_word;
3585 #endif
3586 if (!aspell_check (match_word->str, (int) word_len))
3588 GArray *suggest;
3589 unsigned int res;
3590 guint i;
3592 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3594 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3595 if (res != 0)
3597 char *new_word = NULL;
3599 edit->found_start = word_start;
3600 edit->found_len = word_len;
3601 edit->force |= REDRAW_PAGE;
3602 edit_scroll_screen_over_cursor (edit);
3603 edit_render_keypress (edit);
3605 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3606 edit_cursor_move (edit, word_len - cut_len);
3608 if (retval == B_ENTER && new_word != NULL)
3610 char *cp_word;
3612 #ifdef HAVE_CHARSET
3613 if (mc_global.source_codepage >= 0 &&
3614 (mc_global.source_codepage != mc_global.display_codepage))
3616 GString *tmp_word;
3618 tmp_word = str_convert_to_input (new_word);
3619 g_free (new_word);
3620 new_word = g_string_free (tmp_word, FALSE);
3622 #endif
3623 cp_word = new_word;
3624 for (i = 0; i < word_len; i++)
3625 edit_backspace (edit, TRUE);
3626 for (; *new_word; new_word++)
3627 edit_insert (edit, *new_word);
3628 g_free (cp_word);
3630 else if (retval == B_ADD_WORD && match_word != NULL)
3631 aspell_add_to_dict (match_word->str, (int) word_len);
3635 for (i = 0; i < suggest->len; i++)
3637 char *cur_sugg_word;
3639 cur_sugg_word = g_array_index (suggest, char *, i);
3640 g_free (cur_sugg_word);
3642 g_array_free (suggest, TRUE);
3643 edit->found_start = 0;
3644 edit->found_len = 0;
3646 g_string_free (match_word, TRUE);
3647 return retval;
3650 /* --------------------------------------------------------------------------------------------- */
3652 void
3653 edit_spellcheck_file (WEdit * edit)
3655 if (edit->buffer.curs_line > 0)
3657 edit_cursor_move (edit, -edit->buffer.curs1);
3658 edit_move_to_prev_col (edit, 0);
3659 edit_update_curs_row (edit);
3664 int c1, c2;
3666 c2 = edit_buffer_get_current_byte (&edit->buffer);
3670 if (edit->buffer.curs1 >= edit->buffer.size)
3671 return;
3673 c1 = c2;
3674 edit_cursor_move (edit, 1);
3675 c2 = edit_buffer_get_current_byte (&edit->buffer);
3677 while (is_break_char (c1) || is_break_char (c2));
3679 while (edit_suggest_current_word (edit) != B_CANCEL);
3682 /* --------------------------------------------------------------------------------------------- */
3684 void
3685 edit_set_spell_lang (void)
3687 GArray *lang_list;
3689 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3690 if (aspell_get_lang_list (lang_list) != 0)
3692 char *lang;
3694 lang = spell_dialog_lang_list_show (lang_list);
3695 if (lang != NULL)
3697 (void) aspell_set_lang (lang);
3698 g_free (lang);
3701 aspell_array_clean (lang_list);
3703 #endif /* HAVE_ASPELL */
3705 /* --------------------------------------------------------------------------------------------- */