Ticket #3522: (edit_suggest_current_word): fix memory leak.
[midnight-commander.git] / src / editor / editcmd.c
blob7970a9be0e2e7cffdb9a608e6a333dc7ee015587
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996-2015
5 Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
9 Andrew Borodin <aborodin@vmail.ru>, 2012-2014
10 Ilia Maslakov <il.smind@gmail.com>, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor high level editing commands
30 * \author Paul Sheer
31 * \date 1996, 1997
34 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
36 #include <config.h>
38 #ifdef HAVE_ASSERT_H
39 #include <assert.h>
40 #endif
41 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 #include <stdlib.h>
52 #include "lib/global.h"
53 #include "lib/tty/tty.h"
54 #include "lib/tty/key.h" /* XCTRL */
55 #include "lib/mcconfig.h"
56 #include "lib/skin.h"
57 #include "lib/strutil.h" /* utf string functions */
58 #include "lib/lock.h"
59 #include "lib/util.h" /* tilde_expand() */
60 #include "lib/vfs/vfs.h"
61 #include "lib/widget.h"
62 #include "lib/event.h" /* mc_event_raise() */
63 #ifdef HAVE_CHARSET
64 #include "lib/charsets.h"
65 #endif
67 #include "src/history.h"
68 #include "src/setup.h" /* option_tab_spacing */
69 #ifdef HAVE_CHARSET
70 #include "src/selcodepage.h"
71 #endif
72 #include "src/keybind-defaults.h"
73 #include "src/util.h" /* check_for_default() */
75 #include "edit-impl.h"
76 #include "editwidget.h"
77 #include "editcmd_dialogs.h"
78 #ifdef HAVE_ASPELL
79 #include "spell.h"
80 #include "spell_dialogs.h"
81 #endif
82 #include "etags.h"
84 /*** global variables ****************************************************************************/
86 /* search and replace: */
87 int search_create_bookmark = FALSE;
89 /* queries on a save */
90 int edit_confirm_save = 1;
92 /* whether we need to drop selection on copy to buffer */
93 int option_drop_selection_on_copy = 1;
95 /*** file scope macro definitions ****************************************************************/
97 #define space_width 1
99 #define TEMP_BUF_LEN 1024
101 #define INPUT_INDEX 9
103 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
104 (and the above) routines to work properly - paul */
106 #define is_digit(x) ((x) >= '0' && (x) <= '9')
108 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
110 /*** file scope type declarations ****************************************************************/
112 typedef struct
114 simple_status_msg_t status_msg; /* base class */
116 gboolean first;
117 WEdit *edit;
118 off_t offset;
119 } edit_search_status_msg_t;
121 /*** file scope variables ************************************************************************/
123 static unsigned long edit_save_mode_radio_id, edit_save_mode_input_id;
125 /* --------------------------------------------------------------------------------------------- */
126 /*** file scope functions ************************************************************************/
127 /* --------------------------------------------------------------------------------------------- */
129 static int
130 edit_search_status_update_cb (status_msg_t * sm)
132 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
133 edit_search_status_msg_t *esm = (edit_search_status_msg_t *) sm;
134 Widget *wd = WIDGET (sm->dlg);
136 if (verbose)
137 label_set_textv (ssm->label, _("Searching %s: %3d%%"), esm->edit->last_search_string,
138 edit_buffer_calc_percent (&esm->edit->buffer, esm->offset));
139 else
140 label_set_textv (ssm->label, _("Searching %s"), esm->edit->last_search_string);
142 if (esm->first)
144 int wd_width;
145 Widget *lw = WIDGET (ssm->label);
147 wd_width = max (wd->cols, lw->cols + 6);
148 widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
149 widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
150 esm->first = FALSE;
153 return status_msg_common_update (sm);
156 /* --------------------------------------------------------------------------------------------- */
158 static cb_ret_t
159 edit_save_mode_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
161 switch (msg)
163 case MSG_ACTION:
164 if (sender != NULL && sender->id == edit_save_mode_radio_id)
166 Widget *ww;
168 ww = dlg_find_by_id (DIALOG (w), edit_save_mode_input_id);
169 widget_disable (ww, RADIO (sender)->sel != 2);
170 return MSG_HANDLED;
173 return MSG_NOT_HANDLED;
175 default:
176 return dlg_default_callback (w, sender, msg, parm, data);
180 /* --------------------------------------------------------------------------------------------- */
182 /* If 0 (quick save) then a) create/truncate <filename> file,
183 b) save to <filename>;
184 if 1 (safe save) then a) save to <tempnam>,
185 b) rename <tempnam> to <filename>;
186 if 2 (do backups) then a) save to <tempnam>,
187 b) rename <filename> to <filename.backup_ext>,
188 c) rename <tempnam> to <filename>. */
190 /* returns 0 on error, -1 on abort */
192 static int
193 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
195 char *p;
196 gchar *tmp;
197 off_t filelen = 0;
198 int this_save_mode, rv, fd = -1;
199 vfs_path_t *real_filename_vpath;
200 vfs_path_t *savename_vpath = NULL;
201 const char *start_filename;
202 const vfs_path_element_t *vpath_element;
203 struct stat sb;
205 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
206 if (vpath_element == NULL)
207 return 0;
209 start_filename = vpath_element->path;
210 if (*start_filename == '\0')
211 return 0;
213 if (!IS_PATH_SEP (*start_filename) && edit->dir_vpath != NULL)
214 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
215 else
216 real_filename_vpath = vfs_path_clone (filename_vpath);
218 this_save_mode = option_save_mode;
219 if (this_save_mode != EDIT_QUICK_SAVE)
221 if (!vfs_file_is_local (real_filename_vpath)
222 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
225 * The file does not exists yet, so no safe save or
226 * backup are necessary.
228 this_save_mode = EDIT_QUICK_SAVE;
230 if (fd != -1)
231 mc_close (fd);
234 rv = mc_stat (real_filename_vpath, &sb);
235 if (rv == 0)
237 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt && sb.st_nlink > 1)
239 rv = edit_query_dialog3 (_("Warning"),
240 _("File has hard-links. Detach before saving?"),
241 _("&Yes"), _("&No"), _("&Cancel"));
242 switch (rv)
244 case 0:
245 this_save_mode = EDIT_SAFE_SAVE;
246 /* fallthrough */
247 case 1:
248 edit->skip_detach_prompt = 1;
249 break;
250 default:
251 vfs_path_free (real_filename_vpath);
252 return -1;
256 /* Prevent overwriting changes from other editor sessions. */
257 if (edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
259 /* The default action is "Cancel". */
260 query_set_sel (1);
262 rv = edit_query_dialog2 (_("Warning"),
263 _("The file has been modified in the meantime. Save anyway?"),
264 _("&Yes"), _("&Cancel"));
265 if (rv != 0)
267 vfs_path_free (real_filename_vpath);
268 return -1;
273 if (this_save_mode != EDIT_QUICK_SAVE)
275 char *savedir, *saveprefix;
277 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
278 if (savedir == NULL)
279 savedir = g_strdup (".");
281 /* Token-related function never return leading slash, so we need add it manually */
282 saveprefix = mc_build_filename (PATH_SEP_STR, savedir, "cooledit", NULL);
283 g_free (savedir);
284 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
285 g_free (saveprefix);
286 if (savename_vpath == NULL)
288 vfs_path_free (real_filename_vpath);
289 return 0;
291 /* FIXME:
292 * Close for now because mc_mkstemps use pure open system call
293 * to create temporary file and it needs to be reopened by
294 * VFS-aware mc_open().
296 close (fd);
298 else
299 savename_vpath = vfs_path_clone (real_filename_vpath);
301 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
302 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
304 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
305 if (fd == -1)
306 goto error_save;
308 /* pipe save */
309 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
310 if (p != NULL)
312 FILE *file;
314 mc_close (fd);
315 file = (FILE *) popen (p, "w");
317 if (file)
319 filelen = edit_write_stream (edit, file);
320 #if 1
321 pclose (file);
322 #else
323 if (pclose (file) != 0)
325 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
326 edit_error_dialog (_("Error"), tmp);
327 g_free (tmp);
328 g_free (p);
329 goto error_save;
331 #endif
333 else
335 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
336 edit_error_dialog (_("Error"), get_sys_error (tmp));
337 g_free (p);
338 g_free (tmp);
339 goto error_save;
341 g_free (p);
343 else if (edit->lb == LB_ASIS)
344 { /* do not change line breaks */
345 filelen = edit_buffer_write_file (&edit->buffer, fd);
347 if (filelen != edit->buffer.size)
349 mc_close (fd);
350 goto error_save;
352 if (mc_close (fd) != 0)
353 goto error_save;
354 /* Update the file information, especially the mtime. */
355 if (mc_stat (savename_vpath, &edit->stat1) == -1)
356 goto error_save;
358 else
359 { /* change line breaks */
360 FILE *file;
361 const vfs_path_element_t *path_element;
363 mc_close (fd);
365 path_element = vfs_path_get_by_index (savename_vpath, -1);
366 file = (FILE *) fopen (path_element->path, "w");
367 if (file != NULL)
369 filelen = edit_write_stream (edit, file);
370 fclose (file);
372 else
374 char *msg;
376 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
377 edit_error_dialog (_("Error"), msg);
378 g_free (msg);
379 goto error_save;
383 if (filelen != edit->buffer.size)
384 goto error_save;
386 if (this_save_mode == EDIT_DO_BACKUP)
388 char *tmp_store_filename;
389 vfs_path_element_t *last_vpath_element;
390 vfs_path_t *tmp_vpath;
391 gboolean ok;
393 #ifdef HAVE_ASSERT_H
394 assert (option_backup_ext != NULL);
395 #endif
396 /* add backup extension to the path */
397 tmp_vpath = vfs_path_clone (real_filename_vpath);
398 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
399 tmp_store_filename = last_vpath_element->path;
400 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
401 g_free (tmp_store_filename);
403 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
404 vfs_path_free (tmp_vpath);
405 if (!ok)
406 goto error_save;
409 if (this_save_mode != EDIT_QUICK_SAVE)
410 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
411 goto error_save;
413 vfs_path_free (real_filename_vpath);
414 vfs_path_free (savename_vpath);
415 return 1;
416 error_save:
417 /* FIXME: Is this safe ?
418 * if (this_save_mode != EDIT_QUICK_SAVE)
419 * mc_unlink (savename);
421 vfs_path_free (real_filename_vpath);
422 vfs_path_free (savename_vpath);
423 return 0;
426 /* --------------------------------------------------------------------------------------------- */
428 static gboolean
429 edit_check_newline (const edit_buffer_t * buf)
431 return !(option_check_nl_at_eof && buf->size > 0
432 && edit_buffer_get_byte (buf, buf->size - 1) != '\n'
433 && edit_query_dialog2 (_("Warning"),
434 _("The file you are saving does not end with a newline."),
435 _("C&ontinue"), _("&Cancel")) != 0);
438 /* --------------------------------------------------------------------------------------------- */
440 static vfs_path_t *
441 edit_get_save_file_as (WEdit * edit)
443 static LineBreaks cur_lb = LB_ASIS;
444 char *filename_res;
445 vfs_path_t *ret_vpath = NULL;
447 const char *lb_names[LB_NAMES] = {
448 N_("&Do not change"),
449 N_("&Unix format (LF)"),
450 N_("&Windows/DOS format (CR LF)"),
451 N_("&Macintosh format (CR)")
454 quick_widget_t quick_widgets[] = {
455 /* *INDENT-OFF* */
456 QUICK_LABELED_INPUT (N_("Enter file name:"), input_label_above,
457 vfs_path_as_str (edit->filename_vpath), "save-as",
458 &filename_res, NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
459 QUICK_SEPARATOR (TRUE),
460 QUICK_LABEL (N_("Change line breaks to:"), NULL),
461 QUICK_RADIO (LB_NAMES, lb_names, (int *) &cur_lb, NULL),
462 QUICK_BUTTONS_OK_CANCEL,
463 QUICK_END
464 /* *INDENT-ON* */
467 quick_dialog_t qdlg = {
468 -1, -1, 64,
469 N_("Save As"), "[Save File As]",
470 quick_widgets, NULL, NULL
473 if (quick_dialog (&qdlg) != B_CANCEL)
475 char *fname;
477 edit->lb = cur_lb;
478 fname = tilde_expand (filename_res);
479 g_free (filename_res);
480 ret_vpath = vfs_path_from_str (fname);
481 g_free (fname);
484 return ret_vpath;
487 /* --------------------------------------------------------------------------------------------- */
489 /** returns TRUE on success */
491 static gboolean
492 edit_save_cmd (WEdit * edit)
494 int res, save_lock = 0;
496 if (!edit->locked && !edit->delete_file)
497 save_lock = lock_file (edit->filename_vpath);
498 res = edit_save_file (edit, edit->filename_vpath);
500 /* Maintain modify (not save) lock on failure */
501 if ((res > 0 && edit->locked) || save_lock)
502 edit->locked = unlock_file (edit->filename_vpath);
504 /* On failure try 'save as', it does locking on its own */
505 if (res == 0)
506 return edit_save_as_cmd (edit);
507 edit->force |= REDRAW_COMPLETELY;
508 if (res > 0)
510 edit->delete_file = 0;
511 edit->modified = 0;
514 return TRUE;
517 /* --------------------------------------------------------------------------------------------- */
519 * Load file content
521 * @param h screen the owner of editor window
522 * @param vpath vfs file path
523 * @return TRUE if file content was successfully loaded, FALSE otherwise
526 static inline gboolean
527 edit_load_file_from_filename (WDialog * h, const vfs_path_t * vpath)
529 Widget *w = WIDGET (h);
531 return edit_add_window (h, w->y + 1, w->x, w->lines - 2, w->cols, vpath, 0);
534 /* --------------------------------------------------------------------------------------------- */
536 static void
537 edit_delete_column_of_text (WEdit * edit)
539 off_t p, q, r;
540 off_t m1, m2;
541 off_t n;
542 long b, c, d;
544 eval_marks (edit, &m1, &m2);
545 n = edit_buffer_move_forward (&edit->buffer, m1, 0, m2) + 1;
546 c = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m1), 0, m1);
547 d = (long) edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, m2), 0, m2);
548 b = max (min (c, d), min (edit->column1, edit->column2));
549 c = max (c, max (edit->column1, edit->column2));
551 while (n--)
553 r = edit_buffer_get_current_bol (&edit->buffer);
554 p = edit_move_forward3 (edit, r, b, 0);
555 q = edit_move_forward3 (edit, r, c, 0);
556 if (p < m1)
557 p = m1;
558 if (q > m2)
559 q = m2;
560 edit_cursor_move (edit, p - edit->buffer.curs1);
561 while (q > p)
563 /* delete line between margins */
564 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
565 edit_delete (edit, TRUE);
566 q--;
568 if (n)
569 /* move to next line except on the last delete */
570 edit_cursor_move (edit,
571 edit_buffer_move_forward (&edit->buffer, edit->buffer.curs1, 1,
572 0) - edit->buffer.curs1);
576 /* --------------------------------------------------------------------------------------------- */
577 /** if success return 0 */
579 static int
580 edit_block_delete (WEdit * edit)
582 off_t start_mark, end_mark;
583 off_t curs_pos;
584 long curs_line, c1, c2;
586 if (!eval_marks (edit, &start_mark, &end_mark))
587 return 0;
589 if (edit->column_highlight && edit->mark2 < 0)
590 edit_mark_cmd (edit, FALSE);
591 if ((end_mark - start_mark) > option_max_undo / 2)
593 /* Warning message with a query to continue or cancel the operation */
594 if (edit_query_dialog2
595 (_("Warning"),
597 ("Block is large, you may not be able to undo this action"),
598 _("C&ontinue"), _("&Cancel")))
600 return 1;
603 c1 = min (edit->column1, edit->column2);
604 c2 = max (edit->column1, edit->column2);
605 edit->column1 = c1;
606 edit->column2 = c2;
608 edit_push_markers (edit);
610 curs_line = edit->buffer.curs_line;
612 curs_pos = edit->curs_col + edit->over_col;
614 /* move cursor to start of selection */
615 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
616 edit_scroll_screen_over_cursor (edit);
618 if (start_mark < end_mark)
620 if (edit->column_highlight)
622 off_t line_width;
624 if (edit->mark2 < 0)
625 edit_mark_cmd (edit, FALSE);
626 edit_delete_column_of_text (edit);
627 /* move cursor to the saved position */
628 edit_move_to_line (edit, curs_line);
629 /* calculate line width and cursor position before cut */
630 line_width = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), 0,
631 edit_buffer_get_current_eol (&edit->buffer));
632 if (option_cursor_beyond_eol && curs_pos > line_width)
633 edit->over_col = curs_pos - line_width;
635 else
637 off_t count;
639 for (count = start_mark; count < end_mark; count++)
640 edit_delete (edit, TRUE);
643 edit_set_markers (edit, 0, 0, 0, 0);
644 edit->force |= REDRAW_PAGE;
645 return 0;
648 /* --------------------------------------------------------------------------------------------- */
650 * Get EOL symbol for searching.
652 * @param edit editor object
653 * @return EOL symbol
656 static inline char
657 edit_search_get_current_end_line_char (const WEdit * edit)
659 switch (edit->lb)
661 case LB_MAC:
662 return '\r';
663 default:
664 return '\n';
668 /* --------------------------------------------------------------------------------------------- */
670 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
672 * @param search search object
673 * @return result of checks.
676 static edit_search_line_t
677 edit_get_search_line_type (mc_search_t * search)
679 edit_search_line_t search_line_type = 0;
681 if (search->search_type != MC_SEARCH_T_REGEX)
682 return search_line_type;
684 if (*search->original == '^')
685 search_line_type |= AT_START_LINE;
687 if (search->original[search->original_len - 1] == '$')
688 search_line_type |= AT_END_LINE;
689 return search_line_type;
692 /* --------------------------------------------------------------------------------------------- */
694 * Calculating the start position of next line.
696 * @param buf editor buffer object
697 * @param current_pos current position
698 * @param max_pos max position
699 * @param end_string_symbol end of line symbol
700 * @return start position of next line
703 static off_t
704 edit_calculate_start_of_next_line (const edit_buffer_t * buf, off_t current_pos, off_t max_pos,
705 char end_string_symbol)
707 off_t i;
709 for (i = current_pos; i < max_pos; i++)
711 current_pos++;
712 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
713 break;
716 return current_pos;
719 /* --------------------------------------------------------------------------------------------- */
721 * Calculating the end position of previous line.
723 * @param buf editor buffer object
724 * @param current_pos current position
725 * @param end_string_symbol end of line symbol
726 * @return end position of previous line
729 static off_t
730 edit_calculate_end_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
731 char end_string_symbol)
733 off_t i;
735 for (i = current_pos - 1; i >= 0; i--)
736 if (edit_buffer_get_byte (buf, i) == end_string_symbol)
737 break;
739 return i;
742 /* --------------------------------------------------------------------------------------------- */
744 * Calculating the start position of previous line.
746 * @param buf editor buffer object
747 * @param current_pos current position
748 * @param end_string_symbol end of line symbol
749 * @return start position of previous line
752 static inline off_t
753 edit_calculate_start_of_previous_line (const edit_buffer_t * buf, off_t current_pos,
754 char end_string_symbol)
756 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
757 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
759 return (current_pos + 1);
762 /* --------------------------------------------------------------------------------------------- */
764 * Calculating the start position of current line.
766 * @param buf editor buffer object
767 * @param current_pos current position
768 * @param end_string_symbol end of line symbol
769 * @return start position of current line
772 static inline off_t
773 edit_calculate_start_of_current_line (const edit_buffer_t * buf, off_t current_pos,
774 char end_string_symbol)
776 current_pos = edit_calculate_end_of_previous_line (buf, current_pos, end_string_symbol);
778 return (current_pos + 1);
781 /* --------------------------------------------------------------------------------------------- */
783 * Fixing (if needed) search start position if 'only in selection' option present.
785 * @param edit editor object
788 static void
789 edit_search_fix_search_start_if_selection (WEdit * edit)
791 off_t start_mark = 0;
792 off_t end_mark = 0;
794 if (!edit_search_options.only_in_selection)
795 return;
797 if (!eval_marks (edit, &start_mark, &end_mark) != 0)
798 return;
800 if (edit_search_options.backwards)
802 if (edit->search_start > end_mark || edit->search_start <= start_mark)
803 edit->search_start = end_mark;
805 else
807 if (edit->search_start < start_mark || edit->search_start >= end_mark)
808 edit->search_start = start_mark;
812 /* --------------------------------------------------------------------------------------------- */
814 static gboolean
815 editcmd_find (edit_search_status_msg_t * esm, gsize * len)
817 WEdit *edit = esm->edit;
818 off_t search_start = edit->search_start;
819 off_t search_end;
820 off_t start_mark = 0;
821 off_t end_mark = edit->buffer.size;
822 char end_string_symbol;
824 end_string_symbol = edit_search_get_current_end_line_char (edit);
826 /* prepare for search */
827 if (edit_search_options.only_in_selection)
829 if (!eval_marks (edit, &start_mark, &end_mark))
831 edit->search->error = MC_SEARCH_E_NOTFOUND;
832 edit->search->error_str = g_strdup (_("Search string not found"));
833 return FALSE;
836 /* fix the start and the end of search block positions */
837 if ((edit->search_line_type & AT_START_LINE) != 0
838 && (start_mark != 0
839 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol))
841 start_mark =
842 edit_calculate_start_of_next_line (&edit->buffer, start_mark, edit->buffer.size,
843 end_string_symbol);
845 if ((edit->search_line_type & AT_END_LINE) != 0
846 && (end_mark - 1 != edit->buffer.size
847 || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol))
848 end_mark =
849 edit_calculate_end_of_previous_line (&edit->buffer, end_mark, end_string_symbol);
850 if (start_mark >= end_mark)
852 edit->search->error = MC_SEARCH_E_NOTFOUND;
853 edit->search->error_str = g_strdup (_("Search string not found"));
854 return FALSE;
857 else
859 if (edit_search_options.backwards)
860 end_mark = max (1, edit->buffer.curs1) - 1;
863 /* search */
864 if (edit_search_options.backwards)
866 /* backward search */
867 search_end = end_mark;
869 if ((edit->search_line_type & AT_START_LINE) != 0)
870 search_start =
871 edit_calculate_start_of_current_line (&edit->buffer, search_start,
872 end_string_symbol);
874 while (search_start >= start_mark)
876 if (search_end > (off_t) (search_start + edit->search->original_len)
877 && mc_search_is_fixed_search_str (edit->search))
879 search_end = search_start + edit->search->original_len;
881 if (mc_search_run (edit->search, (void *) esm, search_start, search_end, len)
882 && edit->search->normal_offset == search_start)
884 return TRUE;
888 if ((edit->search_line_type & AT_START_LINE) != 0)
889 search_start =
890 edit_calculate_start_of_previous_line (&edit->buffer, search_start,
891 end_string_symbol);
892 else
893 search_start--;
895 edit->search->error_str = g_strdup (_("Search string not found"));
897 else
899 /* forward search */
900 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
901 search_start =
902 edit_calculate_start_of_next_line (&edit->buffer, search_start, end_mark,
903 end_string_symbol);
904 return mc_search_run (edit->search, (void *) esm, search_start, end_mark, len);
906 return FALSE;
909 /* --------------------------------------------------------------------------------------------- */
911 static char *
912 edit_replace_cmd__conv_to_display (char *str)
914 #ifdef HAVE_CHARSET
915 GString *tmp;
917 tmp = str_convert_to_display (str);
918 if (tmp != NULL)
920 if (tmp->len != 0)
921 return g_string_free (tmp, FALSE);
922 g_string_free (tmp, TRUE);
924 #endif
925 return g_strdup (str);
928 /* --------------------------------------------------------------------------------------------- */
930 static char *
931 edit_replace_cmd__conv_to_input (char *str)
933 #ifdef HAVE_CHARSET
934 GString *tmp;
936 tmp = str_convert_to_input (str);
937 if (tmp != NULL)
939 if (tmp->len != 0)
940 return g_string_free (tmp, FALSE);
941 g_string_free (tmp, TRUE);
943 #endif
944 return g_strdup (str);
947 /* --------------------------------------------------------------------------------------------- */
949 static void
950 edit_do_search (WEdit * edit)
952 edit_search_status_msg_t esm;
953 gsize len = 0;
955 if (edit->search == NULL)
956 edit->search_start = edit->buffer.curs1;
958 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
960 esm.first = TRUE;
961 esm.edit = edit;
962 esm.offset = edit->search_start;
964 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
965 edit_search_status_update_cb, NULL);
967 if (search_create_bookmark)
969 int found = 0, books = 0;
970 long l = 0, l_last = -1;
971 long q = 0;
973 search_create_bookmark = FALSE;
974 book_mark_flush (edit, -1);
976 while (mc_search_run (edit->search, (void *) &esm, q, edit->buffer.size, &len))
978 if (found == 0)
979 edit->search_start = edit->search->normal_offset;
980 found++;
981 l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset);
982 if (l != l_last)
984 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
985 books++;
987 l_last = l;
988 q = edit->search->normal_offset + 1;
991 if (found == 0)
992 edit_error_dialog (_("Search"), _("Search string not found"));
993 else
994 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
996 else
998 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
999 && edit_search_options.backwards)
1000 edit->search_start--;
1002 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
1003 && !edit_search_options.backwards)
1004 edit->search_start++;
1006 if (editcmd_find (&esm, &len))
1008 edit->found_start = edit->search_start = edit->search->normal_offset;
1009 edit->found_len = len;
1010 edit->over_col = 0;
1011 edit_cursor_move (edit, edit->search_start - edit->buffer.curs1);
1012 edit_scroll_screen_over_cursor (edit);
1013 if (edit_search_options.backwards)
1014 edit->search_start--;
1015 else
1016 edit->search_start++;
1018 else
1020 edit->search_start = edit->buffer.curs1;
1021 if (edit->search->error_str != NULL)
1022 edit_query_dialog (_("Search"), edit->search->error_str);
1026 status_msg_deinit (STATUS_MSG (&esm));
1028 edit->force |= REDRAW_COMPLETELY;
1029 edit_scroll_screen_over_cursor (edit);
1032 /* --------------------------------------------------------------------------------------------- */
1034 static void
1035 edit_search (WEdit * edit)
1037 if (editcmd_dialog_search_show (edit))
1039 edit->search_line_type = edit_get_search_line_type (edit->search);
1040 edit_search_fix_search_start_if_selection (edit);
1041 edit_do_search (edit);
1045 /* --------------------------------------------------------------------------------------------- */
1046 /** Return a null terminated length of text. Result must be g_free'd */
1048 static unsigned char *
1049 edit_get_block (WEdit * edit, off_t start, off_t finish, off_t * l)
1051 unsigned char *s, *r;
1053 r = s = g_malloc0 (finish - start + 1);
1054 if (edit->column_highlight)
1056 *l = 0;
1057 /* copy from buffer, excluding chars that are out of the column 'margins' */
1058 while (start < finish)
1060 int c;
1061 off_t x;
1063 x = edit_move_forward3 (edit, edit_buffer_get_bol (&edit->buffer, start), 0, start);
1064 c = edit_buffer_get_byte (&edit->buffer, start);
1065 if ((x >= edit->column1 && x < edit->column2)
1066 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1068 *s++ = c;
1069 (*l)++;
1071 start++;
1074 else
1076 *l = finish - start;
1077 while (start < finish)
1078 *s++ = edit_buffer_get_byte (&edit->buffer, start++);
1080 *s = '\0';
1081 return r;
1084 /* --------------------------------------------------------------------------------------------- */
1085 /** copies a block to clipboard file */
1087 static gboolean
1088 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1090 gboolean ret;
1091 gchar *tmp;
1093 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1094 ret = edit_save_block (edit, tmp, start, finish);
1095 g_free (tmp);
1096 return ret;
1099 /* --------------------------------------------------------------------------------------------- */
1101 static void
1102 pipe_mail (const edit_buffer_t * buf, char *to, char *subject, char *cc)
1104 FILE *p = 0;
1105 char *s;
1107 to = name_quote (to, FALSE);
1108 subject = name_quote (subject, FALSE);
1109 cc = name_quote (cc, FALSE);
1110 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1111 g_free (to);
1112 g_free (subject);
1113 g_free (cc);
1115 if (s != NULL)
1117 p = popen (s, "w");
1118 g_free (s);
1121 if (p != NULL)
1123 off_t i;
1125 for (i = 0; i < buf->size; i++)
1126 fputc (edit_buffer_get_byte (buf, i), p);
1127 pclose (p);
1131 /* --------------------------------------------------------------------------------------------- */
1132 /** find first character of current word */
1134 static gboolean
1135 edit_find_word_start (const edit_buffer_t * buf, off_t * word_start, gsize * word_len)
1137 int c;
1138 off_t i;
1140 /* return if at begin of file */
1141 if (buf->curs1 <= 0)
1142 return FALSE;
1144 c = edit_buffer_get_previous_byte (buf);
1145 /* return if not at end or in word */
1146 if (is_break_char (c))
1147 return FALSE;
1149 /* search start of word to be completed */
1150 for (i = 1;; i++)
1152 int last;
1154 last = c;
1155 c = edit_buffer_get_byte (buf, buf->curs1 - i - 1);
1157 if (is_break_char (c))
1159 /* return if word starts with digit */
1160 if (isdigit (last))
1161 return FALSE;
1163 break;
1167 /* success */
1168 *word_start = buf->curs1 - i; /* start found */
1169 *word_len = (gsize) i;
1170 return TRUE;
1173 /* --------------------------------------------------------------------------------------------- */
1175 * Get current word under cursor
1177 * @param esm status message window
1178 * @param srch mc_search object
1179 * @param word_start start word position
1181 * @return newly allocated string or NULL if no any words under cursor
1184 static char *
1185 edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_search_t * srch,
1186 off_t word_start)
1188 WEdit *edit = esm->edit;
1189 gsize len = 0;
1190 off_t i;
1191 GString *temp;
1193 if (!mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
1194 return NULL;
1196 temp = g_string_sized_new (len);
1198 for (i = 0; i < (off_t) len; i++)
1200 int chr;
1202 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
1203 if (!isspace (chr))
1204 g_string_append_c (temp, chr);
1207 return g_string_free (temp, temp->len == 0);
1210 /* --------------------------------------------------------------------------------------------- */
1211 /** collect the possible completions */
1213 static gsize
1214 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1215 char *match_expr, GString ** compl, gsize * num)
1217 gsize len = 0;
1218 gsize max_len = 0;
1219 gsize i;
1220 int skip;
1221 GString *temp;
1222 mc_search_t *srch;
1223 off_t last_byte, start = -1;
1224 char *current_word;
1225 gboolean entire_file;
1226 edit_search_status_msg_t esm;
1228 #ifdef HAVE_CHARSET
1229 srch = mc_search_new (match_expr, -1, cp_source);
1230 #else
1231 srch = mc_search_new (match_expr, -1, NULL);
1232 #endif
1233 if (srch == NULL)
1234 return 0;
1236 entire_file =
1237 mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
1238 "editor_wordcompletion_collect_entire_file", 0);
1240 last_byte = entire_file ? edit->buffer.size : word_start;
1242 srch->search_type = MC_SEARCH_T_REGEX;
1243 srch->is_case_sensitive = TRUE;
1244 srch->search_fn = edit_search_cmd_callback;
1245 srch->update_fn = edit_search_update_callback;
1247 esm.first = TRUE;
1248 esm.edit = edit;
1249 esm.offset = entire_file ? 0 : word_start;
1251 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
1252 edit_search_status_update_cb, NULL);
1254 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
1256 temp = g_string_new ("");
1258 /* collect max MAX_WORD_COMPLETIONS completions */
1259 while (mc_search_run (srch, (void *) &esm, start + 1, last_byte, &len))
1261 g_string_set_size (temp, 0);
1262 start = srch->normal_offset;
1264 /* add matched completion if not yet added */
1265 for (i = 0; i < len; i++)
1267 skip = edit_buffer_get_byte (&edit->buffer, start + i);
1268 if (isspace (skip))
1269 continue;
1271 /* skip current word */
1272 if (start + (off_t) i == word_start)
1273 break;
1275 g_string_append_c (temp, skip);
1278 if (temp->len == 0)
1279 continue;
1281 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1282 continue;
1284 skip = 0;
1286 for (i = 0; i < *num; i++)
1288 if (strncmp
1289 ((char *) &compl[i]->str[word_len],
1290 (char *) &temp->str[word_len], max (len, compl[i]->len) - word_len) == 0)
1292 GString *this = compl[i];
1293 for (++i; i < *num; i++)
1294 compl[i - 1] = compl[i];
1295 compl[*num - 1] = this;
1296 skip = 1;
1297 break; /* skip it, already added */
1300 if (skip != 0)
1301 continue;
1303 if (*num == MAX_WORD_COMPLETIONS)
1305 g_string_free (compl[0], TRUE);
1306 for (i = 1; i < *num; i++)
1307 compl[i - 1] = compl[i];
1308 (*num)--;
1310 #ifdef HAVE_CHARSET
1312 GString *recoded;
1313 recoded = str_convert_to_display (temp->str);
1315 if (recoded && recoded->len)
1316 g_string_assign (temp, recoded->str);
1318 g_string_free (recoded, TRUE);
1320 #endif
1321 compl[(*num)++] = g_string_new_len (temp->str, temp->len);
1322 start += len;
1324 /* note the maximal length needed for the completion dialog */
1325 if (len > max_len)
1326 max_len = len;
1329 status_msg_deinit (STATUS_MSG (&esm));
1330 mc_search_free (srch);
1331 g_string_free (temp, TRUE);
1332 g_free (current_word);
1334 return max_len;
1337 /* --------------------------------------------------------------------------------------------- */
1339 static void
1340 edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long width,
1341 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
1343 off_t i, cursor;
1344 long col;
1346 cursor = edit->buffer.curs1;
1347 col = edit_get_col (edit);
1349 for (i = 0; i < size; i++)
1351 if (data[i] != '\n')
1352 edit_insert (edit, data[i]);
1353 else
1354 { /* fill in and move to next line */
1355 long l;
1356 off_t p;
1358 if (edit_buffer_get_current_byte (&edit->buffer) != '\n')
1360 for (l = width - (edit_get_col (edit) - col); l > 0; l -= space_width)
1361 edit_insert (edit, ' ');
1363 for (p = edit->buffer.curs1;; p++)
1365 if (p == edit->buffer.size)
1367 edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1);
1368 edit_insert_ahead (edit, '\n');
1369 p++;
1370 break;
1372 if (edit_buffer_get_byte (&edit->buffer, p) == '\n')
1374 p++;
1375 break;
1378 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->buffer.curs1);
1380 for (l = col - edit_get_col (edit); l >= space_width; l -= space_width)
1381 edit_insert (edit, ' ');
1385 *col1 = col;
1386 *col2 = col + width;
1387 *start_pos = cursor;
1388 *end_pos = edit->buffer.curs1;
1389 edit_cursor_move (edit, cursor - edit->buffer.curs1);
1392 /* --------------------------------------------------------------------------------------------- */
1394 static int
1395 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1397 const macros_t *m1 = (const macros_t *) macro1;
1398 const macros_t *m2 = (const macros_t *) macro2;
1400 return m1->hotkey - m2->hotkey;
1403 /* --------------------------------------------------------------------------------------------- */
1405 static void
1406 edit_macro_sort_by_hotkey (void)
1408 if (macros_list != NULL && macros_list->len != 0)
1409 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1412 /* --------------------------------------------------------------------------------------------- */
1414 static gboolean
1415 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1417 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1418 macros_t *result;
1419 macros_t search_macro;
1421 (void) edit;
1423 search_macro.hotkey = hotkey;
1424 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1425 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1427 if (result != NULL && result->macro != NULL)
1429 *indx = (result - array_start);
1430 *macros = result;
1431 return TRUE;
1433 *indx = 0;
1434 return FALSE;
1437 /* --------------------------------------------------------------------------------------------- */
1438 /** returns FALSE on error */
1440 static gboolean
1441 edit_delete_macro (WEdit * edit, int hotkey)
1443 mc_config_t *macros_config = NULL;
1444 const char *section_name = "editor";
1445 gchar *macros_fname;
1446 guint indx;
1447 char *skeyname;
1448 const macros_t *macros = NULL;
1450 /* clear array of actions for current hotkey */
1451 while (edit_get_macro (edit, hotkey, &macros, &indx))
1453 if (macros->macro != NULL)
1454 g_array_free (macros->macro, TRUE);
1455 macros = NULL;
1456 g_array_remove_index (macros_list, indx);
1457 edit_macro_sort_by_hotkey ();
1460 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1461 macros_config = mc_config_init (macros_fname, FALSE);
1462 g_free (macros_fname);
1464 if (macros_config == NULL)
1465 return FALSE;
1467 skeyname = lookup_key_by_code (hotkey);
1468 while (mc_config_del_key (macros_config, section_name, skeyname))
1470 g_free (skeyname);
1471 mc_config_save_file (macros_config, NULL);
1472 mc_config_deinit (macros_config);
1473 return TRUE;
1476 /* --------------------------------------------------------------------------------------------- */
1478 * Callback for the iteration of objects in the 'editors' array.
1479 * Toggle syntax highlighting in editor object.
1481 * @param data probably WEdit object
1482 * @param user_data unused
1485 static void
1486 edit_syntax_onoff_cb (void *data, void *user_data)
1488 (void) user_data;
1490 if (edit_widget_is_editor ((const Widget *) data))
1492 WEdit *edit = (WEdit *) data;
1494 if (option_syntax_highlighting)
1495 edit_load_syntax (edit, NULL, edit->syntax_type);
1496 edit->force |= REDRAW_PAGE;
1500 /* --------------------------------------------------------------------------------------------- */
1502 * Callback for the iteration of objects in the 'editors' array.
1503 * Redraw editor object.
1505 * @param data probably WEdit object
1506 * @param user_data unused
1509 static void
1510 edit_redraw_page_cb (void *data, void *user_data)
1512 (void) user_data;
1514 if (edit_widget_is_editor ((const Widget *) data))
1515 ((WEdit *) data)->force |= REDRAW_PAGE;
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Insert autocompleted word into editor.
1522 * @param edit editor object
1523 * @param completion word for completion
1524 * @param word_len offset from beginning for insert
1527 static void
1528 edit_complete_word_insert_recoded_completion (WEdit * edit, char *completion, gsize word_len)
1530 #ifdef HAVE_CHARSET
1531 GString *temp;
1533 temp = str_convert_to_input (completion);
1535 for (completion = temp->str + word_len; *completion != '\0'; completion++)
1536 edit_insert (edit, *completion);
1537 g_string_free (temp, TRUE);
1538 #else
1539 for (completion += word_len; *completion != '\0'; completion++)
1540 edit_insert (edit, *completion);
1541 #endif
1544 /* --------------------------------------------------------------------------------------------- */
1545 /*** public functions ****************************************************************************/
1546 /* --------------------------------------------------------------------------------------------- */
1548 void
1549 edit_refresh_cmd (void)
1551 clr_scr ();
1552 repaint_screen ();
1553 tty_keypad (TRUE);
1556 /* --------------------------------------------------------------------------------------------- */
1558 * Toggle syntax highlighting in all editor windows.
1560 * @param h root widget for all windows
1563 void
1564 edit_syntax_onoff_cmd (WDialog * h)
1566 option_syntax_highlighting = !option_syntax_highlighting;
1567 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1568 dlg_redraw (h);
1571 /* --------------------------------------------------------------------------------------------- */
1573 * Toggle tabs showing in all editor windows.
1575 * @param h root widget for all windows
1578 void
1579 edit_show_tabs_tws_cmd (WDialog * h)
1581 enable_show_tabs_tws = !enable_show_tabs_tws;
1582 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1583 dlg_redraw (h);
1586 /* --------------------------------------------------------------------------------------------- */
1588 * Toggle right margin showing in all editor windows.
1590 * @param h root widget for all windows
1593 void
1594 edit_show_margin_cmd (WDialog * h)
1596 show_right_margin = !show_right_margin;
1597 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1598 dlg_redraw (h);
1601 /* --------------------------------------------------------------------------------------------- */
1603 * Toggle line numbers showing in all editor windows.
1605 * @param h root widget for all windows
1608 void
1609 edit_show_numbers_cmd (WDialog * h)
1611 option_line_state = !option_line_state;
1612 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1613 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1614 dlg_redraw (h);
1617 /* --------------------------------------------------------------------------------------------- */
1619 void
1620 edit_save_mode_cmd (void)
1622 char *str_result = NULL;
1624 const char *str[] = {
1625 N_("&Quick save"),
1626 N_("&Safe save"),
1627 N_("&Do backups with following extension:")
1630 #ifdef HAVE_ASSERT_H
1631 assert (option_backup_ext != NULL);
1632 #endif
1634 #ifdef ENABLE_NLS
1635 size_t i;
1637 for (i = 0; i < 3; i++)
1638 str[i] = _(str[i]);
1639 #endif
1642 quick_widget_t quick_widgets[] = {
1643 /* *INDENT-OFF* */
1644 QUICK_RADIO (3, str, &option_save_mode, &edit_save_mode_radio_id),
1645 QUICK_INPUT (option_backup_ext, "edit-backup-ext", &str_result,
1646 &edit_save_mode_input_id, FALSE, FALSE, INPUT_COMPLETE_NONE),
1647 QUICK_SEPARATOR (TRUE),
1648 QUICK_CHECKBOX (N_("Check &POSIX new line"), &option_check_nl_at_eof, NULL),
1649 QUICK_BUTTONS_OK_CANCEL,
1650 QUICK_END
1651 /* *INDENT-ON* */
1654 quick_dialog_t qdlg = {
1655 -1, -1, 38,
1656 N_("Edit Save Mode"), "[Edit Save Mode]",
1657 quick_widgets, edit_save_mode_callback, NULL
1660 if (quick_dialog (&qdlg) != B_CANCEL)
1662 g_free (option_backup_ext);
1663 option_backup_ext = str_result;
1668 /* --------------------------------------------------------------------------------------------- */
1670 void
1671 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1673 vfs_path_free (edit->filename_vpath);
1674 edit->filename_vpath = vfs_path_clone (name_vpath);
1676 if (edit->dir_vpath == NULL)
1677 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1680 /* --------------------------------------------------------------------------------------------- */
1681 /* Here we want to warn the users of overwriting an existing file,
1682 but only if they have made a change to the filename */
1683 /* returns TRUE on success */
1684 gboolean
1685 edit_save_as_cmd (WEdit * edit)
1687 /* This heads the 'Save As' dialog box */
1688 vfs_path_t *exp_vpath;
1689 int save_lock = 0;
1690 int different_filename = 0;
1692 if (!edit_check_newline (&edit->buffer))
1693 return FALSE;
1695 exp_vpath = edit_get_save_file_as (edit);
1696 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1698 if (exp_vpath != NULL)
1700 if (vfs_path_len (exp_vpath) == 0)
1701 goto ret;
1702 else
1704 int rv;
1706 if (!vfs_path_equal (edit->filename_vpath, exp_vpath))
1708 int file;
1709 struct stat sb;
1711 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1713 edit_error_dialog (_("Save as"),
1714 get_sys_error (_
1715 ("Cannot save: destination is not a regular file")));
1716 goto ret;
1719 different_filename = 1;
1720 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1722 if (file != -1)
1724 /* the file exists */
1725 mc_close (file);
1726 /* Overwrite the current file or cancel the operation */
1727 if (edit_query_dialog2
1728 (_("Warning"),
1729 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1730 goto ret;
1732 else
1734 edit->stat1.st_mode |= S_IWUSR;
1736 save_lock = lock_file (exp_vpath);
1738 else
1740 /* filenames equal, check if already locked */
1741 if (!edit->locked && !edit->delete_file)
1742 save_lock = lock_file (exp_vpath);
1745 if (different_filename)
1748 * Allow user to write into saved (under another name) file
1749 * even if original file had r/o user permissions.
1751 edit->stat1.st_mode |= S_IWRITE;
1754 rv = edit_save_file (edit, exp_vpath);
1755 switch (rv)
1757 case 1:
1758 /* Successful, so unlock both files */
1759 if (different_filename)
1761 if (save_lock)
1762 unlock_file (exp_vpath);
1763 if (edit->locked)
1764 edit->locked = unlock_file (edit->filename_vpath);
1766 else
1768 if (edit->locked || save_lock)
1769 edit->locked = unlock_file (edit->filename_vpath);
1772 edit_set_filename (edit, exp_vpath);
1773 if (edit->lb != LB_ASIS)
1774 edit_reload (edit, exp_vpath);
1775 edit->modified = 0;
1776 edit->delete_file = 0;
1777 if (different_filename)
1778 edit_load_syntax (edit, NULL, edit->syntax_type);
1779 vfs_path_free (exp_vpath);
1780 edit->force |= REDRAW_COMPLETELY;
1781 return TRUE;
1782 default:
1783 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1784 /* fallthrough */
1785 case -1:
1786 /* Failed, so maintain modify (not save) lock */
1787 if (save_lock)
1788 unlock_file (exp_vpath);
1789 break;
1794 ret:
1795 vfs_path_free (exp_vpath);
1796 edit->force |= REDRAW_COMPLETELY;
1797 return FALSE;
1800 /* {{{ Macro stuff starts here */
1801 /* --------------------------------------------------------------------------------------------- */
1803 void
1804 edit_delete_macro_cmd (WEdit * edit)
1806 int hotkey;
1808 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1810 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1811 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1814 /* --------------------------------------------------------------------------------------------- */
1816 /** returns FALSE on error */
1817 gboolean
1818 edit_execute_macro (WEdit * edit, int hotkey)
1820 gboolean res = FALSE;
1822 if (hotkey != 0)
1824 const macros_t *macros;
1825 guint indx;
1827 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1828 macros->macro != NULL && macros->macro->len != 0)
1830 guint i;
1832 edit->force |= REDRAW_PAGE;
1834 for (i = 0; i < macros->macro->len; i++)
1836 const macro_action_t *m_act;
1838 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1839 edit_execute_cmd (edit, m_act->action, m_act->ch);
1840 res = TRUE;
1845 return res;
1848 /* --------------------------------------------------------------------------------------------- */
1850 /** returns FALSE on error */
1851 gboolean
1852 edit_store_macro_cmd (WEdit * edit)
1854 int i;
1855 int hotkey;
1856 GString *marcros_string;
1857 mc_config_t *macros_config = NULL;
1858 const char *section_name = "editor";
1859 gchar *macros_fname;
1860 GArray *macros; /* current macro */
1861 int tmp_act;
1862 gboolean have_macro = FALSE;
1863 char *skeyname = NULL;
1865 hotkey =
1866 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1867 if (hotkey == ESC_CHAR)
1868 return FALSE;
1870 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1872 /* return FALSE if try assign macro into restricted hotkeys */
1873 if (tmp_act == CK_MacroStartRecord
1874 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1875 return FALSE;
1877 edit_delete_macro (edit, hotkey);
1879 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1880 macros_config = mc_config_init (macros_fname, FALSE);
1881 g_free (macros_fname);
1883 if (macros_config == NULL)
1884 return FALSE;
1886 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1888 marcros_string = g_string_sized_new (250);
1889 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1891 skeyname = lookup_key_by_code (hotkey);
1893 for (i = 0; i < macro_index; i++)
1895 macro_action_t m_act;
1896 const char *action_name;
1898 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1900 if (action_name == NULL)
1901 break;
1903 m_act.action = record_macro_buf[i].action;
1904 m_act.ch = record_macro_buf[i].ch;
1905 g_array_append_val (macros, m_act);
1906 have_macro = TRUE;
1907 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1908 (int) record_macro_buf[i].ch);
1910 if (have_macro)
1912 macros_t macro;
1913 macro.hotkey = hotkey;
1914 macro.macro = macros;
1915 g_array_append_val (macros_list, macro);
1916 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1918 else
1919 mc_config_del_key (macros_config, section_name, skeyname);
1921 g_free (skeyname);
1922 edit_macro_sort_by_hotkey ();
1924 g_string_free (marcros_string, TRUE);
1925 mc_config_save_file (macros_config, NULL);
1926 mc_config_deinit (macros_config);
1927 return TRUE;
1930 /* --------------------------------------------------------------------------------------------- */
1932 gboolean
1933 edit_repeat_macro_cmd (WEdit * edit)
1935 int i, j;
1936 char *f;
1937 long count_repeat;
1938 char *error = NULL;
1940 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL,
1941 INPUT_COMPLETE_NONE);
1942 if (f == NULL || *f == '\0')
1944 g_free (f);
1945 return FALSE;
1948 count_repeat = strtol (f, &error, 0);
1950 if (*error != '\0')
1952 g_free (f);
1953 return FALSE;
1956 g_free (f);
1958 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1959 edit->force |= REDRAW_PAGE;
1961 for (j = 0; j < count_repeat; j++)
1962 for (i = 0; i < macro_index; i++)
1963 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1964 edit_update_screen (edit);
1965 return TRUE;
1968 /* --------------------------------------------------------------------------------------------- */
1969 /** return FALSE on error */
1971 gboolean
1972 edit_load_macro_cmd (WEdit * edit)
1974 mc_config_t *macros_config = NULL;
1975 gchar **profile_keys, **keys;
1976 gchar **values, **curr_values;
1977 const char *section_name = "editor";
1978 gchar *macros_fname;
1980 (void) edit;
1982 if (macros_list == NULL || macros_list->len != 0)
1983 return FALSE;
1985 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1986 macros_config = mc_config_init (macros_fname, TRUE);
1987 g_free (macros_fname);
1989 if (macros_config == NULL)
1990 return FALSE;
1992 keys = mc_config_get_keys (macros_config, section_name, NULL);
1994 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
1996 int hotkey;
1997 gboolean have_macro = FALSE;
1998 GArray *macros;
1999 macros_t macro;
2001 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
2002 values = mc_config_get_string_list (macros_config, section_name, *profile_keys, NULL);
2003 hotkey = lookup_key (*profile_keys, NULL);
2005 for (curr_values = values; *curr_values != NULL && *curr_values[0] != '\0'; curr_values++)
2007 char **macro_pair = NULL;
2009 macro_pair = g_strsplit (*curr_values, ":", 2);
2010 if (macro_pair != NULL)
2012 macro_action_t m_act;
2013 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
2014 m_act.action = 0;
2015 else
2017 m_act.action = keybind_lookup_action (macro_pair[0]);
2018 MC_PTR_FREE (macro_pair[0]);
2020 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
2021 m_act.ch = -1;
2022 else
2024 m_act.ch = strtol (macro_pair[1], NULL, 0);
2025 MC_PTR_FREE (macro_pair[1]);
2027 if (m_act.action != 0)
2029 /* a shell command */
2030 if ((m_act.action / CK_PipeBlock (0)) == 1)
2032 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2033 m_act.ch = -1;
2035 g_array_append_val (macros, m_act);
2036 have_macro = TRUE;
2038 g_strfreev (macro_pair);
2039 macro_pair = NULL;
2042 if (have_macro)
2044 macro.hotkey = hotkey;
2045 macro.macro = macros;
2046 g_array_append_val (macros_list, macro);
2048 g_strfreev (values);
2050 g_strfreev (keys);
2051 mc_config_deinit (macros_config);
2052 edit_macro_sort_by_hotkey ();
2053 return TRUE;
2056 /* }}} Macro stuff end here */
2058 /* --------------------------------------------------------------------------------------------- */
2059 /** returns TRUE on success */
2061 gboolean
2062 edit_save_confirm_cmd (WEdit * edit)
2064 if (edit->filename_vpath == NULL)
2065 return edit_save_as_cmd (edit);
2067 if (!edit_check_newline (&edit->buffer))
2068 return FALSE;
2070 if (edit_confirm_save)
2072 char *f;
2073 gboolean ok;
2075 f = g_strdup_printf (_("Confirm save file: \"%s\""),
2076 vfs_path_as_str (edit->filename_vpath));
2077 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2078 g_free (f);
2079 if (!ok)
2080 return FALSE;
2082 return edit_save_cmd (edit);
2085 /* --------------------------------------------------------------------------------------------- */
2087 * Ask file to edit and load it.
2089 * @return TRUE on success or cancel of ask.
2092 gboolean
2093 edit_load_cmd (WDialog * h)
2095 char *exp;
2096 gboolean ret = TRUE; /* possible cancel */
2098 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2099 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT,
2100 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD);
2102 if (exp != NULL && *exp != '\0')
2104 vfs_path_t *exp_vpath;
2106 exp_vpath = vfs_path_from_str (exp);
2107 ret = edit_load_file_from_filename (h, exp_vpath);
2108 vfs_path_free (exp_vpath);
2111 g_free (exp);
2113 return ret;
2116 /* --------------------------------------------------------------------------------------------- */
2118 * Load syntax file to edit.
2120 * @return TRUE on success
2123 gboolean
2124 edit_load_syntax_file (WDialog * h)
2126 vfs_path_t *extdir_vpath;
2127 int dir = 0;
2128 gboolean ret = FALSE;
2130 if (geteuid () == 0)
2131 dir = query_dialog (_("Syntax file edit"),
2132 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2133 _("&User"), _("&System wide"));
2135 extdir_vpath =
2136 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2137 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2139 vfs_path_free (extdir_vpath);
2140 extdir_vpath =
2141 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2144 if (dir == 0)
2146 vfs_path_t *user_syntax_file_vpath;
2148 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2149 check_for_default (extdir_vpath, user_syntax_file_vpath);
2150 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2151 vfs_path_free (user_syntax_file_vpath);
2153 else if (dir == 1)
2154 ret = edit_load_file_from_filename (h, extdir_vpath);
2156 vfs_path_free (extdir_vpath);
2158 return ret;
2161 /* --------------------------------------------------------------------------------------------- */
2163 * Load menu file to edit.
2165 * @return TRUE on success
2168 gboolean
2169 edit_load_menu_file (WDialog * h)
2171 vfs_path_t *buffer_vpath;
2172 vfs_path_t *menufile_vpath;
2173 int dir;
2174 gboolean ret;
2176 query_set_sel (1);
2177 dir = query_dialog (_("Menu edit"),
2178 _("Which menu file do you want to edit?"), D_NORMAL,
2179 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2181 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2182 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2184 vfs_path_free (menufile_vpath);
2185 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2188 switch (dir)
2190 case 0:
2191 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2192 check_for_default (menufile_vpath, buffer_vpath);
2193 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2194 break;
2196 case 1:
2197 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2198 check_for_default (menufile_vpath, buffer_vpath);
2199 break;
2201 case 2:
2202 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2203 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2205 vfs_path_free (buffer_vpath);
2206 buffer_vpath =
2207 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2209 break;
2211 default:
2212 vfs_path_free (menufile_vpath);
2213 return FALSE;
2216 ret = edit_load_file_from_filename (h, buffer_vpath);
2218 vfs_path_free (buffer_vpath);
2219 vfs_path_free (menufile_vpath);
2221 return ret;
2224 /* --------------------------------------------------------------------------------------------- */
2226 * Close window with opened file.
2228 * @return TRUE if file was closed.
2231 gboolean
2232 edit_close_cmd (WEdit * edit)
2234 gboolean ret;
2236 ret = (edit != NULL) && edit_ok_to_exit (edit);
2238 if (ret)
2240 WDialog *h = WIDGET (edit)->owner;
2242 if (edit->locked != 0)
2243 unlock_file (edit->filename_vpath);
2245 del_widget (edit);
2247 if (edit_widget_is_editor (WIDGET (h->current->data)))
2248 edit = (WEdit *) h->current->data;
2249 else
2251 edit = find_editor (h);
2252 if (edit != NULL)
2253 dlg_set_top_widget (edit);
2257 if (edit != NULL)
2258 edit->force |= REDRAW_COMPLETELY;
2260 return ret;
2263 /* --------------------------------------------------------------------------------------------- */
2265 if mark2 is -1 then marking is from mark1 to the cursor.
2266 Otherwise its between the markers. This handles this.
2267 Returns FALSE if no text is marked.
2270 gboolean
2271 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2273 long end_mark_curs;
2275 if (edit->mark1 == edit->mark2)
2277 *start_mark = *end_mark = 0;
2278 edit->column2 = edit->column1 = 0;
2279 return FALSE;
2282 if (edit->end_mark_curs < 0)
2283 end_mark_curs = edit->buffer.curs1;
2284 else
2285 end_mark_curs = edit->end_mark_curs;
2287 if (edit->mark2 >= 0)
2289 *start_mark = min (edit->mark1, edit->mark2);
2290 *end_mark = max (edit->mark1, edit->mark2);
2292 else
2294 *start_mark = min (edit->mark1, end_mark_curs);
2295 *end_mark = max (edit->mark1, end_mark_curs);
2296 edit->column2 = edit->curs_col + edit->over_col;
2299 if (edit->column_highlight
2300 && ((edit->mark1 > end_mark_curs && edit->column1 < edit->column2)
2301 || (edit->mark1 < end_mark_curs && edit->column1 > edit->column2)))
2303 off_t start_bol, start_eol;
2304 off_t end_bol, end_eol;
2305 long col1, col2;
2306 off_t diff1, diff2;
2308 start_bol = edit_buffer_get_bol (&edit->buffer, *start_mark);
2309 start_eol = edit_buffer_get_eol (&edit->buffer, start_bol - 1) + 1;
2310 end_bol = edit_buffer_get_bol (&edit->buffer, *end_mark);
2311 end_eol = edit_buffer_get_eol (&edit->buffer, *end_mark);
2312 col1 = min (edit->column1, edit->column2);
2313 col2 = max (edit->column1, edit->column2);
2315 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2316 edit_move_forward3 (edit, start_bol, col1, 0);
2317 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2318 edit_move_forward3 (edit, end_bol, col1, 0);
2320 *start_mark -= diff1;
2321 *end_mark += diff2;
2322 *start_mark = max (*start_mark, start_eol);
2323 *end_mark = min (*end_mark, end_eol);
2325 return TRUE;
2328 /* --------------------------------------------------------------------------------------------- */
2330 void
2331 edit_block_copy_cmd (WEdit * edit)
2333 off_t start_mark, end_mark, current = edit->buffer.curs1;
2334 off_t mark1, mark2;
2335 long c1, c2;
2336 off_t size;
2337 unsigned char *copy_buf;
2339 edit_update_curs_col (edit);
2340 if (!eval_marks (edit, &start_mark, &end_mark))
2341 return;
2343 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2345 /* all that gets pushed are deletes hence little space is used on the stack */
2347 edit_push_markers (edit);
2349 if (edit->column_highlight)
2351 long col_delta;
2353 col_delta = labs (edit->column2 - edit->column1);
2354 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2356 else
2358 int size_orig = size;
2360 while (size-- != 0)
2361 edit_insert_ahead (edit, copy_buf[size]);
2363 /* Place cursor at the end of text selection */
2364 if (option_cursor_after_inserted_block)
2365 edit_cursor_move (edit, size_orig);
2368 g_free (copy_buf);
2369 edit_scroll_screen_over_cursor (edit);
2371 if (edit->column_highlight)
2372 edit_set_markers (edit, edit->buffer.curs1, mark2, c1, c2);
2373 else if (start_mark < current && end_mark > current)
2374 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2376 edit->force |= REDRAW_PAGE;
2380 /* --------------------------------------------------------------------------------------------- */
2382 void
2383 edit_block_move_cmd (WEdit * edit)
2385 off_t current;
2386 unsigned char *copy_buf = NULL;
2387 off_t start_mark, end_mark;
2389 if (!eval_marks (edit, &start_mark, &end_mark))
2390 return;
2392 if (!edit->column_highlight && edit->buffer.curs1 > start_mark && edit->buffer.curs1 < end_mark)
2393 return;
2395 if (edit->mark2 < 0)
2396 edit_mark_cmd (edit, FALSE);
2397 edit_push_markers (edit);
2399 if (edit->column_highlight)
2401 off_t mark1, mark2;
2402 off_t size;
2403 long c1, c2, b_width;
2404 long x, x2;
2406 c1 = min (edit->column1, edit->column2);
2407 c2 = max (edit->column1, edit->column2);
2408 b_width = c2 - c1;
2410 edit_update_curs_col (edit);
2412 x = edit->curs_col;
2413 x2 = x + edit->over_col;
2415 /* do nothing when cursor inside first line of selected area */
2416 if ((edit_buffer_get_eol (&edit->buffer, edit->buffer.curs1) ==
2417 edit_buffer_get_eol (&edit->buffer, start_mark)) && x2 > c1 && x2 <= c2)
2418 return;
2420 if (edit->buffer.curs1 > start_mark
2421 && edit->buffer.curs1 < edit_buffer_get_eol (&edit->buffer, end_mark))
2423 if (x > c2)
2424 x -= b_width;
2425 else if (x > c1 && x <= c2)
2426 x = c1;
2428 /* save current selection into buffer */
2429 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2431 /* remove current selection */
2432 edit_block_delete_cmd (edit);
2434 edit->over_col = max (0, edit->over_col - b_width);
2435 /* calculate the cursor pos after delete block */
2436 current = edit_move_forward3 (edit, edit_buffer_get_current_bol (&edit->buffer), x, 0);
2437 edit_cursor_move (edit, current - edit->buffer.curs1);
2438 edit_scroll_screen_over_cursor (edit);
2440 /* add TWS if need before block insertion */
2441 if (option_cursor_beyond_eol && edit->over_col > 0)
2442 edit_insert_over (edit);
2444 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2445 edit_set_markers (edit, mark1, mark2, c1, c2);
2447 else
2449 off_t count, count_orig;
2451 current = edit->buffer.curs1;
2452 copy_buf = g_malloc0 (end_mark - start_mark);
2453 edit_cursor_move (edit, start_mark - edit->buffer.curs1);
2454 edit_scroll_screen_over_cursor (edit);
2456 for (count = start_mark; count < end_mark; count++)
2457 copy_buf[end_mark - count - 1] = edit_delete (edit, TRUE);
2459 edit_scroll_screen_over_cursor (edit);
2460 edit_cursor_move (edit,
2461 current - edit->buffer.curs1 -
2462 (((current - edit->buffer.curs1) > 0) ? end_mark - start_mark : 0));
2463 edit_scroll_screen_over_cursor (edit);
2464 count_orig = count;
2465 while (count-- > start_mark)
2466 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2468 edit_set_markers (edit, edit->buffer.curs1, edit->buffer.curs1 + end_mark - start_mark, 0,
2471 /* Place cursor at the end of text selection */
2472 if (option_cursor_after_inserted_block)
2473 edit_cursor_move (edit, count_orig - start_mark);
2476 edit_scroll_screen_over_cursor (edit);
2477 g_free (copy_buf);
2478 edit->force |= REDRAW_PAGE;
2481 /* --------------------------------------------------------------------------------------------- */
2482 /** returns 1 if canceelled by user */
2485 edit_block_delete_cmd (WEdit * edit)
2487 off_t start_mark, end_mark;
2489 if (eval_marks (edit, &start_mark, &end_mark))
2490 return edit_block_delete (edit);
2492 edit_delete_line (edit);
2493 return 0;
2496 /* --------------------------------------------------------------------------------------------- */
2497 /** call with edit = 0 before shutdown to close memory leaks */
2499 void
2500 edit_replace_cmd (WEdit * edit, int again)
2502 /* 1 = search string, 2 = replace with */
2503 static char *saved1 = NULL; /* saved default[123] */
2504 static char *saved2 = NULL;
2505 char *input1 = NULL; /* user input from the dialog */
2506 char *input2 = NULL;
2507 GString *input2_str = NULL;
2508 char *disp1 = NULL;
2509 char *disp2 = NULL;
2510 long times_replaced = 0;
2511 gboolean once_found = FALSE;
2512 edit_search_status_msg_t esm;
2514 if (edit == NULL)
2516 MC_PTR_FREE (saved1);
2517 MC_PTR_FREE (saved2);
2518 return;
2521 edit->force |= REDRAW_COMPLETELY;
2523 if (again && !saved1 && !saved2)
2524 again = 0;
2526 if (again)
2528 input1 = g_strdup (saved1 ? saved1 : "");
2529 input2 = g_strdup (saved2 ? saved2 : "");
2531 else
2533 char *tmp_inp1, *tmp_inp2;
2535 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2536 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2538 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2540 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2542 g_free (disp1);
2543 g_free (disp2);
2545 if (input1 == NULL || *input1 == '\0')
2547 edit->force = REDRAW_COMPLETELY;
2548 goto cleanup;
2551 tmp_inp1 = input1;
2552 tmp_inp2 = input2;
2553 input1 = edit_replace_cmd__conv_to_input (input1);
2554 input2 = edit_replace_cmd__conv_to_input (input2);
2555 g_free (tmp_inp1);
2556 g_free (tmp_inp2);
2558 g_free (saved1), saved1 = g_strdup (input1);
2559 g_free (saved2), saved2 = g_strdup (input2);
2561 mc_search_free (edit->search);
2562 edit->search = NULL;
2565 input2_str = g_string_new (input2);
2567 if (edit->search == NULL)
2569 #ifdef HAVE_CHARSET
2570 edit->search = mc_search_new (input1, -1, cp_source);
2571 #else
2572 edit->search = mc_search_new (input1, -1, NULL);
2573 #endif
2574 if (edit->search == NULL)
2576 edit->search_start = edit->buffer.curs1;
2577 goto cleanup;
2579 edit->search->search_type = edit_search_options.type;
2580 #ifdef HAVE_CHARSET
2581 edit->search->is_all_charsets = edit_search_options.all_codepages;
2582 #endif
2583 edit->search->is_case_sensitive = edit_search_options.case_sens;
2584 edit->search->whole_words = edit_search_options.whole_words;
2585 edit->search->search_fn = edit_search_cmd_callback;
2586 edit->search->update_fn = edit_search_update_callback;
2587 edit->search_line_type = edit_get_search_line_type (edit->search);
2588 edit_search_fix_search_start_if_selection (edit);
2591 if (edit->found_len && edit->search_start == edit->found_start + 1
2592 && edit_search_options.backwards)
2593 edit->search_start--;
2595 if (edit->found_len && edit->search_start == edit->found_start - 1
2596 && !edit_search_options.backwards)
2597 edit->search_start++;
2599 esm.first = TRUE;
2600 esm.edit = edit;
2601 esm.offset = edit->search_start;
2603 status_msg_init (STATUS_MSG (&esm), _("Search"), 1.0, simple_status_msg_init_cb,
2604 edit_search_status_update_cb, NULL);
2608 gsize len = 0;
2610 if (!editcmd_find (&esm, &len))
2612 if (!(edit->search->error == MC_SEARCH_E_OK ||
2613 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2615 edit_query_dialog (_("Search"), edit->search->error_str);
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_error_dialog (_("Replace"), edit->search->error_str);
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"), _("Search string not found"), 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 = ((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, -1, cp_source);
2793 #else
2794 edit->search = mc_search_new (edit->last_search_string, -1, 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);
3173 e = system (tmp);
3174 g_free (tmp);
3175 if (e != 0)
3177 if (e == -1 || e == 127)
3178 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3179 else
3181 char q[8];
3183 sprintf (q, "%d ", e);
3184 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3185 edit_error_dialog (_("Sort"), tmp);
3186 g_free (tmp);
3188 return -1;
3191 edit->force |= REDRAW_COMPLETELY;
3193 if (edit_block_delete_cmd (edit))
3194 return 1;
3197 vfs_path_t *tmp_vpath;
3199 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3200 edit_insert_file (edit, tmp_vpath);
3201 vfs_path_free (tmp_vpath);
3203 return 0;
3206 /* --------------------------------------------------------------------------------------------- */
3208 * Ask user for a command, execute it and paste its output back to the
3209 * editor.
3213 edit_ext_cmd (WEdit * edit)
3215 char *exp, *tmp, *tmp_edit_temp_file;
3216 int e;
3218 exp =
3219 input_dialog (_("Paste output of external command"),
3220 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, INPUT_LAST_TEXT,
3221 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES
3222 | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
3223 INPUT_COMPLETE_SHELL_ESC);
3225 if (!exp)
3226 return 1;
3228 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3229 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3230 g_free (tmp_edit_temp_file);
3231 e = system (tmp);
3232 g_free (tmp);
3233 g_free (exp);
3235 if (e)
3237 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3238 return -1;
3241 edit->force |= REDRAW_COMPLETELY;
3244 vfs_path_t *tmp_vpath;
3246 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3247 edit_insert_file (edit, tmp_vpath);
3248 vfs_path_free (tmp_vpath);
3250 return 0;
3253 /* --------------------------------------------------------------------------------------------- */
3254 /** if block is 1, a block must be highlighted and the shell command
3255 processes it. If block is 0 the shell command is a straight system
3256 command, that just produces some output which is to be inserted */
3258 void
3259 edit_block_process_cmd (WEdit * edit, int macro_number)
3261 char *fname;
3262 char *macros_fname = NULL;
3264 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3265 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3266 user_menu (edit, macros_fname, 0);
3267 g_free (fname);
3268 g_free (macros_fname);
3269 edit->force |= REDRAW_COMPLETELY;
3272 /* --------------------------------------------------------------------------------------------- */
3274 void
3275 edit_mail_dialog (WEdit * edit)
3277 char *mail_to, *mail_subject, *mail_cc;
3279 quick_widget_t quick_widgets[] = {
3280 /* *INDENT-OFF* */
3281 QUICK_LABEL (N_("mail -s <subject> -c <cc> <to>"), NULL),
3282 QUICK_LABELED_INPUT (N_("To"), input_label_above,
3283 INPUT_LAST_TEXT, "mail-dlg-input-3",
3284 &mail_to, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3285 QUICK_LABELED_INPUT (N_("Subject"), input_label_above,
3286 INPUT_LAST_TEXT, "mail-dlg-input-2",
3287 &mail_subject, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE),
3288 QUICK_LABELED_INPUT (N_("Copies to"), input_label_above,
3289 INPUT_LAST_TEXT, "mail-dlg-input",
3290 &mail_cc, NULL, FALSE, FALSE, INPUT_COMPLETE_USERNAMES),
3291 QUICK_BUTTONS_OK_CANCEL,
3292 QUICK_END
3293 /* *INDENT-ON* */
3296 quick_dialog_t qdlg = {
3297 -1, -1, 50,
3298 N_("Mail"), "[Input Line Keys]",
3299 quick_widgets, NULL, NULL
3302 if (quick_dialog (&qdlg) != B_CANCEL)
3304 pipe_mail (&edit->buffer, mail_to, mail_subject, mail_cc);
3305 g_free (mail_to);
3306 g_free (mail_subject);
3307 g_free (mail_cc);
3311 /* --------------------------------------------------------------------------------------------- */
3313 /*******************/
3314 /* Word Completion */
3315 /*******************/
3318 * Complete current word using regular expression search
3319 * backwards beginning at the current cursor position.
3322 void
3323 edit_complete_word_cmd (WEdit * edit)
3325 gsize i, max_len, word_len = 0, num_compl = 0;
3326 off_t word_start = 0;
3327 GString *match_expr;
3328 GString *compl[MAX_WORD_COMPLETIONS]; /* completions */
3330 /* search start of word to be completed */
3331 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3332 return;
3334 /* prepare match expression */
3335 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3336 match_expr = g_string_new ("(^|\\s+|\\b)");
3337 for (i = 0; i < word_len; i++)
3338 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3339 g_string_append (match_expr,
3340 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
3342 /* collect the possible completions */
3343 /* start search from begin to end of file */
3344 max_len =
3345 edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl,
3346 &num_compl);
3348 if (num_compl > 0)
3350 /* insert completed word if there is only one match */
3351 if (num_compl == 1)
3352 edit_complete_word_insert_recoded_completion (edit, compl[0]->str, word_len);
3353 /* more than one possible completion => ask the user */
3354 else
3356 char *curr_compl;
3358 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3359 /* !!! pressed again the selection dialog pops up, but that !!! */
3360 /* !!! seems to require a further internal state !!! */
3361 /*tty_beep (); */
3363 /* let the user select the preferred completion */
3364 curr_compl = editcmd_dialog_completion_show (edit, max_len,
3365 (GString **) & compl, num_compl);
3367 if (curr_compl != NULL)
3369 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
3370 g_free (curr_compl);
3375 g_string_free (match_expr, TRUE);
3376 /* release memory before return */
3377 for (i = 0; i < num_compl; i++)
3378 g_string_free (compl[i], TRUE);
3381 /* --------------------------------------------------------------------------------------------- */
3383 #ifdef HAVE_CHARSET
3384 void
3385 edit_select_codepage_cmd (WEdit * edit)
3387 if (do_select_codepage ())
3388 edit_set_codeset (edit);
3390 edit->force = REDRAW_PAGE;
3391 widget_redraw (WIDGET (edit));
3393 #endif
3395 /* --------------------------------------------------------------------------------------------- */
3397 void
3398 edit_insert_literal_cmd (WEdit * edit)
3400 int char_for_insertion;
3402 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3403 _("Press any key:"), FALSE);
3404 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3407 /* --------------------------------------------------------------------------------------------- */
3409 void
3410 edit_begin_end_macro_cmd (WEdit * edit)
3412 /* edit is a pointer to the widget */
3413 if (edit != NULL)
3415 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3416 edit_execute_key_command (edit, command, -1);
3420 /* --------------------------------------------------------------------------------------------- */
3422 void
3423 edit_begin_end_repeat_cmd (WEdit * edit)
3425 /* edit is a pointer to the widget */
3426 if (edit != NULL)
3428 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3429 edit_execute_key_command (edit, command, -1);
3433 /* --------------------------------------------------------------------------------------------- */
3435 gboolean
3436 edit_load_forward_cmd (WEdit * edit)
3438 if (edit->modified
3439 && edit_query_dialog2 (_("Warning"),
3440 _("Current text was modified without a file save.\n"
3441 "Continue discards these changes"), _("C&ontinue"),
3442 _("&Cancel")) == 1)
3444 edit->force |= REDRAW_COMPLETELY;
3445 return TRUE;
3448 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3449 return FALSE;
3451 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3452 return FALSE;
3454 edit_stack_iterator++;
3455 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3456 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3457 edit_history_moveto[edit_stack_iterator].line);
3459 return FALSE;
3462 /* --------------------------------------------------------------------------------------------- */
3464 gboolean
3465 edit_load_back_cmd (WEdit * edit)
3467 if (edit->modified
3468 && edit_query_dialog2 (_("Warning"),
3469 _("Current text was modified without a file save.\n"
3470 "Continue discards these changes"), _("C&ontinue"),
3471 _("&Cancel")) == 1)
3473 edit->force |= REDRAW_COMPLETELY;
3474 return TRUE;
3477 /* we are in the bottom of the stack, NO WAY! */
3478 if (edit_stack_iterator == 0)
3479 return FALSE;
3481 edit_stack_iterator--;
3482 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3483 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3484 edit_history_moveto[edit_stack_iterator].line);
3486 return FALSE;
3489 /* --------------------------------------------------------------------------------------------- */
3491 void
3492 edit_get_match_keyword_cmd (WEdit * edit)
3494 gsize word_len = 0, max_len = 0;
3495 int num_def = 0;
3496 gsize i;
3497 off_t word_start = 0;
3498 GString *match_expr;
3499 char *path = NULL;
3500 char *ptr = NULL;
3501 char *tagfile = NULL;
3503 etags_hash_t def_hash[MAX_DEFINITIONS];
3505 for (i = 0; i < MAX_DEFINITIONS; i++)
3507 def_hash[i].filename = NULL;
3510 /* search start of word to be completed */
3511 if (!edit_find_word_start (&edit->buffer, &word_start, &word_len))
3512 return;
3514 /* prepare match expression */
3515 match_expr = g_string_sized_new (word_len);
3516 for (i = 0; i < word_len; i++)
3517 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
3519 ptr = g_get_current_dir ();
3520 path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL);
3521 g_free (ptr);
3523 /* Recursive search file 'TAGS' in parent dirs */
3526 ptr = g_path_get_dirname (path);
3527 g_free (path);
3528 path = ptr;
3529 g_free (tagfile);
3530 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3531 if (exist_file (tagfile))
3532 break;
3534 while (strcmp (path, PATH_SEP_STR) != 0);
3536 if (tagfile)
3538 num_def =
3539 etags_set_definition_hash (tagfile, path, match_expr->str, (etags_hash_t *) & def_hash);
3540 g_free (tagfile);
3542 g_free (path);
3544 max_len = MAX_WIDTH_DEF_DIALOG;
3545 word_len = 0;
3546 if (num_def > 0)
3548 editcmd_dialog_select_definition_show (edit, match_expr->str, max_len, word_len,
3549 (etags_hash_t *) & def_hash, num_def);
3551 g_string_free (match_expr, TRUE);
3554 /* --------------------------------------------------------------------------------------------- */
3556 #ifdef HAVE_ASPELL
3558 edit_suggest_current_word (WEdit * edit)
3560 gsize cut_len = 0;
3561 gsize word_len = 0;
3562 off_t word_start = 0;
3563 int retval = B_SKIP_WORD;
3564 GString *match_word;
3566 /* search start of word to spell check */
3567 match_word = edit_buffer_get_word_from_pos (&edit->buffer, edit->buffer.curs1, &word_start,
3568 &cut_len);
3569 word_len = match_word->len;
3571 #ifdef HAVE_CHARSET
3572 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3574 GString *tmp_word;
3576 tmp_word = str_convert_to_display (match_word->str);
3577 g_string_free (match_word, TRUE);
3578 match_word = tmp_word;
3580 #endif
3581 if (!aspell_check (match_word->str, (int) word_len))
3583 GArray *suggest;
3584 unsigned int res;
3585 guint i;
3587 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3589 res = aspell_suggest (suggest, match_word->str, (int) word_len);
3590 if (res != 0)
3592 char *new_word = NULL;
3594 edit->found_start = word_start;
3595 edit->found_len = word_len;
3596 edit->force |= REDRAW_PAGE;
3597 edit_scroll_screen_over_cursor (edit);
3598 edit_render_keypress (edit);
3600 retval = spell_dialog_spell_suggest_show (edit, match_word->str, &new_word, suggest);
3601 edit_cursor_move (edit, word_len - cut_len);
3603 if (retval == B_ENTER && new_word != NULL)
3605 char *cp_word;
3607 #ifdef HAVE_CHARSET
3608 if (mc_global.source_codepage >= 0 &&
3609 (mc_global.source_codepage != mc_global.display_codepage))
3611 GString *tmp_word;
3613 tmp_word = str_convert_to_input (new_word);
3614 g_free (new_word);
3615 new_word = g_string_free (tmp_word, FALSE);
3617 #endif
3618 cp_word = new_word;
3619 for (i = 0; i < word_len; i++)
3620 edit_backspace (edit, TRUE);
3621 for (; *new_word; new_word++)
3622 edit_insert (edit, *new_word);
3623 g_free (cp_word);
3625 else if (retval == B_ADD_WORD && match_word != NULL)
3626 aspell_add_to_dict (match_word->str, (int) word_len);
3630 for (i = 0; i < suggest->len; i++)
3632 char *cur_sugg_word;
3634 cur_sugg_word = g_array_index (suggest, char *, i);
3635 g_free (cur_sugg_word);
3637 g_array_free (suggest, TRUE);
3638 edit->found_start = 0;
3639 edit->found_len = 0;
3641 g_string_free (match_word, TRUE);
3642 return retval;
3645 /* --------------------------------------------------------------------------------------------- */
3647 void
3648 edit_spellcheck_file (WEdit * edit)
3650 if (edit->buffer.curs_line > 0)
3652 edit_cursor_move (edit, -edit->buffer.curs1);
3653 edit_move_to_prev_col (edit, 0);
3654 edit_update_curs_row (edit);
3659 int c1, c2;
3661 c2 = edit_buffer_get_current_byte (&edit->buffer);
3665 if (edit->buffer.curs1 >= edit->buffer.size)
3666 return;
3668 c1 = c2;
3669 edit_cursor_move (edit, 1);
3670 c2 = edit_buffer_get_current_byte (&edit->buffer);
3672 while (is_break_char (c1) || is_break_char (c2));
3674 while (edit_suggest_current_word (edit) != B_CANCEL);
3677 /* --------------------------------------------------------------------------------------------- */
3679 void
3680 edit_set_spell_lang (void)
3682 GArray *lang_list;
3684 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3685 if (aspell_get_lang_list (lang_list) != 0)
3687 char *lang;
3689 lang = spell_dialog_lang_list_show (lang_list);
3690 if (lang != NULL)
3691 (void) aspell_set_lang (lang);
3693 aspell_array_clean (lang_list);
3695 #endif /* HAVE_ASPELL */
3697 /* --------------------------------------------------------------------------------------------- */