(edit_replace_cmd): change type of 2nd argument from int to gboolean.
[midnight-commander.git] / src / editor / editcmd.c
blob2ed4b7350e3951234cedd43f78ea6781184fd2c0
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 edit->search->error = MC_SEARCH_E_NOTFOUND;
833 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
834 return FALSE;
837 /* fix the start and the end of search block positions */
838 if ((edit->search_line_type & AT_START_LINE) != 0
839 && (start_mark != 0
840 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
842 start_mark =
843 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
844 end_string_symbol);
846 if ((edit->search_line_type & AT_END_LINE) != 0
847 && (end_mark - 1 != edit->buffer.size
848 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
849 end_mark =
850 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
851 if (start_mark >= end_mark)
853 edit->search->error = MC_SEARCH_E_NOTFOUND;
854 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
855 return FALSE;
858 else
860 if (edit_search_options.backwards)
861 end_mark = max (1, edit->buffer.curs1) - 1;
864 /* search */
865 if (edit_search_options.backwards)
867 /* backward search */
868 search_end = end_mark;
870 if ((edit->search_line_type & AT_START_LINE) != 0)
871 search_start =
872 edit_calculate_start_of_current_line (&edit->buffer, search_start,
873 end_string_symbol);
875 while (search_start >= start_mark)
877 if (search_end > (off_t) (search_start + edit->search->original_len)
878 && mc_search_is_fixed_search_str (edit->search))
880 search_end = search_start + edit->search->original_len;
882 if (mc_search_run (edit->search, (void *) esm, search_start, search_end, len)
883 && edit->search->normal_offset == search_start)
885 return TRUE;
889 if ((edit->search_line_type & AT_START_LINE) != 0)
890 search_start =
891 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
892 end_string_symbol);
893 else
894 search_start--;
896 edit->search->error_str = g_strdup (_(STR_E_NOTFOUND));
898 else
900 /* forward search */
901 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
902 search_start =
903 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
904 end_string_symbol);
905 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
907 return FALSE;
910 /* --------------------------------------------------------------------------------------------- */
912 static char *
913 edit_replace_cmd__conv_to_display (const char *str)
915 #ifdef HAVE_CHARSET
916 GString *tmp;
918 tmp = str_convert_to_display (str);
919 if (tmp != NULL)
921 if (tmp->len != 0)
922 return g_string_free (tmp, FALSE);
923 g_string_free (tmp, TRUE);
925 #endif
926 return g_strdup (str);
929 /* --------------------------------------------------------------------------------------------- */
931 static char *
932 edit_replace_cmd__conv_to_input (char *str)
934 #ifdef HAVE_CHARSET
935 GString *tmp;
937 tmp = str_convert_to_input (str);
938 if (tmp != NULL)
940 if (tmp->len != 0)
941 return g_string_free (tmp, FALSE);
942 g_string_free (tmp, TRUE);
944 #endif
945 return g_strdup (str);
948 /* --------------------------------------------------------------------------------------------- */
950 static void
951 edit_do_search (WEdit * edit)
953 edit_search_status_msg_t esm;
954 gsize len = 0;
956 if (edit->search == NULL)
957 edit->search_start = edit->buffer.curs1;
959 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
961 esm.first = TRUE;
962 esm.edit = edit;
963 esm.offset = edit->search_start;
965 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
966 edit_search_status_update_cb, NULL);
968 if (search_create_bookmark)
970 int found = 0, books = 0;
971 long l = 0, l_last = -1;
972 long q = 0;
974 search_create_bookmark = FALSE;
975 book_mark_flush (edit, -1);
977 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
979 if (found == 0)
980 edit->search_start = edit->search->normal_offset;
981 found++;
982 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
983 if (l != l_last)
985 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
986 books++;
988 l_last = l;
989 q = edit->search->normal_offset + 1;
992 if (found == 0)
993 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
994 else
995 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
997 else
999 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
1000 && edit_search_options.backwards)
1001 edit->search_start--;
1003 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1004 && !edit_search_options.backwards)
1005 edit->search_start++;
1007 if (editcmd_find (&esm, &len))
1009 edit->found_start = edit->search_start = edit->search->normal_offset;
1010 edit->found_len = len;
1011 edit->over_col = 0;
1012 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1013 edit_scroll_screen_over_cursor (edit);
1014 if (edit_search_options.backwards)
1015 edit->search_start--;
1016 else
1017 edit->search_start++;
1019 else
1021 edit->search_start = edit->buffer.curs1;
1022 if (edit->search->error_str != NULL)
1023 edit_query_dialog (_("Search"), edit->search->error_str);
1027 status_msg_deinit (STATUS_MSG (&esm));
1029 edit->force |= REDRAW_COMPLETELY;
1030 edit_scroll_screen_over_cursor (edit);
1033 /* --------------------------------------------------------------------------------------------- */
1035 static void
1036 edit_search (WEdit * edit)
1038 if (editcmd_dialog_search_show (edit))
1040 edit->search_line_type = edit_get_search_line_type (edit->search);
1041 edit_search_fix_search_start_if_selection (edit);
1042 edit_do_search (edit);
1046 /* --------------------------------------------------------------------------------------------- */
1047 /** Return a null terminated length of text. Result must be g_free'd */
1049 static unsigned char *
1050 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1052 unsigned char *s, *r;
1054 r = s = g_malloc0 (finish - start + 1);
1055 if (edit->column_highlight)
1057 *l = 0;
1058 /* copy from buffer, excluding chars that are out of the column 'margins' */
1059 while (start < finish)
1061 int c;
1062 off_t x;
1064 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1065 c = edit_buffer_get_byte (&edit->buffer, start);
1066 if ((x >= edit->column1 && x < edit->column2)
1067 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1069 *s++ = c;
1070 (*l)++;
1072 start++;
1075 else
1077 *l = finish - start;
1078 while (start < finish)
1079 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1081 *s = '\0';
1082 return r;
1085 /* --------------------------------------------------------------------------------------------- */
1086 /** copies a block to clipboard file */
1088 static gboolean
1089 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1091 gboolean ret;
1092 gchar *tmp;
1094 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1095 ret = edit_save_block (edit, tmp, start, finish);
1096 g_free (tmp);
1097 return ret;
1100 /* --------------------------------------------------------------------------------------------- */
1102 static void
1103 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1105 FILE *p = 0;
1106 char *s;
1108 to = name_quote (to, FALSE);
1109 subject = name_quote (subject, FALSE);
1110 cc = name_quote (cc, FALSE);
1111 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1112 g_free (to);
1113 g_free (subject);
1114 g_free (cc);
1116 if (s != NULL)
1118 p = popen (s, "w");
1119 g_free (s);
1122 if (p != NULL)
1124 off_t i;
1126 for (i = 0; i < buf->size; i++)
1127 fputc (edit_buffer_get_byte (buf, i), p);
1128 pclose (p);
1132 /* --------------------------------------------------------------------------------------------- */
1133 /** find first character of current word */
1135 static gboolean
1136 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1138 int c;
1139 off_t i;
1141 /* return if at begin of file */
1142 if (buf->curs1 <= 0)
1143 return FALSE;
1145 c = edit_buffer_get_previous_byte (buf);
1146 /* return if not at end or in word */
1147 if (is_break_char (c))
1148 return FALSE;
1150 /* search start of word to be completed */
1151 for (i = 1;; i++)
1153 int last;
1155 last = c;
1156 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1158 if (is_break_char (c))
1160 /* return if word starts with digit */
1161 if (isdigit (last))
1162 return FALSE;
1164 break;
1168 /* success */
1169 *word_start = buf->curs1 - i; /* start found */
1170 *word_len = (gsize) i;
1171 return TRUE;
1174 /* --------------------------------------------------------------------------------------------- */
1176 * Get current word under cursor
1178 * @param esm status message window
1179 * @param srch mc_search object
1180 * @param word_start start word position
1182 * @return newly allocated string or NULL if no any words under cursor
1185 static char *
1186 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1187 off_t word_start)
1189 WEdit *edit = esm->edit;
1190 gsize len = 0;
1191 off_t i;
1192 GString *temp;
1194 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1195 return NULL;
1197 temp = g_string_sized_new (len);
1199 for (i = 0; i < (off_t) len; i++)
1201 int chr;
1203 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1204 if (!isspace (chr))
1205 g_string_append_c (temp, chr);
1208 return g_string_free (temp, temp->len == 0);
1211 /* --------------------------------------------------------------------------------------------- */
1212 /** collect the possible completions */
1214 static gsize
1215 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1216 char *match_expr, GString ** compl, gsize * num)
1218 gsize len = 0;
1219 gsize max_len = 0;
1220 gsize i;
1221 int skip;
1222 GString *temp;
1223 mc_search_t *srch;
1224 off_t last_byte, start = -1;
1225 char *current_word;
1226 gboolean entire_file;
1227 edit_search_status_msg_t esm;
1229 #ifdef HAVE_CHARSET
1230 srch = mc_search_new (match_expr, cp_source);
1231 #else
1232 srch = mc_search_new (match_expr, NULL);
1233 #endif
1234 if (srch == NULL)
1235 return 0;
1237 entire_file =
1238 mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
1239 "editor_wordcompletion_collect_entire_file", 0);
1241 last_byte = entire_file ? edit->buffer.size : word_start;
1243 srch->search_type = MC_SEARCH_T_REGEX;
1244 srch->is_case_sensitive = TRUE;
1245 srch->search_fn = edit_search_cmd_callback;
1246 srch->update_fn = edit_search_update_callback;
1248 esm.first = TRUE;
1249 esm.edit = edit;
1250 esm.offset = entire_file ? 0 : word_start;
1252 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1253 edit_search_status_update_cb, NULL);
1255 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1257 temp = g_string_new ("");
1259 /* collect max MAX_WORD_COMPLETIONS completions */
1260 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1262 g_string_set_size (temp, 0);
1263 start = srch->normal_offset;
1265 /* add matched completion if not yet added */
1266 for (i = 0; i < len; i++)
1268 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1269 if (isspace (skip))
1270 continue;
1272 /* skip current word */
1273 if (start + (off_t) i == word_start)
1274 break;
1276 g_string_append_c (temp, skip);
1279 if (temp->len == 0)
1280 continue;
1282 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1283 continue;
1285 skip = 0;
1287 for (i = 0; i < *num; i++)
1289 if (strncmp
1290 ((char *) &compl[i]->str[word_len],
1291 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1293 GString *this = compl[i];
1294 for (++i; i < *num; i++)
1295 compl[i - 1] = compl[i];
1296 compl[*num - 1] = this;
1297 skip = 1;
1298 break; /* skip it, already added */
1301 if (skip != 0)
1302 continue;
1304 if (*num == MAX_WORD_COMPLETIONS)
1306 g_string_free (compl[0], TRUE);
1307 for (i = 1; i < *num; i++)
1308 compl[i - 1] = compl[i];
1309 (*num)--;
1311 #ifdef HAVE_CHARSET
1313 GString *recoded;
1314 recoded = str_convert_to_display (temp->str);
1316 if (recoded && recoded->len)
1317 g_string_assign (temp, recoded->str);
1319 g_string_free (recoded, TRUE);
1321 #endif
1322 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1323 start += len;
1325 /* note the maximal length needed for the completion dialog */
1326 if (len > max_len)
1327 max_len = len;
1330 status_msg_deinit (STATUS_MSG (&esm));
1331 mc_search_free (srch);
1332 g_string_free (temp, TRUE);
1333 g_free (current_word);
1335 return max_len;
1338 /* --------------------------------------------------------------------------------------------- */
1340 static void
1341 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1342 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1344 off_t i, cursor;
1345 long col;
1347 cursor = edit->buffer.curs1;
1348 col = edit_get_col (edit);
1350 for (i = 0; i < size; i++)
1352 if (data[i] != '\n')
1353 edit_insert (edit, data[i]);
1354 else
1355 { /* fill in and move to next line */
1356 long l;
1357 off_t p;
1359 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1361 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1362 edit_insert (edit, ' ');
1364 for (p = edit->buffer.curs1;; p++)
1366 if (p == edit->buffer.size)
1368 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1369 edit_insert_ahead (edit, '\n');
1370 p++;
1371 break;
1373 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1375 p++;
1376 break;
1379 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1381 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1382 edit_insert (edit, ' ');
1386 *col1 = col;
1387 *col2 = col + width;
1388 *start_pos = cursor;
1389 *end_pos = edit->buffer.curs1;
1390 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1393 /* --------------------------------------------------------------------------------------------- */
1395 static int
1396 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1398 const macros_t *m1 = (const macros_t *) macro1;
1399 const macros_t *m2 = (const macros_t *) macro2;
1401 return m1->hotkey - m2->hotkey;
1404 /* --------------------------------------------------------------------------------------------- */
1406 static void
1407 edit_macro_sort_by_hotkey (void)
1409 if (macros_list != NULL && macros_list->len != 0)
1410 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1413 /* --------------------------------------------------------------------------------------------- */
1415 static gboolean
1416 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1418 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1419 macros_t *result;
1420 macros_t search_macro;
1422 (void) edit;
1424 search_macro.hotkey = hotkey;
1425 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1426 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1428 if (result != NULL && result->macro != NULL)
1430 *indx = (result - array_start);
1431 *macros = result;
1432 return TRUE;
1434 *indx = 0;
1435 return FALSE;
1438 /* --------------------------------------------------------------------------------------------- */
1439 /** returns FALSE on error */
1441 static gboolean
1442 edit_delete_macro (WEdit * edit, int hotkey)
1444 mc_config_t *macros_config = NULL;
1445 const char *section_name = "editor";
1446 gchar *macros_fname;
1447 guint indx;
1448 char *skeyname;
1449 const macros_t *macros = NULL;
1451 /* clear array of actions for current hotkey */
1452 while (edit_get_macro (edit, hotkey, &macros, &indx))
1454 if (macros->macro != NULL)
1455 g_array_free (macros->macro, TRUE);
1456 macros = NULL;
1457 g_array_remove_index (macros_list, indx);
1458 edit_macro_sort_by_hotkey ();
1461 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1462 macros_config = mc_config_init (macros_fname, FALSE);
1463 g_free (macros_fname);
1465 if (macros_config == NULL)
1466 return FALSE;
1468 skeyname = lookup_key_by_code (hotkey);
1469 while (mc_config_del_key (macros_config, section_name, skeyname))
1471 g_free (skeyname);
1472 mc_config_save_file (macros_config, NULL);
1473 mc_config_deinit (macros_config);
1474 return TRUE;
1477 /* --------------------------------------------------------------------------------------------- */
1479 * Callback for the iteration of objects in the 'editors' array.
1480 * Toggle syntax highlighting in editor object.
1482 * @param data probably WEdit object
1483 * @param user_data unused
1486 static void
1487 edit_syntax_onoff_cb (void *data, void *user_data)
1489 (void) user_data;
1491 if (edit_widget_is_editor (CONST_WIDGET (data)))
1493 WEdit *edit = (WEdit *) data;
1495 if (option_syntax_highlighting)
1496 edit_load_syntax (edit, NULL, edit->syntax_type);
1497 edit->force |= REDRAW_PAGE;
1501 /* --------------------------------------------------------------------------------------------- */
1503 * Callback for the iteration of objects in the 'editors' array.
1504 * Redraw editor object.
1506 * @param data probably WEdit object
1507 * @param user_data unused
1510 static void
1511 edit_redraw_page_cb (void *data, void *user_data)
1513 (void) user_data;
1515 if (edit_widget_is_editor (CONST_WIDGET (data)))
1516 ((WEdit *) data)->force |= REDRAW_PAGE;
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Insert autocompleted word into editor.
1523 * @param edit editor object
1524 * @param completion word for completion
1525 * @param word_len offset from beginning for insert
1528 static void
1529 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1531 #ifdef HAVE_CHARSET
1532 GString *temp;
1534 temp = str_convert_to_input (completion);
1536 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1537 edit_insert (edit, *completion);
1538 g_string_free (temp, TRUE);
1539 #else
1540 for (completion += word_len; *completion != '\0'; completion++)
1541 edit_insert (edit, *completion);
1542 #endif
1545 /* --------------------------------------------------------------------------------------------- */
1546 /*** public functions ****************************************************************************/
1547 /* --------------------------------------------------------------------------------------------- */
1549 void
1550 edit_refresh_cmd (void)
1552 clr_scr ();
1553 repaint_screen ();
1554 tty_keypad (TRUE);
1557 /* --------------------------------------------------------------------------------------------- */
1559 * Toggle syntax highlighting in all editor windows.
1561 * @param h root widget for all windows
1564 void
1565 edit_syntax_onoff_cmd (WDialog * h)
1567 option_syntax_highlighting = !option_syntax_highlighting;
1568 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1569 dlg_redraw (h);
1572 /* --------------------------------------------------------------------------------------------- */
1574 * Toggle tabs showing in all editor windows.
1576 * @param h root widget for all windows
1579 void
1580 edit_show_tabs_tws_cmd (WDialog * h)
1582 enable_show_tabs_tws = !enable_show_tabs_tws;
1583 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1584 dlg_redraw (h);
1587 /* --------------------------------------------------------------------------------------------- */
1589 * Toggle right margin showing in all editor windows.
1591 * @param h root widget for all windows
1594 void
1595 edit_show_margin_cmd (WDialog * h)
1597 show_right_margin = !show_right_margin;
1598 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1599 dlg_redraw (h);
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Toggle line numbers showing in all editor windows.
1606 * @param h root widget for all windows
1609 void
1610 edit_show_numbers_cmd (WDialog * h)
1612 option_line_state = !option_line_state;
1613 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1614 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1615 dlg_redraw (h);
1618 /* --------------------------------------------------------------------------------------------- */
1620 void
1621 edit_save_mode_cmd (void)
1623 char *str_result = NULL;
1625 const char *str[] = {
1626 N_("&Quick save"),
1627 N_("&Safe save"),
1628 N_("&Do backups with following extension:")
1631 #ifdef HAVE_ASSERT_H
1632 assert (option_backup_ext != NULL);
1633 #endif
1635 #ifdef ENABLE_NLS
1636 size_t i;
1638 for (i = 0; i < 3; i++)
1639 str[i] = _(str[i]);
1640 #endif
1643 quick_widget_t quick_widgets[] = {
1644 /* *INDENT-OFF* */
1645 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1646 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1647 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1648 QUICK_SEPARATOR (TRUE),
1649 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1650 QUICK_BUTTONS_OK_CANCEL,
1651 QUICK_END
1652 /* *INDENT-ON* */
1655 quick_dialog_t qdlg = {
1656 -1, -1, 38,
1657 N_("Edit Save Mode"), "[Edit Save Mode]",
1658 quick_widgets, edit_save_mode_callback, NULL
1661 if (quick_dialog (&qdlg) != B_CANCEL)
1663 g_free (option_backup_ext);
1664 option_backup_ext = str_result;
1669 /* --------------------------------------------------------------------------------------------- */
1671 void
1672 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1674 vfs_path_free (edit->filename_vpath);
1675 edit->filename_vpath = vfs_path_clone (name_vpath);
1677 if (edit->dir_vpath == NULL)
1678 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1681 /* --------------------------------------------------------------------------------------------- */
1682 /* Here we want to warn the users of overwriting an existing file,
1683 but only if they have made a change to the filename */
1684 /* returns TRUE on success */
1685 gboolean
1686 edit_save_as_cmd (WEdit * edit)
1688 /* This heads the 'Save As' dialog box */
1689 vfs_path_t *exp_vpath;
1690 int save_lock = 0;
1691 int different_filename = 0;
1693 if (!edit_check_newline (&edit->buffer))
1694 return FALSE;
1696 exp_vpath = edit_get_save_file_as (edit);
1697 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1699 if (exp_vpath != NULL)
1701 if (vfs_path_len (exp_vpath) == 0)
1702 goto ret;
1703 else
1705 int rv;
1707 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1709 int file;
1710 struct stat sb;
1712 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1714 edit_error_dialog (_("Save as"),
1715 get_sys_error (_
1716 ("Cannot save: destination is not a regular file")));
1717 goto ret;
1720 different_filename = 1;
1721 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1723 if (file != -1)
1725 /* the file exists */
1726 mc_close (file);
1727 /* Overwrite the current file or cancel the operation */
1728 if (edit_query_dialog2
1729 (_("Warning"),
1730 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1731 goto ret;
1733 else
1735 edit->stat1.st_mode |= S_IWUSR;
1737 save_lock = lock_file (exp_vpath);
1739 else
1741 /* filenames equal, check if already locked */
1742 if (!edit->locked && !edit->delete_file)
1743 save_lock = lock_file (exp_vpath);
1746 if (different_filename)
1749 * Allow user to write into saved (under another name) file
1750 * even if original file had r/o user permissions.
1752 edit->stat1.st_mode |= S_IWRITE;
1755 rv = edit_save_file (edit, exp_vpath);
1756 switch (rv)
1758 case 1:
1759 /* Successful, so unlock both files */
1760 if (different_filename)
1762 if (save_lock)
1763 unlock_file (exp_vpath);
1764 if (edit->locked)
1765 edit->locked = unlock_file (edit->filename_vpath);
1767 else
1769 if (edit->locked || save_lock)
1770 edit->locked = unlock_file (edit->filename_vpath);
1773 edit_set_filename (edit, exp_vpath);
1774 if (edit->lb != LB_ASIS)
1775 edit_reload (edit, exp_vpath);
1776 edit->modified = 0;
1777 edit->delete_file = 0;
1778 if (different_filename)
1779 edit_load_syntax (edit, NULL, edit->syntax_type);
1780 vfs_path_free (exp_vpath);
1781 edit->force |= REDRAW_COMPLETELY;
1782 return TRUE;
1783 default:
1784 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1785 /* fallthrough */
1786 case -1:
1787 /* Failed, so maintain modify (not save) lock */
1788 if (save_lock)
1789 unlock_file (exp_vpath);
1790 break;
1795 ret:
1796 vfs_path_free (exp_vpath);
1797 edit->force |= REDRAW_COMPLETELY;
1798 return FALSE;
1801 /* {{{ Macro stuff starts here */
1802 /* --------------------------------------------------------------------------------------------- */
1804 void
1805 edit_delete_macro_cmd (WEdit * edit)
1807 int hotkey;
1809 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1811 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1812 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1815 /* --------------------------------------------------------------------------------------------- */
1817 /** returns FALSE on error */
1818 gboolean
1819 edit_execute_macro (WEdit * edit, int hotkey)
1821 gboolean res = FALSE;
1823 if (hotkey != 0)
1825 const macros_t *macros;
1826 guint indx;
1828 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1829 macros->macro != NULL && macros->macro->len != 0)
1831 guint i;
1833 edit->force |= REDRAW_PAGE;
1835 for (i = 0; i < macros->macro->len; i++)
1837 const macro_action_t *m_act;
1839 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1840 edit_execute_cmd (edit, m_act->action, m_act->ch);
1841 res = TRUE;
1846 return res;
1849 /* --------------------------------------------------------------------------------------------- */
1851 /** returns FALSE on error */
1852 gboolean
1853 edit_store_macro_cmd (WEdit * edit)
1855 int i;
1856 int hotkey;
1857 GString *marcros_string;
1858 mc_config_t *macros_config = NULL;
1859 const char *section_name = "editor";
1860 gchar *macros_fname;
1861 GArray *macros; /* current macro */
1862 int tmp_act;
1863 gboolean have_macro = FALSE;
1864 char *skeyname = NULL;
1866 hotkey =
1867 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1868 if (hotkey == ESC_CHAR)
1869 return FALSE;
1871 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1873 /* return FALSE if try assign macro into restricted hotkeys */
1874 if (tmp_act == CK_MacroStartRecord
1875 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1876 return FALSE;
1878 edit_delete_macro (edit, hotkey);
1880 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1881 macros_config = mc_config_init (macros_fname, FALSE);
1882 g_free (macros_fname);
1884 if (macros_config == NULL)
1885 return FALSE;
1887 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1889 marcros_string = g_string_sized_new (250);
1890 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1892 skeyname = lookup_key_by_code (hotkey);
1894 for (i = 0; i < macro_index; i++)
1896 macro_action_t m_act;
1897 const char *action_name;
1899 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1901 if (action_name == NULL)
1902 break;
1904 m_act.action = record_macro_buf[i].action;
1905 m_act.ch = record_macro_buf[i].ch;
1906 g_array_append_val (macros, m_act);
1907 have_macro = TRUE;
1908 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1909 (int) record_macro_buf[i].ch);
1911 if (have_macro)
1913 macros_t macro;
1914 macro.hotkey = hotkey;
1915 macro.macro = macros;
1916 g_array_append_val (macros_list, macro);
1917 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1919 else
1920 mc_config_del_key (macros_config, section_name, skeyname);
1922 g_free (skeyname);
1923 edit_macro_sort_by_hotkey ();
1925 g_string_free (marcros_string, TRUE);
1926 mc_config_save_file (macros_config, NULL);
1927 mc_config_deinit (macros_config);
1928 return TRUE;
1931 /* --------------------------------------------------------------------------------------------- */
1933 gboolean
1934 edit_repeat_macro_cmd (WEdit * edit)
1936 int i, j;
1937 char *f;
1938 long count_repeat;
1939 char *error = NULL;
1941 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1942 INPUT_COMPLETE_NONE);
1943 if (f == NULL || *f == '\0')
1945 g_free (f);
1946 return FALSE;
1949 count_repeat = strtol (f, &error, 0);
1951 if (*error != '\0')
1953 g_free (f);
1954 return FALSE;
1957 g_free (f);
1959 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1960 edit->force |= REDRAW_PAGE;
1962 for (j = 0; j < count_repeat; j++)
1963 for (i = 0; i < macro_index; i++)
1964 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1965 edit_update_screen (edit);
1966 return TRUE;
1969 /* --------------------------------------------------------------------------------------------- */
1970 /** return FALSE on error */
1972 gboolean
1973 edit_load_macro_cmd (WEdit * edit)
1975 mc_config_t *macros_config = NULL;
1976 gchar **profile_keys, **keys;
1977 gchar **values, **curr_values;
1978 const char *section_name = "editor";
1979 gchar *macros_fname;
1981 (void) edit;
1983 if (macros_list == NULL || macros_list->len != 0)
1984 return FALSE;
1986 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1987 macros_config = mc_config_init (macros_fname, TRUE);
1988 g_free (macros_fname);
1990 if (macros_config == NULL)
1991 return FALSE;
1993 keys = mc_config_get_keys (macros_config, section_name, NULL);
1995 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1997 int hotkey;
1998 gboolean have_macro = FALSE;
1999 GArray *macros;
2000 macros_t macro;
2002 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
2003 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
2004 hotkey = lookup_key (*profile_keys, NULL);
2006 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2008 char **macro_pair = NULL;
2010 macro_pair = g_strsplit (*curr_values, ":", 2);
2011 if (macro_pair != NULL)
2013 macro_action_t m_act;
2014 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2015 m_act.action = 0;
2016 else
2018 m_act.action = keybind_lookup_action (macro_pair[0]);
2019 MC_PTR_FREE (macro_pair[0]);
2021 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2022 m_act.ch = -1;
2023 else
2025 m_act.ch = strtol (macro_pair[1], NULL, 0);
2026 MC_PTR_FREE (macro_pair[1]);
2028 if (m_act.action != 0)
2030 /* a shell command */
2031 if ((m_act.action / CK_PipeBlock (0)) == 1)
2033 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2034 m_act.ch = -1;
2036 g_array_append_val (macros, m_act);
2037 have_macro = TRUE;
2039 g_strfreev (macro_pair);
2040 macro_pair = NULL;
2043 if (have_macro)
2045 macro.hotkey = hotkey;
2046 macro.macro = macros;
2047 g_array_append_val (macros_list, macro);
2049 g_strfreev (values);
2051 g_strfreev (keys);
2052 mc_config_deinit (macros_config);
2053 edit_macro_sort_by_hotkey ();
2054 return TRUE;
2057 /* }}} Macro stuff end here */
2059 /* --------------------------------------------------------------------------------------------- */
2060 /** returns TRUE on success */
2062 gboolean
2063 edit_save_confirm_cmd (WEdit * edit)
2065 if (edit->filename_vpath == NULL)
2066 return edit_save_as_cmd (edit);
2068 if (!edit_check_newline (&edit->buffer))
2069 return FALSE;
2071 if (edit_confirm_save)
2073 char *f;
2074 gboolean ok;
2076 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2077 vfs_path_as_str (edit->filename_vpath));
2078 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2079 g_free (f);
2080 if (!ok)
2081 return FALSE;
2083 return edit_save_cmd (edit);
2086 /* --------------------------------------------------------------------------------------------- */
2088 * Ask file to edit and load it.
2090 * @return TRUE on success or cancel of ask.
2093 gboolean
2094 edit_load_cmd (WDialog * h)
2096 char *exp;
2097 gboolean ret = TRUE; /* possible cancel */
2099 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2100 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2101 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2103 if (exp != NULL && *exp != '\0')
2105 vfs_path_t *exp_vpath;
2107 exp_vpath = vfs_path_from_str (exp);
2108 ret = edit_load_file_from_filename (h, exp_vpath);
2109 vfs_path_free (exp_vpath);
2112 g_free (exp);
2114 return ret;
2117 /* --------------------------------------------------------------------------------------------- */
2119 * Load syntax file to edit.
2121 * @return TRUE on success
2124 gboolean
2125 edit_load_syntax_file (WDialog * h)
2127 vfs_path_t *extdir_vpath;
2128 int dir = 0;
2129 gboolean ret = FALSE;
2131 if (geteuid () == 0)
2132 dir = query_dialog (_("Syntax file edit"),
2133 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2134 _("&User"), _("&System wide"));
2136 extdir_vpath =
2137 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2138 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2140 vfs_path_free (extdir_vpath);
2141 extdir_vpath =
2142 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2145 if (dir == 0)
2147 vfs_path_t *user_syntax_file_vpath;
2149 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2150 check_for_default (extdir_vpath, user_syntax_file_vpath);
2151 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2152 vfs_path_free (user_syntax_file_vpath);
2154 else if (dir == 1)
2155 ret = edit_load_file_from_filename (h, extdir_vpath);
2157 vfs_path_free (extdir_vpath);
2159 return ret;
2162 /* --------------------------------------------------------------------------------------------- */
2164 * Load menu file to edit.
2166 * @return TRUE on success
2169 gboolean
2170 edit_load_menu_file (WDialog * h)
2172 vfs_path_t *buffer_vpath;
2173 vfs_path_t *menufile_vpath;
2174 int dir;
2175 gboolean ret;
2177 query_set_sel (1);
2178 dir = query_dialog (_("Menu edit"),
2179 _("Which menu file do you want to edit?"), D_NORMAL,
2180 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2182 menufile_vpath =
2183 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2184 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2186 vfs_path_free (menufile_vpath);
2187 menufile_vpath =
2188 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2191 switch (dir)
2193 case 0:
2194 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2195 check_for_default (menufile_vpath, buffer_vpath);
2196 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2197 break;
2199 case 1:
2200 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2201 check_for_default (menufile_vpath, buffer_vpath);
2202 break;
2204 case 2:
2205 buffer_vpath =
2206 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2207 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2209 vfs_path_free (buffer_vpath);
2210 buffer_vpath =
2211 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2213 break;
2215 default:
2216 vfs_path_free (menufile_vpath);
2217 return FALSE;
2220 ret = edit_load_file_from_filename (h, buffer_vpath);
2222 vfs_path_free (buffer_vpath);
2223 vfs_path_free (menufile_vpath);
2225 return ret;
2228 /* --------------------------------------------------------------------------------------------- */
2230 * Close window with opened file.
2232 * @return TRUE if file was closed.
2235 gboolean
2236 edit_close_cmd (WEdit * edit)
2238 gboolean ret;
2240 ret = (edit != NULL) && edit_ok_to_exit (edit);
2242 if (ret)
2244 WDialog *h = WIDGET (edit)->owner;
2246 if (edit->locked != 0)
2247 unlock_file (edit->filename_vpath);
2249 del_widget (edit);
2251 if (edit_widget_is_editor (CONST_WIDGET (h->current->data)))
2252 edit = (WEdit *) h->current->data;
2253 else
2255 edit = find_editor (h);
2256 if (edit != NULL)
2257 dlg_set_top_widget (edit);
2261 if (edit != NULL)
2262 edit->force |= REDRAW_COMPLETELY;
2264 return ret;
2267 /* --------------------------------------------------------------------------------------------- */
2269 if mark2 is -1 then marking is from mark1 to the cursor.
2270 Otherwise its between the markers. This handles this.
2271 Returns FALSE if no text is marked.
2274 gboolean
2275 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2277 long end_mark_curs;
2279 if (edit->mark1 == edit->mark2)
2281 *start_mark = *end_mark = 0;
2282 edit->column2 = edit->column1 = 0;
2283 return FALSE;
2286 if (edit->end_mark_curs < 0)
2287 end_mark_curs = edit->buffer.curs1;
2288 else
2289 end_mark_curs = edit->end_mark_curs;
2291 if (edit->mark2 >= 0)
2293 *start_mark = min (edit->mark1, edit->mark2);
2294 *end_mark = max (edit->mark1, edit->mark2);
2296 else
2298 *start_mark = min (edit->mark1, end_mark_curs);
2299 *end_mark = max (edit->mark1, end_mark_curs);
2300 edit->column2 = edit->curs_col + edit->over_col;
2303 if (edit->column_highlight
2304 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2305 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2307 off_t start_bol, start_eol;
2308 off_t end_bol, end_eol;
2309 long col1, col2;
2310 off_t diff1, diff2;
2312 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2313 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2314 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2315 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2316 col1 = min (edit->column1, edit->column2);
2317 col2 = max (edit->column1, edit->column2);
2319 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2320 edit_move_forward3 (edit, start_bol, col1, 0);
2321 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2322 edit_move_forward3 (edit, end_bol, col1, 0);
2324 *start_mark -= diff1;
2325 *end_mark += diff2;
2326 *start_mark = max (*start_mark, start_eol);
2327 *end_mark = min (*end_mark, end_eol);
2329 return TRUE;
2332 /* --------------------------------------------------------------------------------------------- */
2334 void
2335 edit_block_copy_cmd (WEdit * edit)
2337 off_t start_mark, end_mark, current = edit->buffer.curs1;
2338 off_t mark1 = 0, mark2 = 0;
2339 long c1 = 0, c2 = 0;
2340 off_t size;
2341 unsigned char *copy_buf;
2343 edit_update_curs_col (edit);
2344 if (!eval_marks (edit, &start_mark, &end_mark))
2345 return;
2347 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2349 /* all that gets pushed are deletes hence little space is used on the stack */
2351 edit_push_markers (edit);
2353 if (edit->column_highlight)
2355 long col_delta;
2357 col_delta = labs (edit->column2 - edit->column1);
2358 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2360 else
2362 int size_orig = size;
2364 while (size-- != 0)
2365 edit_insert_ahead (edit, copy_buf[size]);
2367 /* Place cursor at the end of text selection */
2368 if (option_cursor_after_inserted_block)
2369 edit_cursor_move (edit, size_orig);
2372 g_free (copy_buf);
2373 edit_scroll_screen_over_cursor (edit);
2375 if (edit->column_highlight)
2376 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2377 else if (start_mark < current && end_mark > current)
2378 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2380 edit->force |= REDRAW_PAGE;
2384 /* --------------------------------------------------------------------------------------------- */
2386 void
2387 edit_block_move_cmd (WEdit * edit)
2389 off_t current;
2390 unsigned char *copy_buf = NULL;
2391 off_t start_mark, end_mark;
2393 if (!eval_marks (edit, &start_mark, &end_mark))
2394 return;
2396 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2397 return;
2399 if (edit->mark2 < 0)
2400 edit_mark_cmd (edit, FALSE);
2401 edit_push_markers (edit);
2403 if (edit->column_highlight)
2405 off_t mark1, mark2;
2406 off_t size;
2407 long c1, c2, b_width;
2408 long x, x2;
2410 c1 = min (edit->column1, edit->column2);
2411 c2 = max (edit->column1, edit->column2);
2412 b_width = c2 - c1;
2414 edit_update_curs_col (edit);
2416 x = edit->curs_col;
2417 x2 = x + edit->over_col;
2419 /* do nothing when cursor inside first line of selected area */
2420 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2421 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2422 return;
2424 if (edit->buffer.curs1 > start_mark
2425 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2427 if (x > c2)
2428 x -= b_width;
2429 else if (x > c1 && x <= c2)
2430 x = c1;
2432 /* save current selection into buffer */
2433 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2435 /* remove current selection */
2436 edit_block_delete_cmd (edit);
2438 edit->over_col = max (0, edit->over_col - b_width);
2439 /* calculate the cursor pos after delete block */
2440 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2441 edit_cursor_move (edit, current - edit->buffer.curs1);
2442 edit_scroll_screen_over_cursor (edit);
2444 /* add TWS if need before block insertion */
2445 if (option_cursor_beyond_eol && edit->over_col > 0)
2446 edit_insert_over (edit);
2448 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2449 edit_set_markers (edit, mark1, mark2, c1, c2);
2451 else
2453 off_t count, count_orig;
2455 current = edit->buffer.curs1;
2456 copy_buf = g_malloc0 (end_mark - start_mark);
2457 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2458 edit_scroll_screen_over_cursor (edit);
2460 for (count = start_mark; count < end_mark; count++)
2461 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2463 edit_scroll_screen_over_cursor (edit);
2464 edit_cursor_move (edit,
2465 current - edit->buffer.curs1 -
2466 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2467 edit_scroll_screen_over_cursor (edit);
2468 count_orig = count;
2469 while (count-- > start_mark)
2470 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2472 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2475 /* Place cursor at the end of text selection */
2476 if (option_cursor_after_inserted_block)
2477 edit_cursor_move (edit, count_orig - start_mark);
2480 edit_scroll_screen_over_cursor (edit);
2481 g_free (copy_buf);
2482 edit->force |= REDRAW_PAGE;
2485 /* --------------------------------------------------------------------------------------------- */
2486 /** returns 1 if canceelled by user */
2489 edit_block_delete_cmd (WEdit * edit)
2491 off_t start_mark, end_mark;
2493 if (eval_marks (edit, &start_mark, &end_mark))
2494 return edit_block_delete (edit);
2496 edit_delete_line (edit);
2497 return 0;
2500 /* --------------------------------------------------------------------------------------------- */
2501 /** call with edit = 0 before shutdown to close memory leaks */
2503 void
2504 edit_replace_cmd (WEdit * edit, gboolean again)
2506 /* 1 = search string, 2 = replace with */
2507 static char *saved1 = NULL; /* saved default[123] */
2508 static char *saved2 = NULL;
2509 char *input1 = NULL; /* user input from the dialog */
2510 char *input2 = NULL;
2511 GString *input2_str = NULL;
2512 char *disp1 = NULL;
2513 char *disp2 = NULL;
2514 long times_replaced = 0;
2515 gboolean once_found = FALSE;
2516 edit_search_status_msg_t esm;
2518 if (edit == NULL)
2520 MC_PTR_FREE (saved1);
2521 MC_PTR_FREE (saved2);
2522 return;
2525 edit->force |= REDRAW_COMPLETELY;
2527 if (again && !saved1 && !saved2)
2528 again = FALSE;
2530 if (again)
2532 input1 = g_strdup (saved1 ? saved1 : "");
2533 input2 = g_strdup (saved2 ? saved2 : "");
2535 else
2537 char *tmp_inp1, *tmp_inp2;
2539 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : "");
2540 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : "");
2542 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2544 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2546 g_free (disp1);
2547 g_free (disp2);
2549 if (input1 == NULL || *input1 == '\0')
2551 edit->force = REDRAW_COMPLETELY;
2552 goto cleanup;
2555 tmp_inp1 = input1;
2556 tmp_inp2 = input2;
2557 input1 = edit_replace_cmd__conv_to_input (input1);
2558 input2 = edit_replace_cmd__conv_to_input (input2);
2559 g_free (tmp_inp1);
2560 g_free (tmp_inp2);
2562 g_free (saved1), saved1 = g_strdup (input1);
2563 g_free (saved2), saved2 = g_strdup (input2);
2565 mc_search_free (edit->search);
2566 edit->search = NULL;
2569 input2_str = g_string_new (input2);
2571 if (edit->search == NULL)
2573 #ifdef HAVE_CHARSET
2574 edit->search = mc_search_new (input1, cp_source);
2575 #else
2576 edit->search = mc_search_new (input1, NULL);
2577 #endif
2578 if (edit->search == NULL)
2580 edit->search_start = edit->buffer.curs1;
2581 goto cleanup;
2583 edit->search->search_type = edit_search_options.type;
2584 #ifdef HAVE_CHARSET
2585 edit->search->is_all_charsets = edit_search_options.all_codepages;
2586 #endif
2587 edit->search->is_case_sensitive = edit_search_options.case_sens;
2588 edit->search->whole_words = edit_search_options.whole_words;
2589 edit->search->search_fn = edit_search_cmd_callback;
2590 edit->search->update_fn = edit_search_update_callback;
2591 edit->search_line_type = edit_get_search_line_type (edit->search);
2592 edit_search_fix_search_start_if_selection (edit);
2595 if (edit->found_len && edit->search_start == edit->found_start + 1
2596 && edit_search_options.backwards)
2597 edit->search_start--;
2599 if (edit->found_len && edit->search_start == edit->found_start - 1
2600 && !edit_search_options.backwards)
2601 edit->search_start++;
2603 esm.first = TRUE;
2604 esm.edit = edit;
2605 esm.offset = edit->search_start;
2607 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2608 edit_search_status_update_cb, NULL);
2612 gsize len = 0;
2614 if (!editcmd_find (&esm, &len))
2616 if (!(edit->search->error == MC_SEARCH_E_OK ||
2617 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2619 edit_query_dialog (_("Search"), edit->search->error_str);
2621 break;
2623 once_found = TRUE;
2625 edit->search_start = edit->search->normal_offset;
2626 /*returns negative on not found or error in pattern */
2628 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2630 gsize i;
2631 GString *repl_str;
2633 edit->found_start = edit->search_start;
2634 i = edit->found_len = len;
2636 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2637 edit_scroll_screen_over_cursor (edit);
2639 if (edit->replace_mode == 0)
2641 long l;
2642 int prompt;
2644 l = edit->curs_row - WIDGET (edit)->lines / 3;
2645 if (l > 0)
2646 edit_scroll_downward (edit, l);
2647 if (l < 0)
2648 edit_scroll_upward (edit, -l);
2650 edit_scroll_screen_over_cursor (edit);
2651 edit->force |= REDRAW_PAGE;
2652 edit_render_keypress (edit);
2654 /*so that undo stops at each query */
2655 edit_push_key_press (edit);
2656 /* and prompt 2/3 down */
2657 disp1 = edit_replace_cmd__conv_to_display (saved1);
2658 disp2 = edit_replace_cmd__conv_to_display (saved2);
2659 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2660 g_free (disp1);
2661 g_free (disp2);
2663 if (prompt == B_REPLACE_ALL)
2664 edit->replace_mode = 1;
2665 else if (prompt == B_SKIP_REPLACE)
2667 if (edit_search_options.backwards)
2668 edit->search_start--;
2669 else
2670 edit->search_start++;
2671 continue; /* loop */
2673 else if (prompt == B_CANCEL)
2675 edit->replace_mode = -1;
2676 break; /* loop */
2680 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2682 if (edit->search->error != MC_SEARCH_E_OK)
2684 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 /* --------------------------------------------------------------------------------------------- */