Ticket #3836: maint - set default `--with-homedir` value to `.mc` instead of `yes`
[midnight-commander.git] / src / editor / editcmd.c
blob4e1101db83d471ce4a24c8d72db3e6806c954015
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2017
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor high level editing commands
30 * \author Paul Sheer
31 * \date 1996, 1997
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
36 #include <config.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <stdlib.h>
48 #include "lib/global.h"
49 #include "lib/tty/tty.h"
50 #include "lib/tty/key.h" /* XCTRL */
51 #include "lib/mcconfig.h"
52 #include "lib/skin.h"
53 #include "lib/strutil.h" /* utf string functions */
54 #include "lib/fileloc.h"
55 #include "lib/lock.h"
56 #include "lib/util.h" /* tilde_expand() */
57 #include "lib/vfs/vfs.h"
58 #include "lib/widget.h"
59 #include "lib/event.h" /* mc_event_raise() */
60 #ifdef HAVE_CHARSET
61 #include "lib/charsets.h"
62 #endif
64 #include "src/history.h"
65 #include "src/setup.h" /* option_tab_spacing */
66 #ifdef HAVE_CHARSET
67 #include "src/selcodepage.h"
68 #endif
69 #include "src/keybind-defaults.h"
70 #include "src/util.h" /* check_for_default() */
72 #include "edit-impl.h"
73 #include "editwidget.h"
74 #include "editcmd_dialogs.h"
75 #ifdef HAVE_ASPELL
76 #include "spell.h"
77 #include "spell_dialogs.h"
78 #endif
79 #include "etags.h"
81 /*** global variables ****************************************************************************/
83 /* search and replace: */
84 int search_create_bookmark = FALSE;
86 /* queries on a save */
87 gboolean edit_confirm_save = TRUE;
89 /* whether we need to drop selection on copy to buffer */
90 gboolean option_drop_selection_on_copy = TRUE;
92 /*** file scope macro definitions ****************************************************************/
94 #define space_width 1
96 #define TEMP_BUF_LEN 1024
98 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
100 /*** file scope type declarations ****************************************************************/
102 typedef struct
104 simple_status_msg_t status_msg; /* base class */
106 gboolean first;
107 WEdit *edit;
108 off_t offset;
109 } edit_search_status_msg_t;
111 /*** file scope variables ************************************************************************/
113 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
115 /* --------------------------------------------------------------------------------------------- */
116 /*** file scope functions ************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 static int
120 edit_search_status_update_cb (status_msg_t * sm)
122 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
123 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
124 Widget *wd = WIDGET (sm->dlg);
126 if (verbose)
127 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
128 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
129 else
130 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
132 if (esm->first)
134 int wd_width;
135 Widget *lw = WIDGET (ssm->label);
137 wd_width = MAX (wd->cols, lw->cols + 6);
138 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
139 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
140 esm->first = FALSE;
143 return status_msg_common_update (sm);
146 /* --------------------------------------------------------------------------------------------- */
148 static cb_ret_t
149 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
151 switch (msg)
153 case MSG_CHANGED_FOCUS:
154 if (sender != NULL && sender->id == edit_save_mode_radio_id)
156 Widget *ww;
158 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
159 widget_disable (ww, RADIO (sender)->sel != 2);
160 return MSG_HANDLED;
163 return MSG_NOT_HANDLED;
165 default:
166 return dlg_default_callback (w, sender, msg, parm, data);
170 /* --------------------------------------------------------------------------------------------- */
172 /* If 0 (quick save) then a) create/truncate <filename> file,
173 b) save to <filename>;
174 if 1 (safe save) then a) save to <tempnam>,
175 b) rename <tempnam> to <filename>;
176 if 2 (do backups) then a) save to <tempnam>,
177 b) rename <filename> to <filename.backup_ext>,
178 c) rename <tempnam> to <filename>. */
180 /* returns 0 on error, -1 on abort */
182 static int
183 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
185 char *p;
186 gchar *tmp;
187 off_t filelen = 0;
188 int this_save_mode, rv, fd = -1;
189 vfs_path_t *real_filename_vpath;
190 vfs_path_t *savename_vpath = NULL;
191 const char *start_filename;
192 const vfs_path_element_t *vpath_element;
193 struct stat sb;
195 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
196 if (vpath_element == NULL)
197 return 0;
199 start_filename = vpath_element->path;
200 if (*start_filename == '\0')
201 return 0;
203 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
204 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
205 else
206 real_filename_vpath = vfs_path_clone (filename_vpath);
208 this_save_mode = option_save_mode;
209 if (this_save_mode != EDIT_QUICK_SAVE)
211 if (!vfs_file_is_local (real_filename_vpath)
212 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
215 * The file does not exists yet, so no safe save or
216 * backup are necessary.
218 this_save_mode = EDIT_QUICK_SAVE;
220 if (fd != -1)
221 mc_close (fd);
224 rv = mc_stat (real_filename_vpath, &sb);
225 if (rv == 0)
227 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
229 rv = edit_query_dialog3 (_("Warning"),
230 _("File has hard-links. Detach before saving?"),
231 _("&Yes"), _("&No"), _("&Cancel"));
232 switch (rv)
234 case 0:
235 this_save_mode = EDIT_SAFE_SAVE;
236 /* fallthrough */
237 case 1:
238 edit->skip_detach_prompt = 1;
239 break;
240 default:
241 vfs_path_free (real_filename_vpath);
242 return -1;
246 /* Prevent overwriting changes from other editor sessions. */
247 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
249 /* The default action is "Cancel". */
250 query_set_sel (1);
252 rv = edit_query_dialog2 (_("Warning"),
253 _("The file has been modified in the meantime. Save anyway?"),
254 _("&Yes"), _("&Cancel"));
255 if (rv != 0)
257 vfs_path_free (real_filename_vpath);
258 return -1;
263 if (this_save_mode != EDIT_QUICK_SAVE)
265 char *savedir, *saveprefix;
267 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
268 if (savedir == NULL)
269 savedir = g_strdup (".");
271 /* Token-related function never return leading slash, so we need add it manually */
272 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", (char *) NULL);
273 g_free (savedir);
274 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
275 g_free (saveprefix);
276 if (savename_vpath == NULL)
278 vfs_path_free (real_filename_vpath);
279 return 0;
281 /* FIXME:
282 * Close for now because mc_mkstemps use pure open system call
283 * to create temporary file and it needs to be reopened by
284 * VFS-aware mc_open().
286 close (fd);
288 else
289 savename_vpath = vfs_path_clone (real_filename_vpath);
291 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
292 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
294 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
295 if (fd == -1)
296 goto error_save;
298 /* pipe save */
299 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
300 if (p != NULL)
302 FILE *file;
304 mc_close (fd);
305 file = (FILE *) popen (p, "w");
307 if (file)
309 filelen = edit_write_stream (edit, file);
310 #if 1
311 pclose (file);
312 #else
313 if (pclose (file) != 0)
315 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
316 edit_error_dialog (_("Error"), tmp);
317 g_free (tmp);
318 g_free (p);
319 goto error_save;
321 #endif
323 else
325 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
326 edit_error_dialog (_("Error"), get_sys_error (tmp));
327 g_free (p);
328 g_free (tmp);
329 goto error_save;
331 g_free (p);
333 else if (edit->lb == LB_ASIS)
334 { /* do not change line breaks */
335 filelen = edit_buffer_write_file (&edit->buffer, fd);
337 if (filelen != edit->buffer.size)
339 mc_close (fd);
340 goto error_save;
342 if (mc_close (fd) != 0)
343 goto error_save;
344 /* Update the file information, especially the mtime. */
345 if (mc_stat (savename_vpath, &edit->stat1) == -1)
346 goto error_save;
348 else
349 { /* change line breaks */
350 FILE *file;
351 const vfs_path_element_t *path_element;
353 mc_close (fd);
355 path_element = vfs_path_get_by_index (savename_vpath, -1);
356 file = (FILE *) fopen (path_element->path, "w");
357 if (file != NULL)
359 filelen = edit_write_stream (edit, file);
360 fclose (file);
362 else
364 char *msg;
366 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
367 edit_error_dialog (_("Error"), msg);
368 g_free (msg);
369 goto error_save;
373 if (filelen != edit->buffer.size)
374 goto error_save;
376 if (this_save_mode == EDIT_DO_BACKUP)
378 char *tmp_store_filename;
379 vfs_path_element_t *last_vpath_element;
380 vfs_path_t *tmp_vpath;
381 gboolean ok;
383 g_assert (option_backup_ext != NULL);
385 /* add backup extension to the path */
386 tmp_vpath = vfs_path_clone (real_filename_vpath);
387 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
388 tmp_store_filename = last_vpath_element->path;
389 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
390 g_free (tmp_store_filename);
392 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
393 vfs_path_free (tmp_vpath);
394 if (!ok)
395 goto error_save;
398 if (this_save_mode != EDIT_QUICK_SAVE)
399 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
400 goto error_save;
402 vfs_path_free (real_filename_vpath);
403 vfs_path_free (savename_vpath);
404 return 1;
405 error_save:
406 /* FIXME: Is this safe ?
407 * if (this_save_mode != EDIT_QUICK_SAVE)
408 * mc_unlink (savename);
410 vfs_path_free (real_filename_vpath);
411 vfs_path_free (savename_vpath);
412 return 0;
415 /* --------------------------------------------------------------------------------------------- */
417 static gboolean
418 edit_check_newline (const edit_buffer_t * buf)
420 return !(option_check_nl_at_eof && buf->size > 0
421 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
422 && edit_query_dialog2 (_("Warning"),
423 _("The file you are saving does not end with a newline."),
424 _("C&ontinue"), _("&Cancel")) != 0);
427 /* --------------------------------------------------------------------------------------------- */
429 static vfs_path_t *
430 edit_get_save_file_as (WEdit * edit)
432 static LineBreaks cur_lb = LB_ASIS;
433 char *filename_res;
434 vfs_path_t *ret_vpath = NULL;
436 const char *lb_names[LB_NAMES] = {
437 N_("&Do not change"),
438 N_("&Unix format (LF)"),
439 N_("&Windows/DOS format (CR LF)"),
440 N_("&Macintosh format (CR)")
443 quick_widget_t quick_widgets[] = {
444 /* *INDENT-OFF* */
445 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
446 vfs_path_as_str (edit->filename_vpath), "save-as",
447 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
448 QUICK_SEPARATOR (TRUE),
449 QUICK_LABEL (N_("Change line breaks to:"), NULL),
450 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
451 QUICK_BUTTONS_OK_CANCEL,
452 QUICK_END
453 /* *INDENT-ON* */
456 quick_dialog_t qdlg = {
457 -1, -1, 64,
458 N_("Save As"), "[Save File As]",
459 quick_widgets, NULL, NULL
462 if (quick_dialog (&qdlg) != B_CANCEL)
464 char *fname;
466 edit->lb = cur_lb;
467 fname = tilde_expand (filename_res);
468 g_free (filename_res);
469 ret_vpath = vfs_path_from_str (fname);
470 g_free (fname);
473 return ret_vpath;
476 /* --------------------------------------------------------------------------------------------- */
478 /** returns TRUE on success */
480 static gboolean
481 edit_save_cmd (WEdit * edit)
483 int res, save_lock = 0;
485 if (!edit->locked && !edit->delete_file)
486 save_lock = lock_file (edit->filename_vpath);
487 res = edit_save_file (edit, edit->filename_vpath);
489 /* Maintain modify (not save) lock on failure */
490 if ((res > 0 && edit->locked) || save_lock)
491 edit->locked = unlock_file (edit->filename_vpath);
493 /* On failure try 'save as', it does locking on its own */
494 if (res == 0)
495 return edit_save_as_cmd (edit);
496 edit->force |= REDRAW_COMPLETELY;
497 if (res > 0)
499 edit->delete_file = 0;
500 edit->modified = 0;
503 return TRUE;
506 /* --------------------------------------------------------------------------------------------- */
508 * Load file content
510 * @param h screen the owner of editor window
511 * @param vpath vfs file path
512 * @return TRUE if file content was successfully loaded, FALSE otherwise
515 static inline gboolean
516 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
518 Widget *w = WIDGET (h);
520 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
523 /* --------------------------------------------------------------------------------------------- */
525 static void
526 edit_delete_column_of_text (WEdit * edit)
528 off_t p, q, r;
529 off_t m1, m2;
530 off_t n;
531 long b, c, d;
533 eval_marks (edit, &m1, &m2);
534 n = edit_buffer_get_forward_offset (&edit->buffer, m1, 0, m2) + 1;
535 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
536 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
537 b = MAX (MIN (c, d), MIN (edit->column1, edit->column2));
538 c = MAX (c, MAX (edit->column1, edit->column2));
540 while (n--)
542 r = edit_buffer_get_current_bol (&edit->buffer);
543 p = edit_move_forward3 (edit, r, b, 0);
544 q = edit_move_forward3 (edit, r, c, 0);
545 if (p < m1)
546 p = m1;
547 if (q > m2)
548 q = m2;
549 edit_cursor_move (edit, p - edit->buffer.curs1);
550 while (q > p)
552 /* delete line between margins */
553 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
554 edit_delete (edit, TRUE);
555 q--;
557 if (n)
558 /* move to next line except on the last delete */
559 edit_cursor_move (edit,
560 edit_buffer_get_forward_offset (&edit->buffer, edit->buffer.curs1, 1,
561 0) - edit->buffer.curs1);
565 /* --------------------------------------------------------------------------------------------- */
566 /** if success return 0 */
568 static int
569 edit_block_delete (WEdit * edit)
571 off_t start_mark, end_mark;
572 off_t curs_pos;
573 long curs_line, c1, c2;
575 if (!eval_marks (edit, &start_mark, &end_mark))
576 return 0;
578 if (edit->column_highlight && edit->mark2 < 0)
579 edit_mark_cmd (edit, FALSE);
580 if ((end_mark - start_mark) > option_max_undo / 2)
582 /* Warning message with a query to continue or cancel the operation */
583 if (edit_query_dialog2
584 (_("Warning"),
586 ("Block is large, you may not be able to undo this action"),
587 _("C&ontinue"), _("&Cancel")))
589 return 1;
592 c1 = MIN (edit->column1, edit->column2);
593 c2 = MAX (edit->column1, edit->column2);
594 edit->column1 = c1;
595 edit->column2 = c2;
597 edit_push_markers (edit);
599 curs_line = edit->buffer.curs_line;
601 curs_pos = edit->curs_col + edit->over_col;
603 /* move cursor to start of selection */
604 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
605 edit_scroll_screen_over_cursor (edit);
607 if (start_mark < end_mark)
609 if (edit->column_highlight)
611 off_t line_width;
613 if (edit->mark2 < 0)
614 edit_mark_cmd (edit, FALSE);
615 edit_delete_column_of_text (edit);
616 /* move cursor to the saved position */
617 edit_move_to_line (edit, curs_line);
618 /* calculate line width and cursor position before cut */
619 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
620 edit_buffer_get_current_eol (&edit->buffer));
621 if (option_cursor_beyond_eol && curs_pos > line_width)
622 edit->over_col = curs_pos - line_width;
624 else
626 off_t count;
628 for (count = start_mark; count < end_mark; count++)
629 edit_delete (edit, TRUE);
632 edit_set_markers (edit, 0, 0, 0, 0);
633 edit->force |= REDRAW_PAGE;
634 return 0;
637 /* --------------------------------------------------------------------------------------------- */
639 * Get EOL symbol for searching.
641 * @param edit editor object
642 * @return EOL symbol
645 static inline char
646 edit_search_get_current_end_line_char (const WEdit * edit)
648 switch (edit->lb)
650 case LB_MAC:
651 return '\r';
652 default:
653 return '\n';
657 /* --------------------------------------------------------------------------------------------- */
659 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
661 * @param search search object
662 * @return result of checks.
665 static edit_search_line_t
666 edit_get_search_line_type (mc_search_t * search)
668 edit_search_line_t search_line_type = 0;
670 if (search->search_type != MC_SEARCH_T_REGEX)
671 return search_line_type;
673 if (*search->original == '^')
674 search_line_type |= AT_START_LINE;
676 if (search->original[search->original_len - 1] == '$')
677 search_line_type |= AT_END_LINE;
678 return search_line_type;
681 /* --------------------------------------------------------------------------------------------- */
683 * Calculating the start position of next line.
685 * @param buf editor buffer object
686 * @param current_pos current position
687 * @param max_pos max position
688 * @param end_string_symbol end of line symbol
689 * @return start position of next line
692 static off_t
693 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
694 char end_string_symbol)
696 off_t i;
698 for (i = current_pos; i < max_pos; i++)
700 current_pos++;
701 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
702 break;
705 return current_pos;
708 /* --------------------------------------------------------------------------------------------- */
710 * Calculating the end position of previous line.
712 * @param buf editor buffer object
713 * @param current_pos current position
714 * @param end_string_symbol end of line symbol
715 * @return end position of previous line
718 static off_t
719 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
720 char end_string_symbol)
722 off_t i;
724 for (i = current_pos - 1; i >= 0; i--)
725 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
726 break;
728 return i;
731 /* --------------------------------------------------------------------------------------------- */
733 * Calculating the start position of previous line.
735 * @param buf editor buffer object
736 * @param current_pos current position
737 * @param end_string_symbol end of line symbol
738 * @return start position of previous line
741 static inline off_t
742 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
743 char end_string_symbol)
745 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
746 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
748 return (current_pos + 1);
751 /* --------------------------------------------------------------------------------------------- */
753 * Calculating the start position of current line.
755 * @param buf editor buffer object
756 * @param current_pos current position
757 * @param end_string_symbol end of line symbol
758 * @return start position of current line
761 static inline off_t
762 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
763 char end_string_symbol)
765 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
767 return (current_pos + 1);
770 /* --------------------------------------------------------------------------------------------- */
772 * Fixing (if needed) search start position if 'only in selection' option present.
774 * @param edit editor object
777 static void
778 edit_search_fix_search_start_if_selection (WEdit * edit)
780 off_t start_mark = 0;
781 off_t end_mark = 0;
783 if (!edit_search_options.only_in_selection)
784 return;
786 if (!eval_marks (edit, &start_mark, &end_mark))
787 return;
789 if (edit_search_options.backwards)
791 if (edit->search_start > end_mark || edit->search_start <= start_mark)
792 edit->search_start = end_mark;
794 else
796 if (edit->search_start < start_mark || edit->search_start >= end_mark)
797 edit->search_start = start_mark;
801 /* --------------------------------------------------------------------------------------------- */
803 static gboolean
804 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
806 WEdit *edit = esm->edit;
807 off_t search_start = edit->search_start;
808 off_t search_end;
809 off_t start_mark = 0;
810 off_t end_mark = edit->buffer.size;
811 char end_string_symbol;
813 end_string_symbol = edit_search_get_current_end_line_char (edit);
815 /* prepare for search */
816 if (edit_search_options.only_in_selection)
818 if (!eval_marks (edit, &start_mark, &end_mark))
820 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
821 return FALSE;
824 /* fix the start and the end of search block positions */
825 if ((edit->search_line_type & AT_START_LINE) != 0
826 && (start_mark != 0
827 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
828 start_mark =
829 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
830 end_string_symbol);
832 if ((edit->search_line_type & AT_END_LINE) != 0
833 && (end_mark - 1 != edit->buffer.size
834 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
835 end_mark =
836 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
838 if (start_mark >= end_mark)
840 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
841 return FALSE;
844 else if (edit_search_options.backwards)
845 end_mark = MAX (1, edit->buffer.curs1) - 1;
847 /* search */
848 if (edit_search_options.backwards)
850 /* backward search */
851 search_end = end_mark;
853 if ((edit->search_line_type & AT_START_LINE) != 0)
854 search_start =
855 edit_calculate_start_of_current_line (&edit->buffer, search_start,
856 end_string_symbol);
858 while (search_start >= start_mark)
860 gboolean ok;
862 if (search_end > (off_t) (search_start + edit->search->original_len)
863 && mc_search_is_fixed_search_str (edit->search))
864 search_end = search_start + edit->search->original_len;
866 ok = mc_search_run (edit->search, (void *) esm, search_start, search_end, len);
868 if (ok && edit->search->normal_offset == search_start)
869 return TRUE;
871 /* We abort the search in case of a pattern error, or if the user aborts
872 the search. In other words: in all cases except "string not found". */
873 if (!ok && edit->search->error != MC_SEARCH_E_NOTFOUND)
874 return FALSE;
876 if ((edit->search_line_type & AT_START_LINE) != 0)
877 search_start =
878 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
879 end_string_symbol);
880 else
881 search_start--;
884 mc_search_set_error (edit->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND));
885 return FALSE;
888 /* forward search */
889 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
890 search_start =
891 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
892 end_string_symbol);
894 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
897 /* --------------------------------------------------------------------------------------------- */
899 static char *
900 edit_replace_cmd__conv_to_display (const char *str)
902 #ifdef HAVE_CHARSET
903 GString *tmp;
905 tmp = str_convert_to_display (str);
906 if (tmp != NULL)
908 if (tmp->len != 0)
909 return g_string_free (tmp, FALSE);
910 g_string_free (tmp, TRUE);
912 #endif
913 return g_strdup (str);
916 /* --------------------------------------------------------------------------------------------- */
918 static char *
919 edit_replace_cmd__conv_to_input (char *str)
921 #ifdef HAVE_CHARSET
922 GString *tmp;
924 tmp = str_convert_to_input (str);
925 if (tmp != NULL)
927 if (tmp->len != 0)
928 return g_string_free (tmp, FALSE);
929 g_string_free (tmp, TRUE);
931 #endif
932 return g_strdup (str);
935 /* --------------------------------------------------------------------------------------------- */
937 static void
938 edit_show_search_error (const WEdit * edit, const char *title)
940 if (edit->search->error == MC_SEARCH_E_NOTFOUND)
941 edit_query_dialog (title, _(STR_E_NOTFOUND));
942 else if (edit->search->error_str != NULL)
943 edit_query_dialog (title, edit->search->error_str);
946 /* --------------------------------------------------------------------------------------------- */
948 static void
949 edit_do_search (WEdit * edit)
951 edit_search_status_msg_t esm;
952 gsize len = 0;
954 if (edit->search == NULL)
955 edit->search_start = edit->buffer.curs1;
957 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
959 esm.first = TRUE;
960 esm.edit = edit;
961 esm.offset = edit->search_start;
963 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
964 edit_search_status_update_cb, NULL);
966 if (search_create_bookmark)
968 int found = 0, books = 0;
969 long l = 0, l_last = -1;
970 long q = 0;
972 search_create_bookmark = FALSE;
973 book_mark_flush (edit, -1);
975 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
977 if (found == 0)
978 edit->search_start = edit->search->normal_offset;
979 found++;
980 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
981 if (l != l_last)
983 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
984 books++;
986 l_last = l;
987 q = edit->search->normal_offset + 1;
990 if (found == 0)
991 edit_error_dialog (_("Search"), _(STR_E_NOTFOUND));
992 else
993 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
995 else
997 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
998 && edit_search_options.backwards)
999 edit->search_start--;
1001 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1002 && !edit_search_options.backwards)
1003 edit->search_start++;
1005 if (editcmd_find (&esm, &len))
1007 edit->found_start = edit->search_start = edit->search->normal_offset;
1008 edit->found_len = len;
1009 edit->over_col = 0;
1010 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1011 edit_scroll_screen_over_cursor (edit);
1012 if (edit_search_options.backwards)
1013 edit->search_start--;
1014 else
1015 edit->search_start++;
1017 else
1019 edit->search_start = edit->buffer.curs1;
1020 edit_show_search_error (edit, _("Search"));
1024 status_msg_deinit (STATUS_MSG (&esm));
1026 edit->force |= REDRAW_COMPLETELY;
1027 edit_scroll_screen_over_cursor (edit);
1030 /* --------------------------------------------------------------------------------------------- */
1032 static void
1033 edit_search (WEdit * edit)
1035 if (editcmd_dialog_search_show (edit))
1037 edit->search_line_type = edit_get_search_line_type (edit->search);
1038 edit_search_fix_search_start_if_selection (edit);
1039 edit_do_search (edit);
1043 /* --------------------------------------------------------------------------------------------- */
1044 /** Return a null terminated length of text. Result must be g_free'd */
1046 static unsigned char *
1047 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1049 unsigned char *s, *r;
1051 r = s = g_malloc0 (finish - start + 1);
1052 if (edit->column_highlight)
1054 *l = 0;
1055 /* copy from buffer, excluding chars that are out of the column 'margins' */
1056 while (start < finish)
1058 int c;
1059 off_t x;
1061 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1062 c = edit_buffer_get_byte (&edit->buffer, start);
1063 if ((x >= edit->column1 && x < edit->column2)
1064 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1066 *s++ = c;
1067 (*l)++;
1069 start++;
1072 else
1074 *l = finish - start;
1075 while (start < finish)
1076 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1078 *s = '\0';
1079 return r;
1082 /* --------------------------------------------------------------------------------------------- */
1083 /** copies a block to clipboard file */
1085 static gboolean
1086 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1088 gboolean ret;
1089 gchar *tmp;
1091 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1092 ret = edit_save_block (edit, tmp, start, finish);
1093 g_free (tmp);
1094 return ret;
1097 /* --------------------------------------------------------------------------------------------- */
1099 static void
1100 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1102 FILE *p = 0;
1103 char *s;
1105 to = name_quote (to, FALSE);
1106 subject = name_quote (subject, FALSE);
1107 cc = name_quote (cc, FALSE);
1108 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1109 g_free (to);
1110 g_free (subject);
1111 g_free (cc);
1113 if (s != NULL)
1115 p = popen (s, "w");
1116 g_free (s);
1119 if (p != NULL)
1121 off_t i;
1123 for (i = 0; i < buf->size; i++)
1124 fputc (edit_buffer_get_byte (buf, i), p);
1125 pclose (p);
1129 /* --------------------------------------------------------------------------------------------- */
1130 /** find first character of current word */
1132 static gboolean
1133 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1135 int c;
1136 off_t i;
1138 /* return if at begin of file */
1139 if (buf->curs1 <= 0)
1140 return FALSE;
1142 c = edit_buffer_get_previous_byte (buf);
1143 /* return if not at end or in word */
1144 if (is_break_char (c))
1145 return FALSE;
1147 /* search start of word to be completed */
1148 for (i = 1;; i++)
1150 int last;
1152 last = c;
1153 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1155 if (is_break_char (c))
1157 /* return if word starts with digit */
1158 if (isdigit (last))
1159 return FALSE;
1161 break;
1165 /* success */
1166 *word_start = buf->curs1 - i; /* start found */
1167 *word_len = (gsize) i;
1168 return TRUE;
1171 /* --------------------------------------------------------------------------------------------- */
1173 * Get current word under cursor
1175 * @param esm status message window
1176 * @param srch mc_search object
1177 * @param word_start start word position
1179 * @return newly allocated string or NULL if no any words under cursor
1182 static char *
1183 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1184 off_t word_start)
1186 WEdit *edit = esm->edit;
1187 gsize len = 0;
1188 off_t i;
1189 GString *temp;
1191 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1192 return NULL;
1194 temp = g_string_sized_new (len);
1196 for (i = 0; i < (off_t) len; i++)
1198 int chr;
1200 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1201 if (!isspace (chr))
1202 g_string_append_c (temp, chr);
1205 return g_string_free (temp, temp->len == 0);
1208 /* --------------------------------------------------------------------------------------------- */
1209 /** collect the possible completions */
1211 static gsize
1212 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1213 char *match_expr, GString ** compl, gsize * num)
1215 gsize len = 0;
1216 gsize max_len = 0;
1217 gsize i;
1218 int skip;
1219 GString *temp;
1220 mc_search_t *srch;
1221 off_t last_byte, start = -1;
1222 char *current_word;
1223 gboolean entire_file;
1224 edit_search_status_msg_t esm;
1226 #ifdef HAVE_CHARSET
1227 srch = mc_search_new (match_expr, cp_source);
1228 #else
1229 srch = mc_search_new (match_expr, NULL);
1230 #endif
1231 if (srch == NULL)
1232 return 0;
1234 entire_file =
1235 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
1236 "editor_wordcompletion_collect_entire_file", 0);
1238 last_byte = entire_file ? edit->buffer.size : word_start;
1240 srch->search_type = MC_SEARCH_T_REGEX;
1241 srch->is_case_sensitive = TRUE;
1242 srch->search_fn = edit_search_cmd_callback;
1243 srch->update_fn = edit_search_update_callback;
1245 esm.first = TRUE;
1246 esm.edit = edit;
1247 esm.offset = entire_file ? 0 : word_start;
1249 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1250 edit_search_status_update_cb, NULL);
1252 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1254 temp = g_string_new ("");
1256 /* collect max MAX_WORD_COMPLETIONS completions */
1257 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1259 g_string_set_size (temp, 0);
1260 start = srch->normal_offset;
1262 /* add matched completion if not yet added */
1263 for (i = 0; i < len; i++)
1265 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1266 if (isspace (skip))
1267 continue;
1269 /* skip current word */
1270 if (start + (off_t) i == word_start)
1271 break;
1273 g_string_append_c (temp, skip);
1276 if (temp->len == 0)
1277 continue;
1279 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1280 continue;
1282 skip = 0;
1284 for (i = 0; i < *num; i++)
1286 if (strncmp
1287 ((char *) &compl[i]->str[word_len],
1288 (char *) &temp->str[word_len], MAX (len, compl[i]->len) - word_len) == 0)
1290 GString *this = compl[i];
1291 for (++i; i < *num; i++)
1292 compl[i - 1] = compl[i];
1293 compl[*num - 1] = this;
1294 skip = 1;
1295 break; /* skip it, already added */
1298 if (skip != 0)
1299 continue;
1301 if (*num == MAX_WORD_COMPLETIONS)
1303 g_string_free (compl[0], TRUE);
1304 for (i = 1; i < *num; i++)
1305 compl[i - 1] = compl[i];
1306 (*num)--;
1308 #ifdef HAVE_CHARSET
1310 GString *recoded;
1311 recoded = str_convert_to_display (temp->str);
1313 if (recoded && recoded->len)
1314 g_string_assign (temp, recoded->str);
1316 g_string_free (recoded, TRUE);
1318 #endif
1319 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1320 start += len;
1322 /* note the maximal length needed for the completion dialog */
1323 if (len > max_len)
1324 max_len = len;
1327 status_msg_deinit (STATUS_MSG (&esm));
1328 mc_search_free (srch);
1329 g_string_free (temp, TRUE);
1330 g_free (current_word);
1332 return max_len;
1335 /* --------------------------------------------------------------------------------------------- */
1337 static void
1338 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1339 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1341 off_t i, cursor;
1342 long col;
1344 cursor = edit->buffer.curs1;
1345 col = edit_get_col (edit);
1347 for (i = 0; i < size; i++)
1349 if (data[i] != '\n')
1350 edit_insert (edit, data[i]);
1351 else
1352 { /* fill in and move to next line */
1353 long l;
1354 off_t p;
1356 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1358 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1359 edit_insert (edit, ' ');
1361 for (p = edit->buffer.curs1;; p++)
1363 if (p == edit->buffer.size)
1365 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1366 edit_insert_ahead (edit, '\n');
1367 p++;
1368 break;
1370 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1372 p++;
1373 break;
1376 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1378 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1379 edit_insert (edit, ' ');
1383 *col1 = col;
1384 *col2 = col + width;
1385 *start_pos = cursor;
1386 *end_pos = edit->buffer.curs1;
1387 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1390 /* --------------------------------------------------------------------------------------------- */
1392 static int
1393 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1395 const macros_t *m1 = (const macros_t *) macro1;
1396 const macros_t *m2 = (const macros_t *) macro2;
1398 return m1->hotkey - m2->hotkey;
1401 /* --------------------------------------------------------------------------------------------- */
1403 static void
1404 edit_macro_sort_by_hotkey (void)
1406 if (macros_list != NULL && macros_list->len != 0)
1407 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1410 /* --------------------------------------------------------------------------------------------- */
1412 static gboolean
1413 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1415 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1416 macros_t *result;
1417 macros_t search_macro;
1419 (void) edit;
1421 search_macro.hotkey = hotkey;
1422 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1423 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1425 if (result != NULL && result->macro != NULL)
1427 *indx = (result - array_start);
1428 *macros = result;
1429 return TRUE;
1431 *indx = 0;
1432 return FALSE;
1435 /* --------------------------------------------------------------------------------------------- */
1436 /** returns FALSE on error */
1438 static gboolean
1439 edit_delete_macro (WEdit * edit, int hotkey)
1441 mc_config_t *macros_config = NULL;
1442 const char *section_name = "editor";
1443 gchar *macros_fname;
1444 guint indx;
1445 char *skeyname;
1446 const macros_t *macros = NULL;
1448 /* clear array of actions for current hotkey */
1449 while (edit_get_macro (edit, hotkey, &macros, &indx))
1451 if (macros->macro != NULL)
1452 g_array_free (macros->macro, TRUE);
1453 macros = NULL;
1454 g_array_remove_index (macros_list, indx);
1455 edit_macro_sort_by_hotkey ();
1458 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1459 macros_config = mc_config_init (macros_fname, FALSE);
1460 g_free (macros_fname);
1462 if (macros_config == NULL)
1463 return FALSE;
1465 skeyname = lookup_key_by_code (hotkey);
1466 while (mc_config_del_key (macros_config, section_name, skeyname))
1468 g_free (skeyname);
1469 mc_config_save_file (macros_config, NULL);
1470 mc_config_deinit (macros_config);
1471 return TRUE;
1474 /* --------------------------------------------------------------------------------------------- */
1476 * Callback for the iteration of objects in the 'editors' array.
1477 * Toggle syntax highlighting in editor object.
1479 * @param data probably WEdit object
1480 * @param user_data unused
1483 static void
1484 edit_syntax_onoff_cb (void *data, void *user_data)
1486 (void) user_data;
1488 if (edit_widget_is_editor (CONST_WIDGET (data)))
1490 WEdit *edit = (WEdit *) data;
1492 if (option_syntax_highlighting)
1493 edit_load_syntax (edit, NULL, edit->syntax_type);
1494 edit->force |= REDRAW_PAGE;
1498 /* --------------------------------------------------------------------------------------------- */
1500 * Callback for the iteration of objects in the 'editors' array.
1501 * Redraw editor object.
1503 * @param data probably WEdit object
1504 * @param user_data unused
1507 static void
1508 edit_redraw_page_cb (void *data, void *user_data)
1510 (void) user_data;
1512 if (edit_widget_is_editor (CONST_WIDGET (data)))
1513 ((WEdit *) data)->force |= REDRAW_PAGE;
1516 /* --------------------------------------------------------------------------------------------- */
1518 * Insert autocompleted word into editor.
1520 * @param edit editor object
1521 * @param completion word for completion
1522 * @param word_len offset from beginning for insert
1525 static void
1526 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1528 #ifdef HAVE_CHARSET
1529 GString *temp;
1531 temp = str_convert_to_input (completion);
1533 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1534 edit_insert (edit, *completion);
1535 g_string_free (temp, TRUE);
1536 #else
1537 for (completion += word_len; *completion != '\0'; completion++)
1538 edit_insert (edit, *completion);
1539 #endif
1542 /* --------------------------------------------------------------------------------------------- */
1543 /*** public functions ****************************************************************************/
1544 /* --------------------------------------------------------------------------------------------- */
1546 void
1547 edit_refresh_cmd (void)
1549 clr_scr ();
1550 repaint_screen ();
1551 tty_keypad (TRUE);
1554 /* --------------------------------------------------------------------------------------------- */
1556 * Toggle syntax highlighting in all editor windows.
1558 * @param h root widget for all windows
1561 void
1562 edit_syntax_onoff_cmd (WDialog * h)
1564 option_syntax_highlighting = !option_syntax_highlighting;
1565 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1566 dlg_redraw (h);
1569 /* --------------------------------------------------------------------------------------------- */
1571 * Toggle tabs showing in all editor windows.
1573 * @param h root widget for all windows
1576 void
1577 edit_show_tabs_tws_cmd (WDialog * h)
1579 enable_show_tabs_tws = !enable_show_tabs_tws;
1580 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1581 dlg_redraw (h);
1584 /* --------------------------------------------------------------------------------------------- */
1586 * Toggle right margin showing in all editor windows.
1588 * @param h root widget for all windows
1591 void
1592 edit_show_margin_cmd (WDialog * h)
1594 show_right_margin = !show_right_margin;
1595 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1596 dlg_redraw (h);
1599 /* --------------------------------------------------------------------------------------------- */
1601 * Toggle line numbers showing in all editor windows.
1603 * @param h root widget for all windows
1606 void
1607 edit_show_numbers_cmd (WDialog * h)
1609 option_line_state = !option_line_state;
1610 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1611 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1612 dlg_redraw (h);
1615 /* --------------------------------------------------------------------------------------------- */
1617 void
1618 edit_save_mode_cmd (void)
1620 char *str_result = NULL;
1622 const char *str[] = {
1623 N_("&Quick save"),
1624 N_("&Safe save"),
1625 N_("&Do backups with following extension:")
1628 #ifdef ENABLE_NLS
1629 size_t i;
1631 for (i = 0; i < 3; i++)
1632 str[i] = _(str[i]);
1633 #endif
1635 g_assert (option_backup_ext != NULL);
1638 quick_widget_t quick_widgets[] = {
1639 /* *INDENT-OFF* */
1640 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1641 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1642 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1643 QUICK_SEPARATOR (TRUE),
1644 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1645 QUICK_BUTTONS_OK_CANCEL,
1646 QUICK_END
1647 /* *INDENT-ON* */
1650 quick_dialog_t qdlg = {
1651 -1, -1, 38,
1652 N_("Edit Save Mode"), "[Edit Save Mode]",
1653 quick_widgets, edit_save_mode_callback, NULL
1656 if (quick_dialog (&qdlg) != B_CANCEL)
1658 g_free (option_backup_ext);
1659 option_backup_ext = str_result;
1664 /* --------------------------------------------------------------------------------------------- */
1666 void
1667 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1669 vfs_path_free (edit->filename_vpath);
1670 edit->filename_vpath = vfs_path_clone (name_vpath);
1672 if (edit->dir_vpath == NULL)
1673 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1676 /* --------------------------------------------------------------------------------------------- */
1677 /* Here we want to warn the users of overwriting an existing file,
1678 but only if they have made a change to the filename */
1679 /* returns TRUE on success */
1680 gboolean
1681 edit_save_as_cmd (WEdit * edit)
1683 /* This heads the 'Save As' dialog box */
1684 vfs_path_t *exp_vpath;
1685 int save_lock = 0;
1686 int different_filename = 0;
1688 if (!edit_check_newline (&edit->buffer))
1689 return FALSE;
1691 exp_vpath = edit_get_save_file_as (edit);
1692 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1694 if (exp_vpath != NULL)
1696 if (vfs_path_len (exp_vpath) == 0)
1697 goto ret;
1698 else
1700 int rv;
1702 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1704 int file;
1705 struct stat sb;
1707 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1709 edit_error_dialog (_("Save as"),
1710 get_sys_error (_
1711 ("Cannot save: destination is not a regular file")));
1712 goto ret;
1715 different_filename = 1;
1716 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1718 if (file != -1)
1720 /* the file exists */
1721 mc_close (file);
1722 /* Overwrite the current file or cancel the operation */
1723 if (edit_query_dialog2
1724 (_("Warning"),
1725 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1726 goto ret;
1728 else
1730 edit->stat1.st_mode |= S_IWUSR;
1732 save_lock = lock_file (exp_vpath);
1734 else
1736 /* filenames equal, check if already locked */
1737 if (!edit->locked && !edit->delete_file)
1738 save_lock = lock_file (exp_vpath);
1741 if (different_filename)
1744 * Allow user to write into saved (under another name) file
1745 * even if original file had r/o user permissions.
1747 edit->stat1.st_mode |= S_IWRITE;
1750 rv = edit_save_file (edit, exp_vpath);
1751 switch (rv)
1753 case 1:
1754 /* Successful, so unlock both files */
1755 if (different_filename)
1757 if (save_lock)
1758 unlock_file (exp_vpath);
1759 if (edit->locked)
1760 edit->locked = unlock_file (edit->filename_vpath);
1762 else
1764 if (edit->locked || save_lock)
1765 edit->locked = unlock_file (edit->filename_vpath);
1768 edit_set_filename (edit, exp_vpath);
1769 if (edit->lb != LB_ASIS)
1770 edit_reload (edit, exp_vpath);
1771 edit->modified = 0;
1772 edit->delete_file = 0;
1773 if (different_filename)
1774 edit_load_syntax (edit, NULL, edit->syntax_type);
1775 vfs_path_free (exp_vpath);
1776 edit->force |= REDRAW_COMPLETELY;
1777 return TRUE;
1778 default:
1779 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1780 /* fallthrough */
1781 case -1:
1782 /* Failed, so maintain modify (not save) lock */
1783 if (save_lock)
1784 unlock_file (exp_vpath);
1785 break;
1790 ret:
1791 vfs_path_free (exp_vpath);
1792 edit->force |= REDRAW_COMPLETELY;
1793 return FALSE;
1796 /* {{{ Macro stuff starts here */
1797 /* --------------------------------------------------------------------------------------------- */
1799 void
1800 edit_delete_macro_cmd (WEdit * edit)
1802 int hotkey;
1804 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1806 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1807 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1810 /* --------------------------------------------------------------------------------------------- */
1812 /** returns FALSE on error */
1813 gboolean
1814 edit_execute_macro (WEdit * edit, int hotkey)
1816 gboolean res = FALSE;
1818 if (hotkey != 0)
1820 const macros_t *macros;
1821 guint indx;
1823 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1824 macros->macro != NULL && macros->macro->len != 0)
1826 guint i;
1828 edit->force |= REDRAW_PAGE;
1830 for (i = 0; i < macros->macro->len; i++)
1832 const macro_action_t *m_act;
1834 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1835 edit_execute_cmd (edit, m_act->action, m_act->ch);
1836 res = TRUE;
1841 return res;
1844 /* --------------------------------------------------------------------------------------------- */
1846 /** returns FALSE on error */
1847 gboolean
1848 edit_store_macro_cmd (WEdit * edit)
1850 int i;
1851 int hotkey;
1852 GString *marcros_string;
1853 mc_config_t *macros_config = NULL;
1854 const char *section_name = "editor";
1855 gchar *macros_fname;
1856 GArray *macros; /* current macro */
1857 int tmp_act;
1858 gboolean have_macro = FALSE;
1859 char *skeyname = NULL;
1861 hotkey =
1862 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1863 if (hotkey == ESC_CHAR)
1864 return FALSE;
1866 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1868 /* return FALSE if try assign macro into restricted hotkeys */
1869 if (tmp_act == CK_MacroStartRecord
1870 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1871 return FALSE;
1873 edit_delete_macro (edit, hotkey);
1875 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1876 macros_config = mc_config_init (macros_fname, FALSE);
1877 g_free (macros_fname);
1879 if (macros_config == NULL)
1880 return FALSE;
1882 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1884 marcros_string = g_string_sized_new (250);
1885 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1887 skeyname = lookup_key_by_code (hotkey);
1889 for (i = 0; i < macro_index; i++)
1891 macro_action_t m_act;
1892 const char *action_name;
1894 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1896 if (action_name == NULL)
1897 break;
1899 m_act.action = record_macro_buf[i].action;
1900 m_act.ch = record_macro_buf[i].ch;
1901 g_array_append_val (macros, m_act);
1902 have_macro = TRUE;
1903 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1904 (int) record_macro_buf[i].ch);
1906 if (have_macro)
1908 macros_t macro;
1909 macro.hotkey = hotkey;
1910 macro.macro = macros;
1911 g_array_append_val (macros_list, macro);
1912 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1914 else
1915 mc_config_del_key (macros_config, section_name, skeyname);
1917 g_free (skeyname);
1918 edit_macro_sort_by_hotkey ();
1920 g_string_free (marcros_string, TRUE);
1921 mc_config_save_file (macros_config, NULL);
1922 mc_config_deinit (macros_config);
1923 return TRUE;
1926 /* --------------------------------------------------------------------------------------------- */
1928 gboolean
1929 edit_repeat_macro_cmd (WEdit * edit)
1931 int i, j;
1932 char *f;
1933 long count_repeat;
1934 char *error = NULL;
1936 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1937 INPUT_COMPLETE_NONE);
1938 if (f == NULL || *f == '\0')
1940 g_free (f);
1941 return FALSE;
1944 count_repeat = strtol (f, &error, 0);
1946 if (*error != '\0')
1948 g_free (f);
1949 return FALSE;
1952 g_free (f);
1954 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1955 edit->force |= REDRAW_PAGE;
1957 for (j = 0; j < count_repeat; j++)
1958 for (i = 0; i < macro_index; i++)
1959 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1960 edit_update_screen (edit);
1961 return TRUE;
1964 /* --------------------------------------------------------------------------------------------- */
1965 /** return FALSE on error */
1967 gboolean
1968 edit_load_macro_cmd (WEdit * edit)
1970 mc_config_t *macros_config = NULL;
1971 gchar **profile_keys, **keys;
1972 gchar **values, **curr_values;
1973 const char *section_name = "editor";
1974 gchar *macros_fname;
1976 (void) edit;
1978 if (macros_list == NULL || macros_list->len != 0)
1979 return FALSE;
1981 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1982 macros_config = mc_config_init (macros_fname, TRUE);
1983 g_free (macros_fname);
1985 if (macros_config == NULL)
1986 return FALSE;
1988 keys = mc_config_get_keys (macros_config, section_name, NULL);
1990 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1992 int hotkey;
1993 gboolean have_macro = FALSE;
1994 GArray *macros;
1995 macros_t macro;
1997 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1998 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
1999 hotkey = lookup_key (*profile_keys, NULL);
2001 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2003 char **macro_pair = NULL;
2005 macro_pair = g_strsplit (*curr_values, ":", 2);
2006 if (macro_pair != NULL)
2008 macro_action_t m_act;
2009 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2010 m_act.action = 0;
2011 else
2013 m_act.action = keybind_lookup_action (macro_pair[0]);
2014 MC_PTR_FREE (macro_pair[0]);
2016 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2017 m_act.ch = -1;
2018 else
2020 m_act.ch = strtol (macro_pair[1], NULL, 0);
2021 MC_PTR_FREE (macro_pair[1]);
2023 if (m_act.action != 0)
2025 /* a shell command */
2026 if ((m_act.action / CK_PipeBlock (0)) == 1)
2028 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2029 m_act.ch = -1;
2031 g_array_append_val (macros, m_act);
2032 have_macro = TRUE;
2034 g_strfreev (macro_pair);
2035 macro_pair = NULL;
2038 if (have_macro)
2040 macro.hotkey = hotkey;
2041 macro.macro = macros;
2042 g_array_append_val (macros_list, macro);
2044 g_strfreev (values);
2046 g_strfreev (keys);
2047 mc_config_deinit (macros_config);
2048 edit_macro_sort_by_hotkey ();
2049 return TRUE;
2052 /* }}} Macro stuff end here */
2054 /* --------------------------------------------------------------------------------------------- */
2055 /** returns TRUE on success */
2057 gboolean
2058 edit_save_confirm_cmd (WEdit * edit)
2060 if (edit->filename_vpath == NULL)
2061 return edit_save_as_cmd (edit);
2063 if (!edit_check_newline (&edit->buffer))
2064 return FALSE;
2066 if (edit_confirm_save)
2068 char *f;
2069 gboolean ok;
2071 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2072 vfs_path_as_str (edit->filename_vpath));
2073 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2074 g_free (f);
2075 if (!ok)
2076 return FALSE;
2078 return edit_save_cmd (edit);
2081 /* --------------------------------------------------------------------------------------------- */
2083 * Ask file to edit and load it.
2085 * @return TRUE on success or cancel of ask.
2088 gboolean
2089 edit_load_cmd (WDialog * h)
2091 char *exp;
2092 gboolean ret = TRUE; /* possible cancel */
2094 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2095 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2096 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2098 if (exp != NULL && *exp != '\0')
2100 vfs_path_t *exp_vpath;
2102 exp_vpath = vfs_path_from_str (exp);
2103 ret = edit_load_file_from_filename (h, exp_vpath);
2104 vfs_path_free (exp_vpath);
2107 g_free (exp);
2109 return ret;
2112 /* --------------------------------------------------------------------------------------------- */
2114 * Load syntax file to edit.
2116 * @return TRUE on success
2119 gboolean
2120 edit_load_syntax_file (WDialog * h)
2122 vfs_path_t *extdir_vpath;
2123 int dir = 0;
2124 gboolean ret = FALSE;
2126 if (geteuid () == 0)
2127 dir = query_dialog (_("Syntax file edit"),
2128 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2129 _("&User"), _("&System wide"));
2131 extdir_vpath =
2132 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2133 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2135 vfs_path_free (extdir_vpath);
2136 extdir_vpath =
2137 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2140 if (dir == 0)
2142 vfs_path_t *user_syntax_file_vpath;
2144 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2145 check_for_default (extdir_vpath, user_syntax_file_vpath);
2146 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2147 vfs_path_free (user_syntax_file_vpath);
2149 else if (dir == 1)
2150 ret = edit_load_file_from_filename (h, extdir_vpath);
2152 vfs_path_free (extdir_vpath);
2154 return ret;
2157 /* --------------------------------------------------------------------------------------------- */
2159 * Load menu file to edit.
2161 * @return TRUE on success
2164 gboolean
2165 edit_load_menu_file (WDialog * h)
2167 vfs_path_t *buffer_vpath;
2168 vfs_path_t *menufile_vpath;
2169 int dir;
2170 gboolean ret;
2172 query_set_sel (1);
2173 dir = query_dialog (_("Menu edit"),
2174 _("Which menu file do you want to edit?"), D_NORMAL,
2175 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2177 menufile_vpath =
2178 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2179 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2181 vfs_path_free (menufile_vpath);
2182 menufile_vpath =
2183 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2186 switch (dir)
2188 case 0:
2189 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2190 check_for_default (menufile_vpath, buffer_vpath);
2191 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2192 break;
2194 case 1:
2195 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2196 check_for_default (menufile_vpath, buffer_vpath);
2197 break;
2199 case 2:
2200 buffer_vpath =
2201 vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2202 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2204 vfs_path_free (buffer_vpath);
2205 buffer_vpath =
2206 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, (char *) NULL);
2208 break;
2210 default:
2211 vfs_path_free (menufile_vpath);
2212 return FALSE;
2215 ret = edit_load_file_from_filename (h, buffer_vpath);
2217 vfs_path_free (buffer_vpath);
2218 vfs_path_free (menufile_vpath);
2220 return ret;
2223 /* --------------------------------------------------------------------------------------------- */
2225 * Close window with opened file.
2227 * @return TRUE if file was closed.
2230 gboolean
2231 edit_close_cmd (WEdit * edit)
2233 gboolean ret;
2235 ret = (edit != NULL) && edit_ok_to_exit (edit);
2237 if (ret)
2239 WDialog *h = WIDGET (edit)->owner;
2241 if (edit->locked != 0)
2242 unlock_file (edit->filename_vpath);
2244 del_widget (edit);
2246 if (edit_widget_is_editor (CONST_WIDGET (h->current->data)))
2247 edit = (WEdit *) h->current->data;
2248 else
2250 edit = find_editor (h);
2251 if (edit != NULL)
2252 widget_select (WIDGET (edit));
2256 if (edit != NULL)
2257 edit->force |= REDRAW_COMPLETELY;
2259 return ret;
2262 /* --------------------------------------------------------------------------------------------- */
2264 if mark2 is -1 then marking is from mark1 to the cursor.
2265 Otherwise its between the markers. This handles this.
2266 Returns FALSE if no text is marked.
2269 gboolean
2270 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2272 long end_mark_curs;
2274 if (edit->mark1 == edit->mark2)
2276 *start_mark = *end_mark = 0;
2277 edit->column2 = edit->column1 = 0;
2278 return FALSE;
2281 if (edit->end_mark_curs < 0)
2282 end_mark_curs = edit->buffer.curs1;
2283 else
2284 end_mark_curs = edit->end_mark_curs;
2286 if (edit->mark2 >= 0)
2288 *start_mark = MIN (edit->mark1, edit->mark2);
2289 *end_mark = MAX (edit->mark1, edit->mark2);
2291 else
2293 *start_mark = MIN (edit->mark1, end_mark_curs);
2294 *end_mark = MAX (edit->mark1, end_mark_curs);
2295 edit->column2 = edit->curs_col + edit->over_col;
2298 if (edit->column_highlight
2299 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2300 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2302 off_t start_bol, start_eol;
2303 off_t end_bol, end_eol;
2304 long col1, col2;
2305 off_t diff1, diff2;
2307 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2308 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2309 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2310 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2311 col1 = MIN (edit->column1, edit->column2);
2312 col2 = MAX (edit->column1, edit->column2);
2314 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2315 edit_move_forward3 (edit, start_bol, col1, 0);
2316 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2317 edit_move_forward3 (edit, end_bol, col1, 0);
2319 *start_mark -= diff1;
2320 *end_mark += diff2;
2321 *start_mark = MAX (*start_mark, start_eol);
2322 *end_mark = MIN (*end_mark, end_eol);
2324 return TRUE;
2327 /* --------------------------------------------------------------------------------------------- */
2329 void
2330 edit_block_copy_cmd (WEdit * edit)
2332 off_t start_mark, end_mark, current = edit->buffer.curs1;
2333 off_t mark1 = 0, mark2 = 0;
2334 long c1 = 0, c2 = 0;
2335 off_t size;
2336 unsigned char *copy_buf;
2338 edit_update_curs_col (edit);
2339 if (!eval_marks (edit, &start_mark, &end_mark))
2340 return;
2342 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2344 /* all that gets pushed are deletes hence little space is used on the stack */
2346 edit_push_markers (edit);
2348 if (edit->column_highlight)
2350 long col_delta;
2352 col_delta = labs (edit->column2 - edit->column1);
2353 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2355 else
2357 int size_orig = size;
2359 while (size-- != 0)
2360 edit_insert_ahead (edit, copy_buf[size]);
2362 /* Place cursor at the end of text selection */
2363 if (option_cursor_after_inserted_block)
2364 edit_cursor_move (edit, size_orig);
2367 g_free (copy_buf);
2368 edit_scroll_screen_over_cursor (edit);
2370 if (edit->column_highlight)
2371 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2372 else if (start_mark < current && end_mark > current)
2373 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2375 edit->force |= REDRAW_PAGE;
2379 /* --------------------------------------------------------------------------------------------- */
2381 void
2382 edit_block_move_cmd (WEdit * edit)
2384 off_t current;
2385 unsigned char *copy_buf = NULL;
2386 off_t start_mark, end_mark;
2388 if (!eval_marks (edit, &start_mark, &end_mark))
2389 return;
2391 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2392 return;
2394 if (edit->mark2 < 0)
2395 edit_mark_cmd (edit, FALSE);
2396 edit_push_markers (edit);
2398 if (edit->column_highlight)
2400 off_t mark1, mark2;
2401 off_t size;
2402 long c1, c2, b_width;
2403 long x, x2;
2405 c1 = MIN (edit->column1, edit->column2);
2406 c2 = MAX (edit->column1, edit->column2);
2407 b_width = c2 - c1;
2409 edit_update_curs_col (edit);
2411 x = edit->curs_col;
2412 x2 = x + edit->over_col;
2414 /* do nothing when cursor inside first line of selected area */
2415 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2416 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2417 return;
2419 if (edit->buffer.curs1 > start_mark
2420 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2422 if (x > c2)
2423 x -= b_width;
2424 else if (x > c1 && x <= c2)
2425 x = c1;
2427 /* save current selection into buffer */
2428 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2430 /* remove current selection */
2431 edit_block_delete_cmd (edit);
2433 edit->over_col = MAX (0, edit->over_col - b_width);
2434 /* calculate the cursor pos after delete block */
2435 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2436 edit_cursor_move (edit, current - edit->buffer.curs1);
2437 edit_scroll_screen_over_cursor (edit);
2439 /* add TWS if need before block insertion */
2440 if (option_cursor_beyond_eol && edit->over_col > 0)
2441 edit_insert_over (edit);
2443 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2444 edit_set_markers (edit, mark1, mark2, c1, c2);
2446 else
2448 off_t count, count_orig;
2450 current = edit->buffer.curs1;
2451 copy_buf = g_malloc0 (end_mark - start_mark);
2452 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2453 edit_scroll_screen_over_cursor (edit);
2455 for (count = start_mark; count < end_mark; count++)
2456 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2458 edit_scroll_screen_over_cursor (edit);
2459 edit_cursor_move (edit,
2460 current - edit->buffer.curs1 -
2461 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2462 edit_scroll_screen_over_cursor (edit);
2463 count_orig = count;
2464 while (count-- > start_mark)
2465 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2467 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2470 /* Place cursor at the end of text selection */
2471 if (option_cursor_after_inserted_block)
2472 edit_cursor_move (edit, count_orig - start_mark);
2475 edit_scroll_screen_over_cursor (edit);
2476 g_free (copy_buf);
2477 edit->force |= REDRAW_PAGE;
2480 /* --------------------------------------------------------------------------------------------- */
2481 /** returns 1 if canceelled by user */
2484 edit_block_delete_cmd (WEdit * edit)
2486 off_t start_mark, end_mark;
2488 if (eval_marks (edit, &start_mark, &end_mark))
2489 return edit_block_delete (edit);
2491 edit_delete_line (edit);
2492 return 0;
2495 /* --------------------------------------------------------------------------------------------- */
2496 /** call with edit = 0 before shutdown to close memory leaks */
2498 void
2499 edit_replace_cmd (WEdit * edit, gboolean again)
2501 /* 1 = search string, 2 = replace with */
2502 static char *saved1 = NULL; /* saved default[123] */
2503 static char *saved2 = NULL;
2504 char *input1 = NULL; /* user input from the dialog */
2505 char *input2 = NULL;
2506 GString *input2_str = NULL;
2507 char *disp1 = NULL;
2508 char *disp2 = NULL;
2509 long times_replaced = 0;
2510 gboolean once_found = FALSE;
2511 edit_search_status_msg_t esm;
2513 if (edit == NULL)
2515 MC_PTR_FREE (saved1);
2516 MC_PTR_FREE (saved2);
2517 return;
2520 edit->force |= REDRAW_COMPLETELY;
2522 if (again && !saved1 && !saved2)
2523 again = FALSE;
2525 if (again)
2527 input1 = g_strdup (saved1 ? saved1 : "");
2528 input2 = g_strdup (saved2 ? saved2 : "");
2530 else
2532 char *tmp_inp1, *tmp_inp2;
2534 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : "");
2535 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : "");
2537 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2539 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2541 g_free (disp1);
2542 g_free (disp2);
2544 if (input1 == NULL || *input1 == '\0')
2546 edit->force = REDRAW_COMPLETELY;
2547 goto cleanup;
2550 tmp_inp1 = input1;
2551 tmp_inp2 = input2;
2552 input1 = edit_replace_cmd__conv_to_input (input1);
2553 input2 = edit_replace_cmd__conv_to_input (input2);
2554 g_free (tmp_inp1);
2555 g_free (tmp_inp2);
2557 g_free (saved1);
2558 saved1 = g_strdup (input1);
2559 g_free (saved2);
2560 saved2 = g_strdup (input2);
2562 mc_search_free (edit->search);
2563 edit->search = NULL;
2566 input2_str = g_string_new (input2);
2568 if (edit->search == NULL)
2570 #ifdef HAVE_CHARSET
2571 edit->search = mc_search_new (input1, cp_source);
2572 #else
2573 edit->search = mc_search_new (input1, NULL);
2574 #endif
2575 if (edit->search == NULL)
2577 edit->search_start = edit->buffer.curs1;
2578 goto cleanup;
2580 edit->search->search_type = edit_search_options.type;
2581 #ifdef HAVE_CHARSET
2582 edit->search->is_all_charsets = edit_search_options.all_codepages;
2583 #endif
2584 edit->search->is_case_sensitive = edit_search_options.case_sens;
2585 edit->search->whole_words = edit_search_options.whole_words;
2586 edit->search->search_fn = edit_search_cmd_callback;
2587 edit->search->update_fn = edit_search_update_callback;
2588 edit->search_line_type = edit_get_search_line_type (edit->search);
2589 edit_search_fix_search_start_if_selection (edit);
2592 if (edit->found_len && edit->search_start == edit->found_start + 1
2593 && edit_search_options.backwards)
2594 edit->search_start--;
2596 if (edit->found_len && edit->search_start == edit->found_start - 1
2597 && !edit_search_options.backwards)
2598 edit->search_start++;
2600 esm.first = TRUE;
2601 esm.edit = edit;
2602 esm.offset = edit->search_start;
2604 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2605 edit_search_status_update_cb, NULL);
2609 gsize len = 0;
2611 if (!editcmd_find (&esm, &len))
2613 if (!(edit->search->error == MC_SEARCH_E_OK ||
2614 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2615 edit_show_search_error (edit, _("Search"));
2617 break;
2619 once_found = TRUE;
2621 edit->search_start = edit->search->normal_offset;
2622 /*returns negative on not found or error in pattern */
2624 if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size))
2626 gsize i;
2627 GString *repl_str;
2629 edit->found_start = edit->search_start;
2630 i = edit->found_len = len;
2632 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
2633 edit_scroll_screen_over_cursor (edit);
2635 if (edit->replace_mode == 0)
2637 long l;
2638 int prompt;
2640 l = edit->curs_row - WIDGET (edit)->lines / 3;
2641 if (l > 0)
2642 edit_scroll_downward (edit, l);
2643 if (l < 0)
2644 edit_scroll_upward (edit, -l);
2646 edit_scroll_screen_over_cursor (edit);
2647 edit->force |= REDRAW_PAGE;
2648 edit_render_keypress (edit);
2650 /*so that undo stops at each query */
2651 edit_push_key_press (edit);
2652 /* and prompt 2/3 down */
2653 disp1 = edit_replace_cmd__conv_to_display (saved1);
2654 disp2 = edit_replace_cmd__conv_to_display (saved2);
2655 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2656 g_free (disp1);
2657 g_free (disp2);
2659 if (prompt == B_REPLACE_ALL)
2660 edit->replace_mode = 1;
2661 else if (prompt == B_SKIP_REPLACE)
2663 if (edit_search_options.backwards)
2664 edit->search_start--;
2665 else
2666 edit->search_start++;
2667 continue; /* loop */
2669 else if (prompt == B_CANCEL)
2671 edit->replace_mode = -1;
2672 break; /* loop */
2676 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2678 if (edit->search->error != MC_SEARCH_E_OK)
2680 edit_show_search_error (edit, _("Replace"));
2681 g_string_free (repl_str, TRUE);
2682 break;
2685 /* delete then insert new */
2686 for (i = 0; i < len; i++)
2687 edit_delete (edit, TRUE);
2689 for (i = 0; i < repl_str->len; i++)
2690 edit_insert (edit, repl_str->str[i]);
2692 edit->found_len = repl_str->len;
2693 g_string_free (repl_str, TRUE);
2694 times_replaced++;
2696 /* so that we don't find the same string again */
2697 if (edit_search_options.backwards)
2699 edit->search_start--;
2701 else
2703 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2705 if (edit->search_start >= edit->buffer.size)
2706 break;
2709 edit_scroll_screen_over_cursor (edit);
2711 else
2713 /* try and find from right here for next search */
2714 edit->search_start = edit->buffer.curs1;
2715 edit_update_curs_col (edit);
2717 edit->force |= REDRAW_PAGE;
2718 edit_render_keypress (edit);
2720 if (times_replaced == 0)
2721 query_dialog (_("Replace"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&OK"));
2722 break;
2725 while (edit->replace_mode >= 0);
2727 status_msg_deinit (STATUS_MSG (&esm));
2728 edit_scroll_screen_over_cursor (edit);
2729 edit->force |= REDRAW_COMPLETELY;
2730 edit_render_keypress (edit);
2732 if ((edit->replace_mode == 1) && (times_replaced != 0))
2733 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2735 cleanup:
2736 g_free (input1);
2737 g_free (input2);
2738 if (input2_str != NULL)
2739 g_string_free (input2_str, TRUE);
2742 /* --------------------------------------------------------------------------------------------- */
2744 mc_search_cbret_t
2745 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2747 WEdit *edit = ((const edit_search_status_msg_t *) user_data)->edit;
2749 *current_char = edit_buffer_get_byte (&edit->buffer, (off_t) char_offset);
2750 return MC_SEARCH_CB_OK;
2753 /* --------------------------------------------------------------------------------------------- */
2755 mc_search_cbret_t
2756 edit_search_update_callback (const void *user_data, gsize char_offset)
2758 status_msg_t *sm = STATUS_MSG (user_data);
2760 ((edit_search_status_msg_t *) sm)->offset = (off_t) char_offset;
2762 return (sm->update (sm) == B_CANCEL ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK);
2765 /* --------------------------------------------------------------------------------------------- */
2767 void
2768 edit_search_cmd (WEdit * edit, gboolean again)
2771 if (edit == NULL)
2772 return;
2774 if (!again)
2775 edit_search (edit);
2776 else if (edit->last_search_string != NULL)
2777 edit_do_search (edit);
2778 else
2780 /* find last search string in history */
2781 GList *history;
2783 history = history_get (MC_HISTORY_SHARED_SEARCH);
2784 if (history != NULL && history->data != NULL)
2786 edit->last_search_string = (char *) history->data;
2787 history->data = NULL;
2788 history = g_list_first (history);
2789 g_list_free_full (history, g_free);
2791 #ifdef HAVE_CHARSET
2792 edit->search = mc_search_new (edit->last_search_string, cp_source);
2793 #else
2794 edit->search = mc_search_new (edit->last_search_string, NULL);
2795 #endif
2796 if (edit->search == NULL)
2798 /* if not... then ask for an expression */
2799 MC_PTR_FREE (edit->last_search_string);
2800 edit_search (edit);
2802 else
2804 edit->search->search_type = edit_search_options.type;
2805 #ifdef HAVE_CHARSET
2806 edit->search->is_all_charsets = edit_search_options.all_codepages;
2807 #endif
2808 edit->search->is_case_sensitive = edit_search_options.case_sens;
2809 edit->search->whole_words = edit_search_options.whole_words;
2810 edit->search->search_fn = edit_search_cmd_callback;
2811 edit->search->update_fn = edit_search_update_callback;
2812 edit->search_line_type = edit_get_search_line_type (edit->search);
2813 edit_do_search (edit);
2816 else
2818 /* if not... then ask for an expression */
2819 MC_PTR_FREE (edit->last_search_string);
2820 edit_search (edit);
2826 /* --------------------------------------------------------------------------------------------- */
2828 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2830 * @return TRUE if it's OK to exit, FALSE to continue editing.
2833 gboolean
2834 edit_ok_to_exit (WEdit * edit)
2836 const char *fname = N_("[NoName]");
2837 char *msg;
2838 int act;
2840 if (!edit->modified)
2841 return TRUE;
2843 if (edit->filename_vpath != NULL)
2844 fname = vfs_path_as_str (edit->filename_vpath);
2845 #ifdef ENABLE_NLS
2846 else
2847 fname = _(fname);
2848 #endif
2850 if (!mc_global.midnight_shutdown)
2852 query_set_sel (2);
2854 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2855 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2857 else
2859 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2860 fname);
2861 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2863 /* Esc is No */
2864 if (act == -1)
2865 act = 1;
2868 g_free (msg);
2870 switch (act)
2872 case 0: /* Yes */
2873 if (!mc_global.midnight_shutdown && !edit_check_newline (&edit->buffer))
2874 return FALSE;
2875 edit_push_markers (edit);
2876 edit_set_markers (edit, 0, 0, 0, 0);
2877 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2878 return mc_global.midnight_shutdown;
2879 break;
2880 case 1: /* No */
2881 default:
2882 break;
2883 case 2: /* Cancel quit */
2884 case -1: /* Esc */
2885 return FALSE;
2888 return TRUE;
2891 /* --------------------------------------------------------------------------------------------- */
2892 /** save block, returns TRUE on success */
2894 gboolean
2895 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2897 int file;
2898 off_t len = 1;
2899 vfs_path_t *vpath;
2901 vpath = vfs_path_from_str (filename);
2902 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2903 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2904 vfs_path_free (vpath);
2905 if (file == -1)
2906 return FALSE;
2908 if (edit->column_highlight)
2910 int r;
2912 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2913 if (r > 0)
2915 unsigned char *block, *p;
2917 p = block = edit_get_block (edit, start, finish, &len);
2918 while (len)
2920 r = mc_write (file, p, len);
2921 if (r < 0)
2922 break;
2923 p += r;
2924 len -= r;
2926 g_free (block);
2929 else
2931 unsigned char *buf;
2932 off_t i = start;
2933 off_t end;
2935 len = finish - start;
2936 buf = g_malloc0 (TEMP_BUF_LEN);
2937 while (start != finish)
2939 end = MIN (finish, start + TEMP_BUF_LEN);
2940 for (; i < end; i++)
2941 buf[i - start] = edit_buffer_get_byte (&edit->buffer, i);
2942 len -= mc_write (file, (char *) buf, end - start);
2943 start = end;
2945 g_free (buf);
2947 mc_close (file);
2949 return (len == 0);
2952 /* --------------------------------------------------------------------------------------------- */
2954 void
2955 edit_paste_from_history (WEdit * edit)
2957 (void) edit;
2958 edit_error_dialog (_("Error"), _("This function is not implemented"));
2961 /* --------------------------------------------------------------------------------------------- */
2963 gboolean
2964 edit_copy_to_X_buf_cmd (WEdit * edit)
2966 off_t start_mark, end_mark;
2968 if (!eval_marks (edit, &start_mark, &end_mark))
2969 return TRUE;
2971 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2973 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2974 return FALSE;
2976 /* try use external clipboard utility */
2977 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2979 if (option_drop_selection_on_copy)
2980 edit_mark_cmd (edit, TRUE);
2982 return TRUE;
2985 /* --------------------------------------------------------------------------------------------- */
2987 gboolean
2988 edit_cut_to_X_buf_cmd (WEdit * edit)
2990 off_t start_mark, end_mark;
2992 if (!eval_marks (edit, &start_mark, &end_mark))
2993 return TRUE;
2995 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2997 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
2998 return FALSE;
3000 /* try use external clipboard utility */
3001 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3003 edit_block_delete_cmd (edit);
3004 edit_mark_cmd (edit, TRUE);
3006 return TRUE;
3009 /* --------------------------------------------------------------------------------------------- */
3011 gboolean
3012 edit_paste_from_X_buf_cmd (WEdit * edit)
3014 vfs_path_t *tmp;
3015 gboolean ret;
3017 /* try use external clipboard utility */
3018 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3019 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3020 ret = (edit_insert_file (edit, tmp) >= 0);
3021 vfs_path_free (tmp);
3023 return ret;
3026 /* --------------------------------------------------------------------------------------------- */
3028 * Ask user for the line and go to that line.
3029 * Negative numbers mean line from the end (i.e. -1 is the last line).
3032 void
3033 edit_goto_cmd (WEdit * edit)
3035 static gboolean first_run = TRUE;
3037 char *f;
3038 long l;
3039 char *error;
3041 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE,
3042 first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3043 if (f == NULL || *f == '\0')
3045 g_free (f);
3046 return;
3049 l = strtol (f, &error, 0);
3050 if (*error != '\0')
3052 g_free (f);
3053 return;
3056 if (l < 0)
3057 l = edit->buffer.lines + l + 2;
3058 edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1);
3059 edit_move_to_line (edit, l - 1);
3060 edit->force |= REDRAW_COMPLETELY;
3061 g_free (f);
3063 first_run = FALSE;
3067 /* --------------------------------------------------------------------------------------------- */
3068 /** Return TRUE on success */
3070 gboolean
3071 edit_save_block_cmd (WEdit * edit)
3073 off_t start_mark, end_mark;
3074 char *exp, *tmp;
3075 gboolean ret = FALSE;
3077 if (!eval_marks (edit, &start_mark, &end_mark))
3078 return TRUE;
3080 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3081 exp =
3082 input_expand_dialog (_("Save block"), _("Enter file name:"),
3083 MC_HISTORY_EDIT_SAVE_BLOCK, tmp, INPUT_COMPLETE_FILENAMES);
3084 g_free (tmp);
3085 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3087 if (exp != NULL && *exp != '\0')
3089 if (edit_save_block (edit, exp, start_mark, end_mark))
3090 ret = TRUE;
3091 else
3092 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3094 edit->force |= REDRAW_COMPLETELY;
3097 g_free (exp);
3099 return ret;
3103 /* --------------------------------------------------------------------------------------------- */
3105 /** returns TRUE on success */
3106 gboolean
3107 edit_insert_file_cmd (WEdit * edit)
3109 char *tmp;
3110 char *exp;
3111 gboolean ret = FALSE;
3113 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3114 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3115 MC_HISTORY_EDIT_INSERT_FILE, tmp, INPUT_COMPLETE_FILENAMES);
3116 g_free (tmp);
3118 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3120 if (exp != NULL && *exp != '\0')
3122 vfs_path_t *exp_vpath;
3124 exp_vpath = vfs_path_from_str (exp);
3125 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3126 vfs_path_free (exp_vpath);
3128 if (!ret)
3129 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3132 g_free (exp);
3134 edit->force |= REDRAW_COMPLETELY;
3135 return ret;
3138 /* --------------------------------------------------------------------------------------------- */
3139 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3142 edit_sort_cmd (WEdit * edit)
3144 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3145 off_t start_mark, end_mark;
3146 int e;
3148 if (!eval_marks (edit, &start_mark, &end_mark))
3150 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3151 return 0;
3154 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3155 edit_save_block (edit, tmp, start_mark, end_mark);
3156 g_free (tmp);
3158 exp = input_dialog (_("Run sort"),
3159 _("Enter sort options (see manpage) separated by whitespace:"),
3160 MC_HISTORY_EDIT_SORT, INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
3162 if (exp == NULL)
3163 return 1;
3165 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3166 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3167 tmp =
3168 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3169 " > ", tmp_edit_temp_name, (char *) NULL);
3170 g_free (tmp_edit_temp_name);
3171 g_free (tmp_edit_block_name);
3172 g_free (exp);
3174 e = system (tmp);
3175 g_free (tmp);
3176 if (e != 0)
3178 if (e == -1 || e == 127)
3179 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3180 else
3182 char q[8];
3184 sprintf (q, "%d ", e);
3185 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3186 edit_error_dialog (_("Sort"), tmp);
3187 g_free (tmp);
3189 return -1;
3192 edit->force |= REDRAW_COMPLETELY;
3194 if (edit_block_delete_cmd (edit))
3195 return 1;
3198 vfs_path_t *tmp_vpath;
3200 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3201 edit_insert_file (edit, tmp_vpath);
3202 vfs_path_free (tmp_vpath);
3204 return 0;
3207 /* --------------------------------------------------------------------------------------------- */
3209 * Ask user for a command, execute it and paste its output back to the
3210 * editor.
3214 edit_ext_cmd (WEdit * edit)
3216 char *exp, *tmp, *tmp_edit_temp_file;
3217 int e;
3219 exp =
3220 input_dialog (_("Paste output of external command"),
3221 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3222 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3223 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3224 INPUT_COMPLETE_SHELL_ESC);
3226 if (!exp)
3227 return 1;
3229 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3230 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3231 g_free (tmp_edit_temp_file);
3232 e = system (tmp);
3233 g_free (tmp);
3234 g_free (exp);
3236 if (e)
3238 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3239 return -1;
3242 edit->force |= REDRAW_COMPLETELY;
3245 vfs_path_t *tmp_vpath;
3247 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3248 edit_insert_file (edit, tmp_vpath);
3249 vfs_path_free (tmp_vpath);
3251 return 0;
3254 /* --------------------------------------------------------------------------------------------- */
3255 /** if block is 1, a block must be highlighted and the shell command
3256 processes it. If block is 0 the shell command is a straight system
3257 command, that just produces some output which is to be inserted */
3259 void
3260 edit_block_process_cmd (WEdit * edit, int macro_number)
3262 char *fname;
3263 char *macros_fname = NULL;
3265 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3266 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3267 user_menu (edit, macros_fname, 0);
3268 g_free (fname);
3269 g_free (macros_fname);
3270 edit->force |= REDRAW_COMPLETELY;
3273 /* --------------------------------------------------------------------------------------------- */
3275 void
3276 edit_mail_dialog (WEdit * edit)
3278 char *mail_to, *mail_subject, *mail_cc;
3280 quick_widget_t quick_widgets[] = {
3281 /* *INDENT-OFF* */
3282 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3283 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3284 INPUT_LAST_TEXT, "mail-dlg-input-3",
3285 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3286 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3287 INPUT_LAST_TEXT, "mail-dlg-input-2",
3288 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3289 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3290 INPUT_LAST_TEXT, "mail-dlg-input",
3291 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3292 QUICK_BUTTONS_OK_CANCEL,
3293 QUICK_END
3294 /* *INDENT-ON* */
3297 quick_dialog_t qdlg = {
3298 -1, -1, 50,
3299 N_("Mail"), "[Input Line Keys]",
3300 quick_widgets, NULL, NULL
3303 if (quick_dialog (&qdlg) != B_CANCEL)
3305 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3306 g_free (mail_to);
3307 g_free (mail_subject);
3308 g_free (mail_cc);
3312 /* --------------------------------------------------------------------------------------------- */
3314 /*******************/
3315 /* Word Completion */
3316 /*******************/
3319 * Complete current word using regular expression search
3320 * backwards beginning at the current cursor position.
3323 void
3324 edit_complete_word_cmd (WEdit * edit)
3326 gsize i, max_len, word_len = 0, num_compl = 0;
3327 off_t word_start = 0;
3328 GString *match_expr;
3329 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3331 /* search start of word to be completed */
3332 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3333 return;
3335 /* prepare match expression */
3336 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3337 match_expr = g_string_new ("(^|\\s+|\\b)");
3338 for (i = 0; i < word_len; i++)
3339 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3340 g_string_append (match_expr,
3341 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3343 /* collect the possible completions */
3344 /* start search from begin to end of file */
3345 max_len =
3346 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3347 &num_compl);
3349 if (num_compl > 0)
3351 /* insert completed word if there is only one match */
3352 if (num_compl == 1)
3353 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3354 /* more than one possible completion => ask the user */
3355 else
3357 char *curr_compl;
3359 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3360 /* !!! pressed again the selection dialog pops up, but that !!! */
3361 /* !!! seems to require a further internal state !!! */
3362 /*tty_beep (); */
3364 /* let the user select the preferred completion */
3365 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3366 (GString **) & compl, num_compl);
3368 if (curr_compl != NULL)
3370 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3371 g_free (curr_compl);
3376 g_string_free (match_expr, TRUE);
3377 /* release memory before return */
3378 for (i = 0; i < num_compl; i++)
3379 g_string_free (compl[i], TRUE);
3382 /* --------------------------------------------------------------------------------------------- */
3384 #ifdef HAVE_CHARSET
3385 void
3386 edit_select_codepage_cmd (WEdit * edit)
3388 if (do_select_codepage ())
3389 edit_set_codeset (edit);
3391 edit->force = REDRAW_PAGE;
3392 widget_redraw (WIDGET (edit));
3394 #endif
3396 /* --------------------------------------------------------------------------------------------- */
3398 void
3399 edit_insert_literal_cmd (WEdit * edit)
3401 int char_for_insertion;
3403 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3404 _("Press any key:"), FALSE);
3405 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3408 /* --------------------------------------------------------------------------------------------- */
3410 void
3411 edit_begin_end_macro_cmd (WEdit * edit)
3413 /* edit is a pointer to the widget */
3414 if (edit != NULL)
3416 long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3417 edit_execute_key_command (edit, command, -1);
3421 /* --------------------------------------------------------------------------------------------- */
3423 void
3424 edit_begin_end_repeat_cmd (WEdit * edit)
3426 /* edit is a pointer to the widget */
3427 if (edit != NULL)
3429 long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3430 edit_execute_key_command (edit, command, -1);
3434 /* --------------------------------------------------------------------------------------------- */
3436 gboolean
3437 edit_load_forward_cmd (WEdit * edit)
3439 if (edit->modified
3440 && edit_query_dialog2 (_("Warning"),
3441 _("Current text was modified without a file save.\n"
3442 "Continue discards these changes"), _("C&ontinue"),
3443 _("&Cancel")) == 1)
3445 edit->force |= REDRAW_COMPLETELY;
3446 return TRUE;
3449 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3450 return FALSE;
3452 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3453 return FALSE;
3455 edit_stack_iterator++;
3456 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3457 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3458 edit_history_moveto[edit_stack_iterator].line);
3460 return FALSE;
3463 /* --------------------------------------------------------------------------------------------- */
3465 gboolean
3466 edit_load_back_cmd (WEdit * edit)
3468 if (edit->modified
3469 && edit_query_dialog2 (_("Warning"),
3470 _("Current text was modified without a file save.\n"
3471 "Continue discards these changes"), _("C&ontinue"),
3472 _("&Cancel")) == 1)
3474 edit->force |= REDRAW_COMPLETELY;
3475 return TRUE;
3478 /* we are in the bottom of the stack, NO WAY! */
3479 if (edit_stack_iterator == 0)
3480 return FALSE;
3482 edit_stack_iterator--;
3483 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3484 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3485 edit_history_moveto[edit_stack_iterator].line);
3487 return FALSE;
3490 /* --------------------------------------------------------------------------------------------- */
3492 void
3493 edit_get_match_keyword_cmd (WEdit * edit)
3495 gsize word_len = 0, max_len = 0;
3496 int num_def = 0;
3497 gsize i;
3498 off_t word_start = 0;
3499 GString *match_expr;
3500 char *path = NULL;
3501 char *ptr = NULL;
3502 char *tagfile = NULL;
3504 etags_hash_t def_hash[MAX_DEFINITIONS];
3506 for (i = 0; i < MAX_DEFINITIONS; i++)
3508 def_hash[i].filename = NULL;
3511 /* search start of word to be completed */
3512 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3513 return;
3515 /* prepare match expression */
3516 match_expr = g_string_sized_new (word_len);
3517 for (i = 0; i < word_len; i++)
3518 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3520 ptr = g_get_current_dir ();
3521 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3522 g_free (ptr);
3524 /* Recursive search file 'TAGS' in parent dirs */
3527 ptr = g_path_get_dirname (path);
3528 g_free (path);
3529 path = ptr;
3530 g_free (tagfile);
3531 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3532 if (exist_file (tagfile))
3533 break;
3535 while (strcmp (path, PATH_SEP_STR) != 0);
3537 if (tagfile)
3539 num_def =
3540 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3541 g_free (tagfile);
3543 g_free (path);
3545 max_len = MAX_WIDTH_DEF_DIALOG;
3546 word_len = 0;
3547 if (num_def > 0)
3549 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3550 (etags_hash_t *) & def_hash, num_def);
3552 g_string_free (match_expr, TRUE);
3555 /* --------------------------------------------------------------------------------------------- */
3557 #ifdef HAVE_ASPELL
3559 edit_suggest_current_word (WEdit * edit)
3561 gsize cut_len = 0;
3562 gsize word_len = 0;
3563 off_t word_start = 0;
3564 int retval = B_SKIP_WORD;
3565 GString *match_word;
3567 /* search start of word to spell check */
3568 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3569 &cut_len);
3570 word_len = match_word->len;
3572 #ifdef HAVE_CHARSET
3573 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3575 GString *tmp_word;
3577 tmp_word = str_convert_to_display (match_word->str);
3578 g_string_free (match_word, TRUE);
3579 match_word = tmp_word;
3581 #endif
3582 if (!aspell_check (match_word->str, (int) word_len))
3584 GArray *suggest;
3585 unsigned int res;
3586 guint i;
3588 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3590 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3591 if (res != 0)
3593 char *new_word = NULL;
3595 edit->found_start = word_start;
3596 edit->found_len = word_len;
3597 edit->force |= REDRAW_PAGE;
3598 edit_scroll_screen_over_cursor (edit);
3599 edit_render_keypress (edit);
3601 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3602 edit_cursor_move (edit, word_len - cut_len);
3604 if (retval == B_ENTER && new_word != NULL)
3606 char *cp_word;
3608 #ifdef HAVE_CHARSET
3609 if (mc_global.source_codepage >= 0 &&
3610 (mc_global.source_codepage != mc_global.display_codepage))
3612 GString *tmp_word;
3614 tmp_word = str_convert_to_input (new_word);
3615 g_free (new_word);
3616 new_word = g_string_free (tmp_word, FALSE);
3618 #endif
3619 cp_word = new_word;
3620 for (i = 0; i < word_len; i++)
3621 edit_backspace (edit, TRUE);
3622 for (; *new_word; new_word++)
3623 edit_insert (edit, *new_word);
3624 g_free (cp_word);
3626 else if (retval == B_ADD_WORD && match_word != NULL)
3627 aspell_add_to_dict (match_word->str, (int) word_len);
3631 for (i = 0; i < suggest->len; i++)
3633 char *cur_sugg_word;
3635 cur_sugg_word = g_array_index (suggest, char *, i);
3636 g_free (cur_sugg_word);
3638 g_array_free (suggest, TRUE);
3639 edit->found_start = 0;
3640 edit->found_len = 0;
3642 g_string_free (match_word, TRUE);
3643 return retval;
3646 /* --------------------------------------------------------------------------------------------- */
3648 void
3649 edit_spellcheck_file (WEdit * edit)
3651 if (edit->buffer.curs_line > 0)
3653 edit_cursor_move (edit, -edit->buffer.curs1);
3654 edit_move_to_prev_col (edit, 0);
3655 edit_update_curs_row (edit);
3660 int c1, c2;
3662 c2 = edit_buffer_get_current_byte (&edit->buffer);
3666 if (edit->buffer.curs1 >= edit->buffer.size)
3667 return;
3669 c1 = c2;
3670 edit_cursor_move (edit, 1);
3671 c2 = edit_buffer_get_current_byte (&edit->buffer);
3673 while (is_break_char (c1) || is_break_char (c2));
3675 while (edit_suggest_current_word (edit) != B_CANCEL);
3678 /* --------------------------------------------------------------------------------------------- */
3680 void
3681 edit_set_spell_lang (void)
3683 GArray *lang_list;
3685 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3686 if (aspell_get_lang_list (lang_list) != 0)
3688 char *lang;
3690 lang = spell_dialog_lang_list_show (lang_list);
3691 if (lang != NULL)
3693 (void) aspell_set_lang (lang);
3694 g_free (lang);
3697 aspell_array_clean (lang_list);
3699 #endif /* HAVE_ASPELL */
3701 /* --------------------------------------------------------------------------------------------- */