Handle CK_ShowNumbers action in dialog level instead of widget one.
[midnight-commander.git] / src / editor / editcmd.c
blob045420763020e95650595939d378db4f221fe082
1 /*
2 Editor high level editing commands
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 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>
51 #include <fcntl.h>
53 #include "lib/global.h"
54 #include "lib/tty/tty.h"
55 #include "lib/tty/key.h" /* XCTRL */
56 #include "lib/mcconfig.h"
57 #include "lib/skin.h"
58 #include "lib/strutil.h" /* utf string functions */
59 #include "lib/lock.h"
60 #include "lib/util.h" /* tilde_expand() */
61 #include "lib/vfs/vfs.h"
62 #include "lib/widget.h"
63 #include "lib/charsets.h"
64 #include "lib/event.h" /* mc_event_raise() */
66 #include "src/history.h"
67 #include "src/setup.h" /* option_tab_spacing */
68 #include "src/main.h" /* mactos_t */
69 #include "src/selcodepage.h"
70 #include "src/keybind-defaults.h"
71 #include "src/util.h" /* check_for_default() */
72 #include "src/filemanager/layout.h" /* mc_refresh() */
74 #include "edit-impl.h"
75 #include "editwidget.h"
76 #include "editcmd_dialogs.h"
77 #include "etags.h"
79 /*** global variables ****************************************************************************/
81 /* search and replace: */
82 int search_create_bookmark = FALSE;
84 /* queries on a save */
85 int edit_confirm_save = 1;
87 /*** file scope macro definitions ****************************************************************/
89 #define space_width 1
91 #define TEMP_BUF_LEN 1024
93 #define INPUT_INDEX 9
95 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
96 (and the above) routines to work properly - paul */
98 #define is_digit(x) ((x) >= '0' && (x) <= '9')
100 #define MAIL_DLG_HEIGHT 12
102 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
104 /*** file scope type declarations ****************************************************************/
106 /*** file scope variables ************************************************************************/
108 /*** file scope functions ************************************************************************/
109 /* --------------------------------------------------------------------------------------------- */
111 /* If 0 (quick save) then a) create/truncate <filename> file,
112 b) save to <filename>;
113 if 1 (safe save) then a) save to <tempnam>,
114 b) rename <tempnam> to <filename>;
115 if 2 (do backups) then a) save to <tempnam>,
116 b) rename <filename> to <filename.backup_ext>,
117 c) rename <tempnam> to <filename>. */
119 /* returns 0 on error, -1 on abort */
121 static int
122 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
124 char *p;
125 gchar *tmp;
126 long filelen = 0;
127 int this_save_mode, fd = -1;
128 vfs_path_t *real_filename_vpath;
129 vfs_path_t *savename_vpath = NULL;
130 const char *start_filename;
131 const vfs_path_element_t *vpath_element;
133 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
134 if (vpath_element == NULL)
135 return 0;
137 start_filename = vpath_element->path;
138 if (*start_filename == '\0')
139 return 0;
141 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
143 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
145 else
147 real_filename_vpath = vfs_path_clone (filename_vpath);
150 this_save_mode = option_save_mode;
151 if (this_save_mode != EDIT_QUICK_SAVE)
153 if (!vfs_file_is_local (real_filename_vpath)
154 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
157 * The file does not exists yet, so no safe save or
158 * backup are necessary.
160 this_save_mode = EDIT_QUICK_SAVE;
162 if (fd != -1)
163 mc_close (fd);
166 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
168 int rv;
169 struct stat sb;
171 rv = mc_stat (real_filename_vpath, &sb);
172 if (rv == 0 && sb.st_nlink > 1)
174 rv = edit_query_dialog3 (_("Warning"),
175 _("File has hard-links. Detach before saving?"),
176 _("&Yes"), _("&No"), _("&Cancel"));
177 switch (rv)
179 case 0:
180 this_save_mode = EDIT_SAFE_SAVE;
181 /* fallthrough */
182 case 1:
183 edit->skip_detach_prompt = 1;
184 break;
185 default:
186 vfs_path_free (real_filename_vpath);
187 return -1;
191 /* Prevent overwriting changes from other editor sessions. */
192 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
194 /* The default action is "Cancel". */
195 query_set_sel (1);
197 rv = edit_query_dialog2 (_("Warning"),
198 _("The file has been modified in the meantime. Save anyway?"),
199 _("&Yes"), _("&Cancel"));
200 if (rv != 0)
202 vfs_path_free (real_filename_vpath);
203 return -1;
208 if (this_save_mode != EDIT_QUICK_SAVE)
210 char *savedir, *saveprefix;
212 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
213 if (savedir == NULL)
214 savedir = g_strdup (".");
216 saveprefix = mc_build_filename (savedir, "cooledit", NULL);
217 g_free (savedir);
218 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
219 g_free (saveprefix);
220 if (savename_vpath == NULL)
222 vfs_path_free (real_filename_vpath);
223 return 0;
225 /* FIXME:
226 * Close for now because mc_mkstemps use pure open system call
227 * to create temporary file and it needs to be reopened by
228 * VFS-aware mc_open().
230 close (fd);
232 else
233 savename_vpath = vfs_path_clone (real_filename_vpath);
235 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
236 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
238 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
239 if (fd == -1)
240 goto error_save;
242 /* pipe save */
243 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
244 if (p != NULL)
246 FILE *file;
248 mc_close (fd);
249 file = (FILE *) popen (p, "w");
251 if (file)
253 filelen = edit_write_stream (edit, file);
254 #if 1
255 pclose (file);
256 #else
257 if (pclose (file) != 0)
259 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
260 edit_error_dialog (_("Error"), tmp);
261 g_free (tmp);
262 g_free (p);
263 goto error_save;
265 #endif
267 else
269 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
270 edit_error_dialog (_("Error"), get_sys_error (tmp));
271 g_free (p);
272 g_free (tmp);
273 goto error_save;
275 g_free (p);
277 else if (edit->lb == LB_ASIS)
278 { /* do not change line breaks */
279 long buf;
280 buf = 0;
281 filelen = edit->last_byte;
282 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
284 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
286 mc_close (fd);
287 goto error_save;
289 buf++;
291 if (mc_write
292 (fd, (char *) edit->buffers1[buf],
293 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
295 filelen = -1;
297 else if (edit->curs2)
299 edit->curs2--;
300 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
301 if (mc_write
302 (fd,
303 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
304 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
305 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
307 filelen = -1;
309 else
311 while (--buf >= 0)
313 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
315 filelen = -1;
316 break;
320 edit->curs2++;
322 if (mc_close (fd))
323 goto error_save;
325 /* Update the file information, especially the mtime. */
326 if (mc_stat (savename_vpath, &edit->stat1) == -1)
327 goto error_save;
329 else
330 { /* change line breaks */
331 FILE *file;
332 const vfs_path_element_t *path_element;
334 mc_close (fd);
336 path_element = vfs_path_get_by_index (savename_vpath, -1);
337 file = (FILE *) fopen (path_element->path, "w");
338 if (file != NULL)
340 filelen = edit_write_stream (edit, file);
341 fclose (file);
343 else
345 char *msg;
347 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
348 edit_error_dialog (_("Error"), msg);
349 g_free (msg);
350 goto error_save;
354 if (filelen != edit->last_byte)
355 goto error_save;
357 if (this_save_mode == EDIT_DO_BACKUP)
359 vfs_path_t *tmp_vpath;
360 gboolean ok;
362 #ifdef HAVE_ASSERT_H
363 assert (option_backup_ext != NULL);
364 #endif
365 tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
366 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
367 vfs_path_free (tmp_vpath);
368 if (!ok)
369 goto error_save;
372 if (this_save_mode != EDIT_QUICK_SAVE)
373 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
374 goto error_save;
376 vfs_path_free (real_filename_vpath);
377 vfs_path_free (savename_vpath);
378 return 1;
379 error_save:
380 /* FIXME: Is this safe ?
381 * if (this_save_mode != EDIT_QUICK_SAVE)
382 * mc_unlink (savename);
384 vfs_path_free (real_filename_vpath);
385 vfs_path_free (savename_vpath);
386 return 0;
389 /* --------------------------------------------------------------------------------------------- */
391 static gboolean
392 edit_check_newline (WEdit * edit)
394 return !(option_check_nl_at_eof && edit->last_byte > 0
395 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
396 && edit_query_dialog2 (_("Warning"),
397 _("The file you are saving is not finished with a newline"),
398 _("C&ontinue"), _("&Cancel")));
401 /* --------------------------------------------------------------------------------------------- */
403 static vfs_path_t *
404 edit_get_save_file_as (WEdit * edit)
406 #define DLG_WIDTH 64
407 #define DLG_HEIGHT 14
409 static LineBreaks cur_lb = LB_ASIS;
411 char *filename = vfs_path_to_str (edit->filename_vpath);
412 char *filename_res;
414 const char *lb_names[LB_NAMES] = {
415 N_("&Do not change"),
416 N_("&Unix format (LF)"),
417 N_("&Windows/DOS format (CR LF)"),
418 N_("&Macintosh format (CR)")
421 QuickWidget quick_widgets[] = {
422 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
423 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
424 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
425 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
426 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
427 "save-as", &filename_res),
428 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
429 QUICK_END
432 QuickDialog Quick_options = {
433 DLG_WIDTH, DLG_HEIGHT, -1, -1,
434 N_("Save As"), "[Save File As]",
435 quick_widgets, NULL, NULL, FALSE
438 if (quick_dialog (&Quick_options) != B_CANCEL)
440 char *fname;
441 vfs_path_t *ret_vpath;
443 edit->lb = cur_lb;
444 fname = tilde_expand (filename_res);
445 g_free (filename_res);
446 ret_vpath = vfs_path_from_str (fname);
447 g_free (fname);
448 return ret_vpath;
450 g_free (filename);
452 return NULL;
454 #undef DLG_WIDTH
455 #undef DLG_HEIGHT
458 /* --------------------------------------------------------------------------------------------- */
460 /** returns 1 on success */
462 static int
463 edit_save_cmd (WEdit * edit)
465 int res, save_lock = 0;
467 if (!edit->locked && !edit->delete_file)
468 save_lock = lock_file (edit->filename_vpath);
469 res = edit_save_file (edit, edit->filename_vpath);
471 /* Maintain modify (not save) lock on failure */
472 if ((res > 0 && edit->locked) || save_lock)
473 edit->locked = unlock_file (edit->filename_vpath);
475 /* On failure try 'save as', it does locking on its own */
476 if (!res)
477 return edit_save_as_cmd (edit);
478 edit->force |= REDRAW_COMPLETELY;
479 if (res > 0)
481 edit->delete_file = 0;
482 edit->modified = 0;
485 return 1;
488 /* --------------------------------------------------------------------------------------------- */
490 * Load file content
492 * @param h screen the owner of editor window
493 * @param vpath vfs file path
494 * @return TRUE if file content was successfully loaded, FALSE otherwise
497 static inline gboolean
498 edit_load_file_from_filename (Dlg_head *h, const vfs_path_t *vpath)
500 return edit_add_window (h, h->y + 1, h->x, h->lines - 2, h->cols, vpath, 0);
503 /* --------------------------------------------------------------------------------------------- */
505 static void
506 edit_delete_column_of_text (WEdit * edit)
508 long p, q, r, m1, m2;
509 long b, c, d, n;
511 eval_marks (edit, &m1, &m2);
512 n = edit_move_forward (edit, m1, 0, m2) + 1;
513 c = edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
514 d = edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
515 b = max (min (c, d), min (edit->column1, edit->column2));
516 c = max (c, max (edit->column1, edit->column2));
518 while (n--)
520 r = edit_bol (edit, edit->curs1);
521 p = edit_move_forward3 (edit, r, b, 0);
522 q = edit_move_forward3 (edit, r, c, 0);
523 if (p < m1)
524 p = m1;
525 if (q > m2)
526 q = m2;
527 edit_cursor_move (edit, p - edit->curs1);
528 while (q > p)
530 /* delete line between margins */
531 if (edit_get_byte (edit, edit->curs1) != '\n')
532 edit_delete (edit, 1);
533 q--;
535 if (n)
536 /* move to next line except on the last delete */
537 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
541 /* --------------------------------------------------------------------------------------------- */
542 /** if success return 0 */
544 static int
545 edit_block_delete (WEdit * edit)
547 long count;
548 long start_mark, end_mark;
549 int curs_pos, line_width;
550 long curs_line, c1, c2;
552 if (eval_marks (edit, &start_mark, &end_mark))
553 return 0;
554 if (edit->column_highlight && edit->mark2 < 0)
555 edit_mark_cmd (edit, 0);
556 if ((end_mark - start_mark) > option_max_undo / 2)
558 /* Warning message with a query to continue or cancel the operation */
559 if (edit_query_dialog2
560 (_("Warning"),
562 ("Block is large, you may not be able to undo this action"),
563 _("C&ontinue"), _("&Cancel")))
565 return 1;
568 c1 = min (edit->column1, edit->column2);
569 c2 = max (edit->column1, edit->column2);
570 edit->column1 = c1;
571 edit->column2 = c2;
573 edit_push_markers (edit);
575 curs_line = edit->curs_line;
577 /* calculate line width and cursor position before cut */
578 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
579 edit_eol (edit, edit->curs1));
580 curs_pos = edit->curs_col + edit->over_col;
582 /* move cursor to start of selection */
583 edit_cursor_move (edit, start_mark - edit->curs1);
584 edit_scroll_screen_over_cursor (edit);
585 count = start_mark;
586 if (start_mark < end_mark)
588 if (edit->column_highlight)
590 if (edit->mark2 < 0)
591 edit_mark_cmd (edit, 0);
592 edit_delete_column_of_text (edit);
593 /* move cursor to the saved position */
594 edit_move_to_line (edit, curs_line);
595 /* calculate line width after cut */
596 line_width = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
597 edit_eol (edit, edit->curs1));
598 if (option_cursor_beyond_eol && curs_pos > line_width)
599 edit->over_col = curs_pos - line_width;
601 else
603 while (count < end_mark)
605 edit_delete (edit, 1);
606 count++;
610 edit_set_markers (edit, 0, 0, 0, 0);
611 edit->force |= REDRAW_PAGE;
612 return 0;
615 /* --------------------------------------------------------------------------------------------- */
617 * Get EOL symbol for searching.
619 * @param edit editor object
620 * @return EOL symbol
623 static inline char
624 edit_search_get_current_end_line_char (const WEdit * edit)
626 switch (edit->lb)
628 case LB_MAC:
629 return '\r';
630 default:
631 return '\n';
635 /* --------------------------------------------------------------------------------------------- */
637 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
639 * @param search search object
640 * @return result of checks.
643 static edit_search_line_t
644 edit_get_search_line_type (mc_search_t * search)
646 edit_search_line_t search_line_type = 0;
648 if (search->search_type != MC_SEARCH_T_REGEX)
649 return search_line_type;
651 if (*search->original == '^')
652 search_line_type |= AT_START_LINE;
654 if (search->original[search->original_len - 1] == '$')
655 search_line_type |= AT_END_LINE;
656 return search_line_type;
659 /* --------------------------------------------------------------------------------------------- */
661 * Calculating the start position of next line.
663 * @param edit editor object
664 * @param current_pos current position
665 * @param max_pos max position
666 * @param end_string_symbol end of line symbol
667 * @return start position of next line
670 static off_t
671 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
672 char end_string_symbol)
674 off_t i;
676 for (i = current_pos; i < max_pos; i++)
678 current_pos++;
679 if (edit_get_byte (edit, i) == end_string_symbol)
680 break;
683 return current_pos;
686 /* --------------------------------------------------------------------------------------------- */
688 * Calculating the end position of previous line.
690 * @param edit editor object
691 * @param current_pos current position
692 * @param end_string_symbol end of line symbol
693 * @return end position of previous line
696 static off_t
697 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
699 off_t i;
701 for (i = current_pos - 1; i >= 0; i--)
702 if (edit_get_byte (edit, i) == end_string_symbol)
703 break;
705 return i;
708 /* --------------------------------------------------------------------------------------------- */
710 * Calculating the start position of previous line.
712 * @param edit editor object
713 * @param current_pos current position
714 * @param end_string_symbol end of line symbol
715 * @return start position of previous line
718 static inline off_t
719 edit_calculate_start_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
721 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
722 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
724 return (current_pos + 1);
727 /* --------------------------------------------------------------------------------------------- */
729 * Calculating the start position of current line.
731 * @param edit editor object
732 * @param current_pos current position
733 * @param end_string_symbol end of line symbol
734 * @return start position of current line
737 static inline off_t
738 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
740 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
742 return (current_pos + 1);
745 /* --------------------------------------------------------------------------------------------- */
747 * Fixing (if needed) search start position if 'only in selection' option present.
749 * @param edit editor object
752 static void
753 edit_search_fix_search_start_if_selection (WEdit * edit)
755 long start_mark = 0;
756 long end_mark = 0;
758 if (!edit_search_options.only_in_selection)
759 return;
761 if (eval_marks (edit, &start_mark, &end_mark) != 0)
762 return;
764 if (edit_search_options.backwards)
766 if (edit->search_start > end_mark || edit->search_start <= start_mark)
767 edit->search_start = end_mark;
769 else
771 if (edit->search_start < start_mark || edit->search_start >= end_mark)
772 edit->search_start = start_mark;
776 /* --------------------------------------------------------------------------------------------- */
778 static gboolean
779 editcmd_find (WEdit * edit, gsize * len)
781 off_t search_start = edit->search_start;
782 off_t search_end;
783 long start_mark = 0;
784 long end_mark = edit->last_byte;
785 int mark_res = 0;
786 char end_string_symbol;
788 end_string_symbol = edit_search_get_current_end_line_char (edit);
790 /* prepare for search */
791 if (edit_search_options.only_in_selection)
793 mark_res = eval_marks (edit, &start_mark, &end_mark);
794 if (mark_res != 0)
796 edit->search->error = MC_SEARCH_E_NOTFOUND;
797 edit->search->error_str = g_strdup (_("Search string not found"));
798 return FALSE;
801 /* fix the start and the end of search block positions */
802 if ((edit->search_line_type & AT_START_LINE) != 0
803 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
805 start_mark =
806 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
807 end_string_symbol);
809 if ((edit->search_line_type & AT_END_LINE) != 0
810 && (end_mark - 1 != edit->last_byte
811 || edit_get_byte (edit, end_mark) != end_string_symbol))
813 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
815 if (start_mark >= end_mark)
817 edit->search->error = MC_SEARCH_E_NOTFOUND;
818 edit->search->error_str = g_strdup (_("Search string not found"));
819 return FALSE;
822 else
824 if (edit_search_options.backwards)
825 end_mark = max (1, edit->curs1) - 1;
828 /* search */
829 if (edit_search_options.backwards)
831 /* backward search */
832 search_end = end_mark;
834 if ((edit->search_line_type & AT_START_LINE) != 0)
835 search_start =
836 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
838 while ((int) search_start >= start_mark)
840 if (search_end > (off_t) (search_start + edit->search->original_len)
841 && mc_search_is_fixed_search_str (edit->search))
843 search_end = search_start + edit->search->original_len;
845 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
846 && edit->search->normal_offset == search_start)
848 return TRUE;
852 if ((edit->search_line_type & AT_START_LINE) != 0)
853 search_start =
854 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
855 else
856 search_start--;
858 edit->search->error_str = g_strdup (_("Search string not found"));
860 else
862 /* forward search */
863 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
864 search_start =
865 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
866 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
868 return FALSE;
871 /* --------------------------------------------------------------------------------------------- */
873 static char *
874 edit_replace_cmd__conv_to_display (char *str)
876 #ifdef HAVE_CHARSET
877 GString *tmp;
879 tmp = str_convert_to_display (str);
880 if (tmp != NULL)
882 if (tmp->len != 0)
883 return g_string_free (tmp, FALSE);
884 g_string_free (tmp, TRUE);
886 #endif
887 return g_strdup (str);
890 /* --------------------------------------------------------------------------------------------- */
892 static char *
893 edit_replace_cmd__conv_to_input (char *str)
895 #ifdef HAVE_CHARSET
896 GString *tmp;
898 tmp = str_convert_to_input (str);
899 if (tmp != NULL)
901 if (tmp->len != 0)
902 return g_string_free (tmp, FALSE);
903 g_string_free (tmp, TRUE);
905 #endif
906 return g_strdup (str);
909 /* --------------------------------------------------------------------------------------------- */
911 static void
912 edit_do_search (WEdit * edit)
914 gsize len = 0;
916 if (edit->search == NULL)
917 edit->search_start = edit->curs1;
919 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
921 if (search_create_bookmark)
923 int found = 0, books = 0;
924 long l = 0, l_last = -1;
925 long q = 0;
927 search_create_bookmark = FALSE;
928 book_mark_flush (edit, -1);
930 while (TRUE)
932 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
933 break;
934 if (found == 0)
935 edit->search_start = edit->search->normal_offset;
936 found++;
937 l += edit_count_lines (edit, q, edit->search->normal_offset);
938 if (l != l_last)
940 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
941 books++;
943 l_last = l;
944 q = edit->search->normal_offset + 1;
947 if (found == 0)
948 edit_error_dialog (_("Search"), _("Search string not found"));
949 else
950 edit_cursor_move (edit, edit->search_start - edit->curs1);
952 else
954 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
955 && edit_search_options.backwards)
956 edit->search_start--;
958 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
959 && !edit_search_options.backwards)
960 edit->search_start++;
962 if (editcmd_find (edit, &len))
964 edit->found_start = edit->search_start = edit->search->normal_offset;
965 edit->found_len = len;
966 edit->over_col = 0;
967 edit_cursor_move (edit, edit->search_start - edit->curs1);
968 edit_scroll_screen_over_cursor (edit);
969 if (edit_search_options.backwards)
970 edit->search_start--;
971 else
972 edit->search_start++;
974 else
976 edit->search_start = edit->curs1;
977 if (edit->search->error_str != NULL)
978 edit_error_dialog (_("Search"), edit->search->error_str);
982 edit->force |= REDRAW_COMPLETELY;
983 edit_scroll_screen_over_cursor (edit);
986 /* --------------------------------------------------------------------------------------------- */
988 static void
989 edit_search (WEdit * edit)
991 if (editcmd_dialog_search_show (edit))
993 edit->search_line_type = edit_get_search_line_type (edit->search);
994 edit_search_fix_search_start_if_selection (edit);
995 edit_do_search (edit);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Return a null terminated length of text. Result must be g_free'd */
1002 static unsigned char *
1003 edit_get_block (WEdit * edit, long start, long finish, int *l)
1005 unsigned char *s, *r;
1006 r = s = g_malloc0 (finish - start + 1);
1007 if (edit->column_highlight)
1009 *l = 0;
1010 /* copy from buffer, excluding chars that are out of the column 'margins' */
1011 while (start < finish)
1013 int c;
1014 long x;
1015 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1016 c = edit_get_byte (edit, start);
1017 if ((x >= edit->column1 && x < edit->column2)
1018 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1020 *s++ = c;
1021 (*l)++;
1023 start++;
1026 else
1028 *l = finish - start;
1029 while (start < finish)
1030 *s++ = edit_get_byte (edit, start++);
1032 *s = 0;
1033 return r;
1036 /* --------------------------------------------------------------------------------------------- */
1037 /** copies a block to clipboard file */
1039 static int
1040 edit_save_block_to_clip_file (WEdit * edit, long start, long finish)
1042 int ret;
1043 gchar *tmp;
1044 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1045 ret = edit_save_block (edit, tmp, start, finish);
1046 g_free (tmp);
1047 return ret;
1050 /* --------------------------------------------------------------------------------------------- */
1052 static void
1053 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1055 FILE *p = 0;
1056 char *s;
1058 to = name_quote (to, 0);
1059 subject = name_quote (subject, 0);
1060 cc = name_quote (cc, 0);
1061 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1062 g_free (to);
1063 g_free (subject);
1064 g_free (cc);
1066 if (s)
1068 p = popen (s, "w");
1069 g_free (s);
1072 if (p)
1074 long i;
1075 for (i = 0; i < edit->last_byte; i++)
1076 fputc (edit_get_byte (edit, i), p);
1077 pclose (p);
1081 /* --------------------------------------------------------------------------------------------- */
1083 static gboolean
1084 is_break_char (char c)
1086 return (isspace (c) || strchr ("{}[]()<>=|/\\!?~-+`'\",.;:#$%^&*", c));
1089 /* --------------------------------------------------------------------------------------------- */
1090 /** find first character of current word */
1092 static int
1093 edit_find_word_start (WEdit * edit, long *word_start, gsize * word_len)
1095 int c, last;
1096 gsize i;
1098 /* return if at begin of file */
1099 if (edit->curs1 <= 0)
1100 return 0;
1102 c = (unsigned char) edit_get_byte (edit, edit->curs1 - 1);
1103 /* return if not at end or in word */
1104 if (is_break_char (c))
1105 return 0;
1107 /* search start of word to be completed */
1108 for (i = 2;; i++)
1110 /* return if at begin of file */
1111 if ((gsize) edit->curs1 < i)
1112 return 0;
1114 last = c;
1115 c = (unsigned char) edit_get_byte (edit, edit->curs1 - i);
1117 if (is_break_char (c))
1119 /* return if word starts with digit */
1120 if (isdigit (last))
1121 return 0;
1123 *word_start = edit->curs1 - (i - 1); /* start found */
1124 *word_len = i - 1;
1125 break;
1128 /* success */
1129 return 1;
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Get current word under cursor
1136 * @param edit editor object
1137 * @param srch mc_search object
1138 * @param word_start start word position
1140 * @return newly allocated string or NULL if no any words under cursor
1143 static char *
1144 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, long word_start)
1146 gsize len = 0, i;
1147 GString *temp;
1149 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1150 return NULL;
1152 temp = g_string_sized_new (len);
1154 for (i = 0; i < len; i++)
1156 int chr;
1158 chr = edit_get_byte (edit, word_start + i);
1159 if (!isspace (chr))
1160 g_string_append_c (temp, chr);
1163 return g_string_free (temp, temp->len == 0);
1166 /* --------------------------------------------------------------------------------------------- */
1167 /** collect the possible completions */
1169 static gsize
1170 edit_collect_completions (WEdit * edit, long word_start, gsize word_len,
1171 char *match_expr, struct selection *compl, gsize * num)
1173 gsize len = 0;
1174 gsize max_len = 0;
1175 gsize i;
1176 int skip;
1177 GString *temp;
1178 mc_search_t *srch;
1179 long last_byte, start = -1;
1180 char *current_word;
1182 srch = mc_search_new (match_expr, -1);
1183 if (srch == NULL)
1184 return 0;
1186 if (mc_config_get_bool
1187 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1189 last_byte = edit->last_byte;
1191 else
1193 last_byte = word_start;
1196 srch->search_type = MC_SEARCH_T_REGEX;
1197 srch->is_case_sensitive = TRUE;
1198 srch->search_fn = edit_search_cmd_callback;
1200 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1202 temp = g_string_new ("");
1204 /* collect max MAX_WORD_COMPLETIONS completions */
1205 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1207 g_string_set_size (temp, 0);
1208 start = srch->normal_offset;
1210 /* add matched completion if not yet added */
1211 for (i = 0; i < len; i++)
1213 skip = edit_get_byte (edit, start + i);
1214 if (isspace (skip))
1215 continue;
1217 /* skip current word */
1218 if (start + (long) i == word_start)
1219 break;
1221 g_string_append_c (temp, skip);
1224 if (temp->len == 0)
1225 continue;
1227 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1228 continue;
1230 skip = 0;
1232 for (i = 0; i < *num; i++)
1234 if (strncmp
1235 ((char *) &compl[i].text[word_len],
1236 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1238 struct selection this = compl[i];
1239 for (++i; i < *num; i++)
1241 compl[i - 1] = compl[i];
1243 compl[*num - 1] = this;
1244 skip = 1;
1245 break; /* skip it, already added */
1248 if (skip != 0)
1249 continue;
1251 if (*num == MAX_WORD_COMPLETIONS)
1253 g_free (compl[0].text);
1254 for (i = 1; i < *num; i++)
1256 compl[i - 1] = compl[i];
1258 (*num)--;
1260 #ifdef HAVE_CHARSET
1262 GString *recoded;
1263 recoded = str_convert_to_display (temp->str);
1265 if (recoded && recoded->len)
1266 g_string_assign (temp, recoded->str);
1268 g_string_free (recoded, TRUE);
1270 #endif
1271 compl[*num].text = g_strdup (temp->str);
1272 compl[*num].len = temp->len;
1273 (*num)++;
1274 start += len;
1276 /* note the maximal length needed for the completion dialog */
1277 if (len > max_len)
1278 max_len = len;
1281 mc_search_free (srch);
1282 g_string_free (temp, TRUE);
1283 g_free (current_word);
1285 return max_len;
1288 /* --------------------------------------------------------------------------------------------- */
1290 static void
1291 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1292 long *start_pos, long *end_pos, int *col1, int *col2)
1294 long cursor;
1295 int i, col;
1297 cursor = edit->curs1;
1298 col = edit_get_col (edit);
1300 for (i = 0; i < size; i++)
1302 if (data[i] != '\n')
1303 edit_insert (edit, data[i]);
1304 else
1305 { /* fill in and move to next line */
1306 int l;
1307 long p;
1309 if (edit_get_byte (edit, edit->curs1) != '\n')
1311 l = width - (edit_get_col (edit) - col);
1312 while (l > 0)
1314 edit_insert (edit, ' ');
1315 l -= space_width;
1318 for (p = edit->curs1;; p++)
1320 if (p == edit->last_byte)
1322 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1323 edit_insert_ahead (edit, '\n');
1324 p++;
1325 break;
1327 if (edit_get_byte (edit, p) == '\n')
1329 p++;
1330 break;
1333 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1334 l = col - edit_get_col (edit);
1335 while (l >= space_width)
1337 edit_insert (edit, ' ');
1338 l -= space_width;
1343 *col1 = col;
1344 *col2 = col + width;
1345 *start_pos = cursor;
1346 *end_pos = edit->curs1;
1347 edit_cursor_move (edit, cursor - edit->curs1);
1350 /* --------------------------------------------------------------------------------------------- */
1352 static int
1353 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1355 const macros_t *m1 = (const macros_t *) macro1;
1356 const macros_t *m2 = (const macros_t *) macro2;
1358 return m1->hotkey - m2->hotkey;
1361 /* --------------------------------------------------------------------------------------------- */
1363 static void
1364 edit_macro_sort_by_hotkey (void)
1366 if (macros_list != NULL && macros_list->len != 0)
1367 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1370 /* --------------------------------------------------------------------------------------------- */
1372 static gboolean
1373 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1375 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1376 macros_t *result;
1377 macros_t search_macro;
1379 (void) edit;
1381 search_macro.hotkey = hotkey;
1382 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1383 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1385 if (result != NULL && result->macro != NULL)
1387 *indx = (result - array_start);
1388 *macros = result;
1389 return TRUE;
1391 *indx = 0;
1392 return FALSE;
1395 /* --------------------------------------------------------------------------------------------- */
1396 /** returns FALSE on error */
1398 static gboolean
1399 edit_delete_macro (WEdit * edit, int hotkey)
1401 mc_config_t *macros_config = NULL;
1402 const char *section_name = "editor";
1403 gchar *macros_fname;
1404 guint indx;
1405 char *skeyname;
1406 const macros_t *macros = NULL;
1408 /* clear array of actions for current hotkey */
1409 while (edit_get_macro (edit, hotkey, &macros, &indx))
1411 if (macros->macro != NULL)
1412 g_array_free (macros->macro, TRUE);
1413 macros = NULL;
1414 g_array_remove_index (macros_list, indx);
1415 edit_macro_sort_by_hotkey ();
1418 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1419 macros_config = mc_config_init (macros_fname);
1420 g_free (macros_fname);
1422 if (macros_config == NULL)
1423 return FALSE;
1425 skeyname = lookup_key_by_code (hotkey);
1426 while (mc_config_del_key (macros_config, section_name, skeyname))
1428 g_free (skeyname);
1429 mc_config_save_file (macros_config, NULL);
1430 mc_config_deinit (macros_config);
1431 return TRUE;
1434 /* --------------------------------------------------------------------------------------------- */
1436 * Callback for the iteration of objects in the 'editors' array.
1437 * Toggle syntax highlighting in editor object.
1439 * @param data probably WEdit object
1440 * @param user_data unused
1443 static void
1444 edit_syntax_onoff_cb (void *data, void *user_data)
1446 (void) user_data;
1448 if (edit_widget_is_editor ((const Widget *) data))
1450 WEdit *edit = (WEdit *) data;
1452 if (option_syntax_highlighting)
1453 edit_load_syntax (edit, NULL, edit->syntax_type);
1454 edit->force |= REDRAW_PAGE;
1458 /* --------------------------------------------------------------------------------------------- */
1460 * Callback for the iteration of objects in the 'editors' array.
1461 * Redraw editor object.
1463 * @param data probably WEdit object
1464 * @param user_data unused
1467 static void
1468 edit_redraw_page_cb (void *data, void *user_data)
1470 (void) user_data;
1472 if (edit_widget_is_editor ((const Widget *) data))
1473 ((WEdit *) data)->force |= REDRAW_PAGE;
1476 /* --------------------------------------------------------------------------------------------- */
1477 /*** public functions ****************************************************************************/
1478 /* --------------------------------------------------------------------------------------------- */
1480 void
1481 edit_refresh_cmd (void)
1483 clr_scr ();
1484 repaint_screen ();
1485 tty_keypad (TRUE);
1488 /* --------------------------------------------------------------------------------------------- */
1490 * Toggle syntax highlighting in all editor windows.
1492 * @param h root widget for all windows
1495 void
1496 edit_syntax_onoff_cmd (Dlg_head * h)
1498 option_syntax_highlighting = !option_syntax_highlighting;
1499 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1500 dlg_redraw (h);
1503 /* --------------------------------------------------------------------------------------------- */
1505 * Toggle tabs showing in all editor windows.
1507 * @param h root widget for all windows
1510 void
1511 edit_show_tabs_tws_cmd (Dlg_head * h)
1513 enable_show_tabs_tws = !enable_show_tabs_tws;
1514 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1515 dlg_redraw (h);
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Toggle right margin showing in all editor windows.
1522 * @param h root widget for all windows
1525 void
1526 edit_show_margin_cmd (Dlg_head * h)
1528 show_right_margin = !show_right_margin;
1529 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1530 dlg_redraw (h);
1533 /* --------------------------------------------------------------------------------------------- */
1535 * Toggle line numbers showing in all editor windows.
1537 * @param h root widget for all windows
1540 void
1541 edit_show_numbers_cmd (Dlg_head * h)
1543 option_line_state = !option_line_state;
1544 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1545 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1546 dlg_redraw (h);
1549 /* --------------------------------------------------------------------------------------------- */
1551 void
1552 edit_save_mode_cmd (void)
1554 /* diaog sizes */
1555 const int DLG_X = 38;
1556 const int DLG_Y = 13;
1558 char *str_result;
1560 const char *str[] = {
1561 N_("&Quick save"),
1562 N_("&Safe save"),
1563 N_("&Do backups with following extension:")
1566 QuickWidget widgets[] = {
1567 /* 0 */
1568 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1569 /* 1 */
1570 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1571 /* 2 */
1572 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1573 /* 3 */
1574 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1575 /* 4 */
1576 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1577 QUICK_END
1580 QuickDialog dialog = {
1581 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1582 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1585 size_t i;
1586 size_t maxlen = 0;
1587 size_t w0, w1, b_len, w3;
1589 #ifdef HAVE_ASSERT_H
1590 assert (option_backup_ext != NULL);
1591 #endif
1593 /* OK/Cancel buttons */
1594 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1595 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1596 b_len = w0 + w1 + 3;
1598 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1600 w3 = 0;
1601 for (i = 0; i < 3; i++)
1603 #ifdef ENABLE_NLS
1604 str[i] = _(str[i]);
1605 #endif
1606 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1609 maxlen = max (maxlen, w3 + 4);
1611 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1613 widgets[3].u.input.len = w3;
1614 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1615 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1617 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1618 widgets[i].x_divisions = dialog.xlen;
1620 if (quick_dialog (&dialog) != B_CANCEL)
1622 g_free (option_backup_ext);
1623 option_backup_ext = str_result;
1627 /* --------------------------------------------------------------------------------------------- */
1629 void
1630 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1632 vfs_path_free (edit->filename_vpath);
1633 edit->filename_vpath = vfs_path_clone (name_vpath);
1635 if (edit->dir_vpath == NULL)
1636 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1639 /* --------------------------------------------------------------------------------------------- */
1640 /* Here we want to warn the users of overwriting an existing file,
1641 but only if they have made a change to the filename */
1642 /* returns 1 on success */
1644 edit_save_as_cmd (WEdit * edit)
1646 /* This heads the 'Save As' dialog box */
1647 vfs_path_t *exp_vpath;
1648 int save_lock = 0;
1649 int different_filename = 0;
1651 if (!edit_check_newline (edit))
1652 return 0;
1654 exp_vpath = edit_get_save_file_as (edit);
1655 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1657 if (exp_vpath != NULL)
1659 if (vfs_path_len (exp_vpath) == 0)
1660 goto ret;
1661 else
1663 int rv;
1665 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1667 int file;
1668 struct stat sb;
1670 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1672 edit_error_dialog (_("Save as"),
1673 get_sys_error (_
1674 ("Cannot save: destination is not a regular file")));
1675 goto ret;
1678 different_filename = 1;
1679 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1681 if (file != -1)
1683 /* the file exists */
1684 mc_close (file);
1685 /* Overwrite the current file or cancel the operation */
1686 if (edit_query_dialog2
1687 (_("Warning"),
1688 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1689 goto ret;
1691 else
1693 edit->stat1.st_mode |= S_IWUSR;
1695 save_lock = lock_file (exp_vpath);
1697 else
1699 /* filenames equal, check if already locked */
1700 if (!edit->locked && !edit->delete_file)
1701 save_lock = lock_file (exp_vpath);
1704 if (different_filename)
1707 * Allow user to write into saved (under another name) file
1708 * even if original file had r/o user permissions.
1710 edit->stat1.st_mode |= S_IWRITE;
1713 rv = edit_save_file (edit, exp_vpath);
1714 switch (rv)
1716 case 1:
1717 /* Succesful, so unlock both files */
1718 if (different_filename)
1720 if (save_lock)
1721 unlock_file (exp_vpath);
1722 if (edit->locked)
1723 edit->locked = unlock_file (edit->filename_vpath);
1725 else
1727 if (edit->locked || save_lock)
1728 edit->locked = unlock_file (edit->filename_vpath);
1731 edit_set_filename (edit, exp_vpath);
1732 if (edit->lb != LB_ASIS)
1733 edit_reload (edit, exp_vpath);
1734 edit->modified = 0;
1735 edit->delete_file = 0;
1736 if (different_filename)
1737 edit_load_syntax (edit, NULL, edit->syntax_type);
1738 vfs_path_free (exp_vpath);
1739 edit->force |= REDRAW_COMPLETELY;
1740 return 1;
1741 default:
1742 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1743 /* fallthrough */
1744 case -1:
1745 /* Failed, so maintain modify (not save) lock */
1746 if (save_lock)
1747 unlock_file (exp_vpath);
1748 break;
1753 ret:
1754 vfs_path_free (exp_vpath);
1755 edit->force |= REDRAW_COMPLETELY;
1756 return 0;
1759 /* {{{ Macro stuff starts here */
1760 /* --------------------------------------------------------------------------------------------- */
1762 void
1763 edit_delete_macro_cmd (WEdit * edit)
1765 int hotkey;
1767 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), 1);
1769 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1770 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1773 /* --------------------------------------------------------------------------------------------- */
1775 /** returns FALSE on error */
1776 gboolean
1777 edit_execute_macro (WEdit * edit, int hotkey)
1779 gboolean res = FALSE;
1781 if (hotkey != 0)
1783 const macros_t *macros;
1784 guint indx;
1786 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1787 macros->macro != NULL && macros->macro->len != 0)
1789 guint i;
1791 edit->force |= REDRAW_PAGE;
1793 for (i = 0; i < macros->macro->len; i++)
1795 const macro_action_t *m_act;
1797 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1798 edit_execute_cmd (edit, m_act->action, m_act->ch);
1799 res = TRUE;
1804 return res;
1807 /* --------------------------------------------------------------------------------------------- */
1809 /** returns FALSE on error */
1810 gboolean
1811 edit_store_macro_cmd (WEdit * edit)
1813 int i;
1814 int hotkey;
1815 GString *marcros_string;
1816 mc_config_t *macros_config = NULL;
1817 const char *section_name = "editor";
1818 gchar *macros_fname;
1819 GArray *macros; /* current macro */
1820 int tmp_act;
1821 gboolean have_macro = FALSE;
1822 char *skeyname = NULL;
1824 hotkey = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
1825 if (hotkey == ESC_CHAR)
1826 return FALSE;
1828 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1830 /* return FALSE if try assign macro into restricted hotkeys */
1831 if (tmp_act == CK_MacroStartRecord
1832 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1833 return FALSE;
1835 edit_delete_macro (edit, hotkey);
1837 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1838 macros_config = mc_config_init (macros_fname);
1839 g_free (macros_fname);
1841 if (macros_config == NULL)
1842 return FALSE;
1844 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1846 marcros_string = g_string_sized_new (250);
1847 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1849 skeyname = lookup_key_by_code (hotkey);
1851 for (i = 0; i < macro_index; i++)
1853 macro_action_t m_act;
1854 const char *action_name;
1856 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1858 if (action_name == NULL)
1859 break;
1861 m_act.action = record_macro_buf[i].action;
1862 m_act.ch = record_macro_buf[i].ch;
1863 g_array_append_val (macros, m_act);
1864 have_macro = TRUE;
1865 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1866 (int) record_macro_buf[i].ch);
1868 if (have_macro)
1870 macros_t macro;
1871 macro.hotkey = hotkey;
1872 macro.macro = macros;
1873 g_array_append_val (macros_list, macro);
1874 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1876 else
1877 mc_config_del_key (macros_config, section_name, skeyname);
1879 g_free (skeyname);
1880 edit_macro_sort_by_hotkey ();
1882 g_string_free (marcros_string, TRUE);
1883 mc_config_save_file (macros_config, NULL);
1884 mc_config_deinit (macros_config);
1885 return TRUE;
1888 /* --------------------------------------------------------------------------------------------- */
1890 gboolean
1891 edit_repeat_macro_cmd (WEdit * edit)
1893 int i, j;
1894 char *f;
1895 long count_repeat;
1896 char *error = NULL;
1898 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1899 if (f == NULL || *f == '\0')
1901 g_free (f);
1902 return FALSE;
1905 count_repeat = strtol (f, &error, 0);
1907 if (*error != '\0')
1909 g_free (f);
1910 return FALSE;
1913 g_free (f);
1915 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1916 edit->force |= REDRAW_PAGE;
1918 for (j = 0; j < count_repeat; j++)
1919 for (i = 0; i < macro_index; i++)
1920 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1921 edit_update_screen (edit);
1922 return TRUE;
1925 /* --------------------------------------------------------------------------------------------- */
1926 /** return FALSE on error */
1928 gboolean
1929 edit_load_macro_cmd (WEdit * edit)
1931 mc_config_t *macros_config = NULL;
1932 gchar **profile_keys, **keys;
1933 gchar **values, **curr_values;
1934 gsize len, values_len;
1935 const char *section_name = "editor";
1936 gchar *macros_fname;
1937 int hotkey;
1939 (void) edit;
1941 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1942 macros_config = mc_config_init (macros_fname);
1943 g_free (macros_fname);
1945 if (macros_config == NULL)
1946 return FALSE;
1948 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1949 while (*profile_keys != NULL)
1951 gboolean have_macro;
1952 GArray *macros;
1953 macros_t macro;
1955 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1957 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1958 *profile_keys, &values_len);
1959 hotkey = lookup_key (*profile_keys, NULL);
1960 have_macro = FALSE;
1962 while (*curr_values != NULL && *curr_values[0] != '\0')
1964 char **macro_pair = NULL;
1966 macro_pair = g_strsplit (*curr_values, ":", 2);
1968 if (macro_pair != NULL)
1970 macro_action_t m_act;
1971 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1972 m_act.action = 0;
1973 else
1975 m_act.action = keybind_lookup_action (macro_pair[0]);
1976 g_free (macro_pair[0]);
1977 macro_pair[0] = NULL;
1979 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1980 m_act.ch = -1;
1981 else
1983 m_act.ch = strtol (macro_pair[1], NULL, 0);
1984 g_free (macro_pair[1]);
1985 macro_pair[1] = NULL;
1987 if (m_act.action != 0)
1989 /* a shell command */
1990 if ((m_act.action / CK_PipeBlock (0)) == 1)
1992 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
1993 m_act.ch = -1;
1995 g_array_append_val (macros, m_act);
1996 have_macro = TRUE;
1998 g_strfreev (macro_pair);
1999 macro_pair = NULL;
2001 curr_values++;
2003 if (have_macro)
2005 macro.hotkey = hotkey;
2006 macro.macro = macros;
2007 g_array_append_val (macros_list, macro);
2009 profile_keys++;
2010 g_strfreev (values);
2012 g_strfreev (keys);
2013 mc_config_deinit (macros_config);
2014 edit_macro_sort_by_hotkey ();
2015 return TRUE;
2018 /* }}} Macro stuff end here */
2020 /* --------------------------------------------------------------------------------------------- */
2021 /** returns 1 on success */
2024 edit_save_confirm_cmd (WEdit * edit)
2026 gchar *f = NULL;
2028 if (edit->filename_vpath == NULL)
2029 return edit_save_as_cmd (edit);
2031 if (!edit_check_newline (edit))
2032 return 0;
2034 if (edit_confirm_save)
2036 char *filename;
2037 gboolean ok;
2039 filename = vfs_path_to_str (edit->filename_vpath);
2040 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2041 g_free (filename);
2042 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2043 g_free (f);
2044 if (!ok)
2045 return 0;
2047 return edit_save_cmd (edit);
2050 /* --------------------------------------------------------------------------------------------- */
2052 * Ask file to edit and load it.
2054 * @returns TRUE on success or cancel of ask.
2057 gboolean
2058 edit_load_cmd (Dlg_head * h)
2060 char *exp;
2061 gboolean ret = TRUE; /* possible cancel */
2063 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2064 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2066 if (exp != NULL && *exp != '\0')
2068 vfs_path_t *exp_vpath;
2070 exp_vpath = vfs_path_from_str (exp);
2071 ret = edit_load_file_from_filename (h, exp_vpath);
2072 vfs_path_free (exp_vpath);
2075 g_free (exp);
2077 return ret;
2080 /* --------------------------------------------------------------------------------------------- */
2082 * Load syntax file to edit.
2084 * @returns TRUE on success
2087 gboolean
2088 edit_load_syntax_file (Dlg_head * h)
2090 vfs_path_t *extdir_vpath;
2091 int dir = 0;
2092 gboolean ret = FALSE;
2094 if (geteuid () == 0)
2095 dir = query_dialog (_("Syntax file edit"),
2096 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2097 _("&User"), _("&System wide"));
2099 extdir_vpath =
2100 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2101 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2103 vfs_path_free (extdir_vpath);
2104 extdir_vpath =
2105 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2108 if (dir == 0)
2110 vfs_path_t *user_syntax_file_vpath;
2112 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2113 check_for_default (extdir_vpath, user_syntax_file_vpath);
2114 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2115 vfs_path_free (user_syntax_file_vpath);
2117 else if (dir == 1)
2118 ret = edit_load_file_from_filename (h, extdir_vpath);
2120 vfs_path_free (extdir_vpath);
2122 return ret;
2125 /* --------------------------------------------------------------------------------------------- */
2127 * Load menu file to edit.
2129 * @returns TRUE on success
2132 gboolean
2133 edit_load_menu_file (Dlg_head * h)
2135 vfs_path_t *buffer_vpath;
2136 vfs_path_t *menufile_vpath;
2137 int dir;
2138 gboolean ret;
2140 dir = query_dialog (_("Menu edit"),
2141 _("Which menu file do you want to edit?"), D_NORMAL,
2142 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2144 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2145 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2147 vfs_path_free (menufile_vpath);
2148 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2151 switch (dir)
2153 case 0:
2154 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2155 check_for_default (menufile_vpath, buffer_vpath);
2156 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2157 break;
2159 case 1:
2160 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2161 check_for_default (menufile_vpath, buffer_vpath);
2162 break;
2164 case 2:
2165 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2166 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2168 vfs_path_free (buffer_vpath);
2169 buffer_vpath =
2170 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2172 break;
2174 default:
2175 vfs_path_free (menufile_vpath);
2176 return FALSE;
2179 ret = edit_load_file_from_filename (h, buffer_vpath);
2181 vfs_path_free (buffer_vpath);
2182 vfs_path_free (menufile_vpath);
2184 return ret;
2187 /* --------------------------------------------------------------------------------------------- */
2189 * Close window with opened file.
2191 * @returns TRUE if file was closed.
2194 gboolean
2195 edit_close_cmd (WEdit * edit)
2197 gboolean ret;
2199 ret = (edit != NULL) && edit_ok_to_exit (edit);
2201 if (ret)
2203 Dlg_head *h = ((Widget *) edit)->owner;
2205 if (edit->locked != 0)
2206 unlock_file (edit->filename_vpath);
2208 del_widget (edit);
2210 if (edit_widget_is_editor ((Widget *) h->current->data))
2211 edit = (WEdit *) h->current->data;
2212 else
2214 edit = find_editor (h);
2215 if (edit != NULL)
2216 dlg_set_top_widget (edit);
2220 if (edit != NULL)
2221 edit->force |= REDRAW_COMPLETELY;
2223 return ret;
2226 /* --------------------------------------------------------------------------------------------- */
2228 if mark2 is -1 then marking is from mark1 to the cursor.
2229 Otherwise its between the markers. This handles this.
2230 Returns 1 if no text is marked.
2234 eval_marks (WEdit * edit, long *start_mark, long *end_mark)
2236 if (edit->mark1 != edit->mark2)
2238 long start_bol, start_eol;
2239 long end_bol, end_eol;
2240 long col1, col2;
2241 long diff1, diff2;
2242 long end_mark_curs;
2244 if (edit->end_mark_curs < 0)
2245 end_mark_curs = edit->curs1;
2246 else
2247 end_mark_curs = edit->end_mark_curs;
2249 if (edit->mark2 >= 0)
2251 *start_mark = min (edit->mark1, edit->mark2);
2252 *end_mark = max (edit->mark1, edit->mark2);
2254 else
2256 *start_mark = min (edit->mark1, end_mark_curs);
2257 *end_mark = max (edit->mark1, end_mark_curs);
2258 edit->column2 = edit->curs_col + edit->over_col;
2261 if (edit->column_highlight
2262 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2263 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2265 start_bol = edit_bol (edit, *start_mark);
2266 start_eol = edit_eol (edit, start_bol - 1) + 1;
2267 end_bol = edit_bol (edit, *end_mark);
2268 end_eol = edit_eol (edit, *end_mark);
2269 col1 = min (edit->column1, edit->column2);
2270 col2 = max (edit->column1, edit->column2);
2272 diff1 =
2273 edit_move_forward3 (edit, start_bol, col2, 0) - edit_move_forward3 (edit, start_bol,
2274 col1, 0);
2275 diff2 =
2276 edit_move_forward3 (edit, end_bol, col2, 0) - edit_move_forward3 (edit, end_bol,
2277 col1, 0);
2279 *start_mark -= diff1;
2280 *end_mark += diff2;
2281 *start_mark = max (*start_mark, start_eol);
2282 *end_mark = min (*end_mark, end_eol);
2284 return 0;
2286 else
2288 *start_mark = *end_mark = 0;
2289 edit->column2 = edit->column1 = 0;
2290 return 1;
2294 /* --------------------------------------------------------------------------------------------- */
2296 void
2297 edit_insert_over (WEdit * edit)
2299 int i;
2301 for (i = 0; i < edit->over_col; i++)
2303 edit_insert (edit, ' ');
2305 edit->over_col = 0;
2308 /* --------------------------------------------------------------------------------------------- */
2311 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2312 long *start_pos, long *end_pos, int *col1, int *col2)
2314 long cursor;
2315 int col;
2316 int blocklen = -1, width = 0;
2317 unsigned char *data;
2319 cursor = edit->curs1;
2320 col = edit_get_col (edit);
2321 data = g_malloc0 (TEMP_BUF_LEN);
2323 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2325 int i;
2326 for (width = 0; width < blocklen; width++)
2328 if (data[width] == '\n')
2329 break;
2331 for (i = 0; i < blocklen; i++)
2333 if (data[i] == '\n')
2334 { /* fill in and move to next line */
2335 int l;
2336 long p;
2337 if (edit_get_byte (edit, edit->curs1) != '\n')
2339 l = width - (edit_get_col (edit) - col);
2340 while (l > 0)
2342 edit_insert (edit, ' ');
2343 l -= space_width;
2346 for (p = edit->curs1;; p++)
2348 if (p == edit->last_byte)
2350 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2351 edit_insert_ahead (edit, '\n');
2352 p++;
2353 break;
2355 if (edit_get_byte (edit, p) == '\n')
2357 p++;
2358 break;
2361 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2362 l = col - edit_get_col (edit);
2363 while (l >= space_width)
2365 edit_insert (edit, ' ');
2366 l -= space_width;
2368 continue;
2370 edit_insert (edit, data[i]);
2373 *col1 = col;
2374 *col2 = col + width;
2375 *start_pos = cursor;
2376 *end_pos = edit->curs1;
2377 edit_cursor_move (edit, cursor - edit->curs1);
2378 g_free (data);
2380 return blocklen;
2383 /* --------------------------------------------------------------------------------------------- */
2385 void
2386 edit_block_copy_cmd (WEdit * edit)
2388 long start_mark, end_mark, current = edit->curs1;
2389 long col_delta = 0;
2390 long mark1, mark2;
2391 int c1, c2;
2392 int size;
2393 unsigned char *copy_buf;
2395 edit_update_curs_col (edit);
2396 if (eval_marks (edit, &start_mark, &end_mark))
2397 return;
2399 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2401 /* all that gets pushed are deletes hence little space is used on the stack */
2403 edit_push_markers (edit);
2405 if (edit->column_highlight)
2407 col_delta = abs (edit->column2 - edit->column1);
2408 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2410 else
2412 while (size--)
2413 edit_insert_ahead (edit, copy_buf[size]);
2416 g_free (copy_buf);
2417 edit_scroll_screen_over_cursor (edit);
2419 if (edit->column_highlight)
2420 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2421 else if (start_mark < current && end_mark > current)
2422 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2424 edit->force |= REDRAW_PAGE;
2428 /* --------------------------------------------------------------------------------------------- */
2430 void
2431 edit_block_move_cmd (WEdit * edit)
2433 long current;
2434 unsigned char *copy_buf = NULL;
2435 long start_mark, end_mark;
2436 long line;
2438 if (eval_marks (edit, &start_mark, &end_mark))
2439 return;
2441 line = edit->curs_line;
2442 if (edit->mark2 < 0)
2443 edit_mark_cmd (edit, 0);
2444 edit_push_markers (edit);
2446 if (edit->column_highlight)
2448 long mark1, mark2;
2449 int size;
2450 int b_width = 0;
2451 int c1, c2;
2452 int x, x2;
2454 c1 = min (edit->column1, edit->column2);
2455 c2 = max (edit->column1, edit->column2);
2456 b_width = (c2 - c1);
2458 edit_update_curs_col (edit);
2460 x = edit->curs_col;
2461 x2 = x + edit->over_col;
2463 /* do nothing when cursor inside first line of selected area */
2464 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2465 return;
2467 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2469 if (x > c2)
2470 x -= b_width;
2471 else if (x > c1 && x <= c2)
2472 x = c1;
2474 /* save current selection into buffer */
2475 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2477 /* remove current selection */
2478 edit_block_delete_cmd (edit);
2480 edit->over_col = max (0, edit->over_col - b_width);
2481 /* calculate the cursor pos after delete block */
2482 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2483 edit_cursor_move (edit, current - edit->curs1);
2484 edit_scroll_screen_over_cursor (edit);
2486 /* add TWS if need before block insertion */
2487 if (option_cursor_beyond_eol && edit->over_col > 0)
2488 edit_insert_over (edit);
2490 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2491 edit_set_markers (edit, mark1, mark2, c1, c2);
2493 else
2495 long count;
2497 current = edit->curs1;
2498 copy_buf = g_malloc0 (end_mark - start_mark);
2499 edit_cursor_move (edit, start_mark - edit->curs1);
2500 edit_scroll_screen_over_cursor (edit);
2501 count = start_mark;
2502 while (count < end_mark)
2504 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2505 count++;
2507 edit_scroll_screen_over_cursor (edit);
2508 edit_cursor_move (edit,
2509 current - edit->curs1 -
2510 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2511 edit_scroll_screen_over_cursor (edit);
2512 while (count-- > start_mark)
2513 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2514 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2517 edit_scroll_screen_over_cursor (edit);
2518 g_free (copy_buf);
2519 edit->force |= REDRAW_PAGE;
2522 /* --------------------------------------------------------------------------------------------- */
2523 /** returns 1 if canceelled by user */
2526 edit_block_delete_cmd (WEdit * edit)
2528 long start_mark, end_mark;
2529 if (eval_marks (edit, &start_mark, &end_mark))
2531 edit_delete_line (edit);
2532 return 0;
2534 return edit_block_delete (edit);
2537 /* --------------------------------------------------------------------------------------------- */
2538 /** call with edit = 0 before shutdown to close memory leaks */
2540 void
2541 edit_replace_cmd (WEdit * edit, int again)
2543 /* 1 = search string, 2 = replace with */
2544 static char *saved1 = NULL; /* saved default[123] */
2545 static char *saved2 = NULL;
2546 char *input1 = NULL; /* user input from the dialog */
2547 char *input2 = NULL;
2548 GString *input2_str = NULL;
2549 char *disp1 = NULL;
2550 char *disp2 = NULL;
2551 long times_replaced = 0;
2552 gboolean once_found = FALSE;
2554 if (!edit)
2556 g_free (saved1), saved1 = NULL;
2557 g_free (saved2), saved2 = NULL;
2558 return;
2561 edit->force |= REDRAW_COMPLETELY;
2563 if (again && !saved1 && !saved2)
2564 again = 0;
2566 if (again)
2568 input1 = g_strdup (saved1 ? saved1 : "");
2569 input2 = g_strdup (saved2 ? saved2 : "");
2571 else
2573 char *tmp_inp1, *tmp_inp2;
2575 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2576 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2578 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2580 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2582 g_free (disp1);
2583 g_free (disp2);
2585 if (input1 == NULL || *input1 == '\0')
2587 edit->force = REDRAW_COMPLETELY;
2588 goto cleanup;
2591 tmp_inp1 = input1;
2592 tmp_inp2 = input2;
2593 input1 = edit_replace_cmd__conv_to_input (input1);
2594 input2 = edit_replace_cmd__conv_to_input (input2);
2595 g_free (tmp_inp1);
2596 g_free (tmp_inp2);
2598 g_free (saved1), saved1 = g_strdup (input1);
2599 g_free (saved2), saved2 = g_strdup (input2);
2601 mc_search_free (edit->search);
2602 edit->search = NULL;
2605 input2_str = g_string_new (input2);
2607 if (!edit->search)
2609 edit->search = mc_search_new (input1, -1);
2610 if (edit->search == NULL)
2612 edit->search_start = edit->curs1;
2613 goto cleanup;
2615 edit->search->search_type = edit_search_options.type;
2616 edit->search->is_all_charsets = edit_search_options.all_codepages;
2617 edit->search->is_case_sensitive = edit_search_options.case_sens;
2618 edit->search->whole_words = edit_search_options.whole_words;
2619 edit->search->search_fn = edit_search_cmd_callback;
2620 edit->search_line_type = edit_get_search_line_type (edit->search);
2621 edit_search_fix_search_start_if_selection (edit);
2624 if (edit->found_len && edit->search_start == edit->found_start + 1
2625 && edit_search_options.backwards)
2626 edit->search_start--;
2628 if (edit->found_len && edit->search_start == edit->found_start - 1
2629 && !edit_search_options.backwards)
2630 edit->search_start++;
2634 gsize len = 0;
2636 if (!editcmd_find (edit, &len))
2638 if (!(edit->search->error == MC_SEARCH_E_OK ||
2639 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2641 edit_error_dialog (_("Search"), edit->search->error_str);
2643 break;
2645 once_found = TRUE;
2647 edit->search_start = edit->search->normal_offset;
2648 /*returns negative on not found or error in pattern */
2650 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2652 gsize i;
2653 GString *repl_str;
2655 edit->found_start = edit->search_start;
2656 i = edit->found_len = len;
2658 edit_cursor_move (edit, edit->search_start - edit->curs1);
2659 edit_scroll_screen_over_cursor (edit);
2661 if (edit->replace_mode == 0)
2663 int l;
2664 int prompt;
2666 l = edit->curs_row - edit->widget.lines / 3;
2667 if (l > 0)
2668 edit_scroll_downward (edit, l);
2669 if (l < 0)
2670 edit_scroll_upward (edit, -l);
2672 edit_scroll_screen_over_cursor (edit);
2673 edit->force |= REDRAW_PAGE;
2674 edit_render_keypress (edit);
2676 /*so that undo stops at each query */
2677 edit_push_key_press (edit);
2678 /* and prompt 2/3 down */
2679 disp1 = edit_replace_cmd__conv_to_display (saved1);
2680 disp2 = edit_replace_cmd__conv_to_display (saved2);
2681 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2682 g_free (disp1);
2683 g_free (disp2);
2685 if (prompt == B_REPLACE_ALL)
2686 edit->replace_mode = 1;
2687 else if (prompt == B_SKIP_REPLACE)
2689 if (edit_search_options.backwards)
2690 edit->search_start--;
2691 else
2692 edit->search_start++;
2693 continue; /* loop */
2695 else if (prompt == B_CANCEL)
2697 edit->replace_mode = -1;
2698 break; /* loop */
2702 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2704 if (edit->search->error != MC_SEARCH_E_OK)
2706 edit_error_dialog (_("Replace"), edit->search->error_str);
2707 g_string_free (repl_str, TRUE);
2708 break;
2711 /* delete then insert new */
2712 for (i = 0; i < len; i++)
2713 edit_delete (edit, 1);
2715 for (i = 0; i < repl_str->len; i++)
2716 edit_insert (edit, repl_str->str[i]);
2718 edit->found_len = repl_str->len;
2719 g_string_free (repl_str, TRUE);
2720 times_replaced++;
2722 /* so that we don't find the same string again */
2723 if (edit_search_options.backwards)
2725 edit->search_start--;
2727 else
2729 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2731 if (edit->search_start >= edit->last_byte)
2732 break;
2735 edit_scroll_screen_over_cursor (edit);
2737 else
2739 /* try and find from right here for next search */
2740 edit->search_start = edit->curs1;
2741 edit_update_curs_col (edit);
2743 edit->force |= REDRAW_PAGE;
2744 edit_render_keypress (edit);
2746 if (times_replaced == 0)
2747 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2748 break;
2751 while (edit->replace_mode >= 0);
2753 edit_scroll_screen_over_cursor (edit);
2754 edit->force |= REDRAW_COMPLETELY;
2755 edit_render_keypress (edit);
2757 if ((edit->replace_mode == 1) && (times_replaced != 0))
2758 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2760 cleanup:
2761 g_free (input1);
2762 g_free (input2);
2763 if (input2_str != NULL)
2764 g_string_free (input2_str, TRUE);
2767 /* --------------------------------------------------------------------------------------------- */
2769 mc_search_cbret_t
2770 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2772 *current_char = edit_get_byte ((WEdit *) user_data, (long) char_offset);
2773 return MC_SEARCH_CB_OK;
2776 /* --------------------------------------------------------------------------------------------- */
2778 void
2779 edit_search_cmd (WEdit * edit, gboolean again)
2782 if (edit == NULL)
2783 return;
2785 if (!again)
2786 edit_search (edit);
2787 else if (edit->last_search_string != NULL)
2788 edit_do_search (edit);
2789 else
2791 /* find last search string in history */
2792 GList *history;
2794 history = history_get (MC_HISTORY_SHARED_SEARCH);
2795 if (history != NULL && history->data != NULL)
2797 edit->last_search_string = (char *) history->data;
2798 history->data = NULL;
2799 history = g_list_first (history);
2800 g_list_foreach (history, (GFunc) g_free, NULL);
2801 g_list_free (history);
2803 edit->search = mc_search_new (edit->last_search_string, -1);
2804 if (edit->search == NULL)
2806 /* if not... then ask for an expression */
2807 g_free (edit->last_search_string);
2808 edit->last_search_string = NULL;
2809 edit_search (edit);
2811 else
2813 edit->search->search_type = edit_search_options.type;
2814 edit->search->is_all_charsets = edit_search_options.all_codepages;
2815 edit->search->is_case_sensitive = edit_search_options.case_sens;
2816 edit->search->whole_words = edit_search_options.whole_words;
2817 edit->search->search_fn = edit_search_cmd_callback;
2818 edit->search_line_type = edit_get_search_line_type (edit->search);
2819 edit_do_search (edit);
2822 else
2824 /* if not... then ask for an expression */
2825 g_free (edit->last_search_string);
2826 edit->last_search_string = NULL;
2827 edit_search (edit);
2833 /* --------------------------------------------------------------------------------------------- */
2835 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2837 * @returns TRUE if it's OK to exit, FALSE to continue editing.
2840 gboolean
2841 edit_ok_to_exit (WEdit * edit)
2843 char *fname = (char *) N_("[NoName]");
2844 char *msg;
2845 int act;
2847 if (!edit->modified)
2848 return TRUE;
2850 if (edit->filename_vpath != NULL)
2851 fname = vfs_path_to_str (edit->filename_vpath);
2852 #ifdef ENABLE_NLS
2853 else
2854 fname = g_strdup (_(fname));
2855 #else
2856 else
2857 fname = g_strdup (fname);
2858 #endif
2860 if (!mc_global.midnight_shutdown)
2862 if (!edit_check_newline (edit))
2864 g_free (fname);
2865 return FALSE;
2868 query_set_sel (2);
2870 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2871 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2873 else
2875 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2876 fname);
2877 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2879 /* Esc is No */
2880 if (act == -1)
2881 act = 1;
2884 g_free (msg);
2885 g_free (fname);
2887 switch (act)
2889 case 0: /* Yes */
2890 edit_push_markers (edit);
2891 edit_set_markers (edit, 0, 0, 0, 0);
2892 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2893 return mc_global.midnight_shutdown;
2894 break;
2895 case 1: /* No */
2896 break;
2897 case 2: /* Cancel quit */
2898 case -1: /* Esc */
2899 return FALSE;
2902 return TRUE;
2905 /* --------------------------------------------------------------------------------------------- */
2906 /** save block, returns 1 on success */
2909 edit_save_block (WEdit * edit, const char *filename, long start, long finish)
2911 int len, file;
2912 vfs_path_t *vpath;
2914 vpath = vfs_path_from_str (filename);
2915 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2916 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2917 vfs_path_free (vpath);
2918 if (file == -1)
2919 return 0;
2921 if (edit->column_highlight)
2923 int r;
2925 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2926 if (r > 0)
2928 unsigned char *block, *p;
2930 p = block = edit_get_block (edit, start, finish, &len);
2931 while (len)
2933 r = mc_write (file, p, len);
2934 if (r < 0)
2935 break;
2936 p += r;
2937 len -= r;
2939 g_free (block);
2942 else
2944 unsigned char *buf;
2945 int i = start, end;
2947 len = finish - start;
2948 buf = g_malloc0 (TEMP_BUF_LEN);
2949 while (start != finish)
2951 end = min (finish, start + TEMP_BUF_LEN);
2952 for (; i < end; i++)
2953 buf[i - start] = edit_get_byte (edit, i);
2954 len -= mc_write (file, (char *) buf, end - start);
2955 start = end;
2957 g_free (buf);
2959 mc_close (file);
2960 if (len)
2961 return 0;
2962 return 1;
2965 /* --------------------------------------------------------------------------------------------- */
2967 void
2968 edit_paste_from_history (WEdit * edit)
2970 (void) edit;
2971 edit_error_dialog (_("Error"), _("This function is not implemented"));
2974 /* --------------------------------------------------------------------------------------------- */
2977 edit_copy_to_X_buf_cmd (WEdit * edit)
2979 long start_mark, end_mark;
2980 if (eval_marks (edit, &start_mark, &end_mark))
2981 return 0;
2982 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2984 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2985 return 1;
2987 /* try use external clipboard utility */
2988 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
2990 return 0;
2993 /* --------------------------------------------------------------------------------------------- */
2996 edit_cut_to_X_buf_cmd (WEdit * edit)
2998 long start_mark, end_mark;
2999 if (eval_marks (edit, &start_mark, &end_mark))
3000 return 0;
3001 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3003 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3004 return 1;
3006 /* try use external clipboard utility */
3007 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3009 edit_block_delete_cmd (edit);
3010 edit_mark_cmd (edit, 1);
3011 return 0;
3014 /* --------------------------------------------------------------------------------------------- */
3016 void
3017 edit_paste_from_X_buf_cmd (WEdit * edit)
3019 vfs_path_t *tmp;
3021 /* try use external clipboard utility */
3022 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3023 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3024 edit_insert_file (edit, tmp);
3025 vfs_path_free (tmp);
3029 /* --------------------------------------------------------------------------------------------- */
3031 * Ask user for the line and go to that line.
3032 * Negative numbers mean line from the end (i.e. -1 is the last line).
3035 void
3036 edit_goto_cmd (WEdit * edit)
3038 char *f;
3039 static long line = 0; /* line as typed, saved as default */
3040 long l;
3041 char *error;
3042 char s[32];
3044 g_snprintf (s, sizeof (s), "%ld", line);
3045 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3046 if (!f)
3047 return;
3049 if (!*f)
3051 g_free (f);
3052 return;
3055 l = strtol (f, &error, 0);
3056 if (*error)
3058 g_free (f);
3059 return;
3062 line = l;
3063 if (l < 0)
3064 l = edit->total_lines + l + 2;
3065 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3066 edit_move_to_line (edit, l - 1);
3067 edit->force |= REDRAW_COMPLETELY;
3068 g_free (f);
3072 /* --------------------------------------------------------------------------------------------- */
3073 /** Return 1 on success */
3076 edit_save_block_cmd (WEdit * edit)
3078 long start_mark, end_mark;
3079 char *exp, *tmp;
3081 if (eval_marks (edit, &start_mark, &end_mark))
3082 return 1;
3084 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3085 exp =
3086 input_expand_dialog (_("Save block"), _("Enter file name:"),
3087 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3088 g_free (tmp);
3089 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3090 if (exp)
3092 if (!*exp)
3094 g_free (exp);
3095 return 0;
3097 else
3099 if (edit_save_block (edit, exp, start_mark, end_mark))
3101 g_free (exp);
3102 edit->force |= REDRAW_COMPLETELY;
3103 return 1;
3105 else
3107 g_free (exp);
3108 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3112 edit->force |= REDRAW_COMPLETELY;
3113 return 0;
3117 /* --------------------------------------------------------------------------------------------- */
3119 /** returns TRUE on success */
3120 gboolean
3121 edit_insert_file_cmd (WEdit * edit)
3123 char *tmp;
3124 char *exp;
3125 gboolean ret = FALSE;
3127 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3128 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3129 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3130 g_free (tmp);
3132 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3134 if (exp != NULL && *exp != '\0')
3136 vfs_path_t *exp_vpath;
3138 exp_vpath = vfs_path_from_str (exp);
3139 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3140 vfs_path_free (exp_vpath);
3142 if (!ret)
3143 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3146 g_free (exp);
3148 edit->force |= REDRAW_COMPLETELY;
3149 return ret;
3152 /* --------------------------------------------------------------------------------------------- */
3153 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3156 edit_sort_cmd (WEdit * edit)
3158 static char *old = 0;
3159 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3160 long start_mark, end_mark;
3161 int e;
3163 if (eval_marks (edit, &start_mark, &end_mark))
3165 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3166 return 0;
3169 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3170 edit_save_block (edit, tmp, start_mark, end_mark);
3171 g_free (tmp);
3173 exp = input_dialog (_("Run sort"),
3174 _("Enter sort options (see manpage) separated by whitespace:"),
3175 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3177 if (!exp)
3178 return 1;
3179 g_free (old);
3180 old = exp;
3181 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3182 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3183 tmp =
3184 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3185 " > ", tmp_edit_temp_name, (char *) NULL);
3186 g_free (tmp_edit_temp_name);
3187 g_free (tmp_edit_block_name);
3189 e = system (tmp);
3190 g_free (tmp);
3191 if (e)
3193 if (e == -1 || e == 127)
3195 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3197 else
3199 char q[8];
3200 sprintf (q, "%d ", e);
3201 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3202 edit_error_dialog (_("Sort"), tmp);
3203 g_free (tmp);
3205 return -1;
3208 edit->force |= REDRAW_COMPLETELY;
3210 if (edit_block_delete_cmd (edit))
3211 return 1;
3214 vfs_path_t *tmp_vpath;
3216 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3217 edit_insert_file (edit, tmp_vpath);
3218 vfs_path_free (tmp_vpath);
3220 return 0;
3223 /* --------------------------------------------------------------------------------------------- */
3225 * Ask user for a command, execute it and paste its output back to the
3226 * editor.
3230 edit_ext_cmd (WEdit * edit)
3232 char *exp, *tmp, *tmp_edit_temp_file;
3233 int e;
3235 exp =
3236 input_dialog (_("Paste output of external command"),
3237 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3239 if (!exp)
3240 return 1;
3242 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3243 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3244 g_free (tmp_edit_temp_file);
3245 e = system (tmp);
3246 g_free (tmp);
3247 g_free (exp);
3249 if (e)
3251 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3252 return -1;
3255 edit->force |= REDRAW_COMPLETELY;
3258 vfs_path_t *tmp_vpath;
3260 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3261 edit_insert_file (edit, tmp_vpath);
3262 vfs_path_free (tmp_vpath);
3264 return 0;
3267 /* --------------------------------------------------------------------------------------------- */
3268 /** if block is 1, a block must be highlighted and the shell command
3269 processes it. If block is 0 the shell command is a straight system
3270 command, that just produces some output which is to be inserted */
3272 void
3273 edit_block_process_cmd (WEdit * edit, int macro_number)
3275 char *fname;
3276 char *macros_fname = NULL;
3278 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3279 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3280 user_menu (edit, macros_fname, 0);
3281 g_free (fname);
3282 g_free (macros_fname);
3283 edit->force |= REDRAW_COMPLETELY;
3286 /* --------------------------------------------------------------------------------------------- */
3288 void
3289 edit_mail_dialog (WEdit * edit)
3291 char *tmail_to;
3292 char *tmail_subject;
3293 char *tmail_cc;
3295 static char *mail_cc_last = 0;
3296 static char *mail_subject_last = 0;
3297 static char *mail_to_last = 0;
3299 QuickWidget quick_widgets[] = {
3300 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3301 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3302 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3303 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3304 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3305 &tmail_subject),
3306 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3307 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3308 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3309 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3310 QUICK_END
3313 QuickDialog Quick_input = {
3314 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3315 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3318 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3319 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3320 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3322 if (quick_dialog (&Quick_input) != B_CANCEL)
3324 g_free (mail_cc_last);
3325 g_free (mail_subject_last);
3326 g_free (mail_to_last);
3327 mail_cc_last = tmail_cc;
3328 mail_subject_last = tmail_subject;
3329 mail_to_last = tmail_to;
3330 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3335 /*******************/
3336 /* Word Completion */
3337 /*******************/
3339 /* --------------------------------------------------------------------------------------------- */
3341 * Complete current word using regular expression search
3342 * backwards beginning at the current cursor position.
3345 void
3346 edit_complete_word_cmd (WEdit * edit)
3348 gsize i, max_len, word_len = 0, num_compl = 0;
3349 long word_start = 0;
3350 unsigned char *bufpos;
3351 char *match_expr;
3352 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3354 /* search start of word to be completed */
3355 if (!edit_find_word_start (edit, &word_start, &word_len))
3356 return;
3358 /* prepare match expression */
3359 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3361 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3362 match_expr =
3363 g_strdup_printf
3364 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3365 (int) word_len, bufpos);
3367 /* collect the possible completions */
3368 /* start search from begin to end of file */
3369 max_len =
3370 edit_collect_completions (edit, word_start, word_len, match_expr,
3371 (struct selection *) &compl, &num_compl);
3373 if (num_compl > 0)
3375 /* insert completed word if there is only one match */
3376 if (num_compl == 1)
3378 for (i = word_len; i < compl[0].len; i++)
3379 edit_insert (edit, *(compl[0].text + i));
3381 /* more than one possible completion => ask the user */
3382 else
3384 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3385 /* !!! pressed again the selection dialog pops up, but that !!! */
3386 /* !!! seems to require a further internal state !!! */
3387 /*tty_beep (); */
3389 /* let the user select the preferred completion */
3390 editcmd_dialog_completion_show (edit, max_len, word_len,
3391 (struct selection *) &compl, num_compl);
3395 g_free (match_expr);
3396 /* release memory before return */
3397 for (i = 0; i < num_compl; i++)
3398 g_free (compl[i].text);
3401 /* --------------------------------------------------------------------------------------------- */
3403 void
3404 edit_select_codepage_cmd (WEdit * edit)
3406 #ifdef HAVE_CHARSET
3407 if (do_select_codepage ())
3408 edit_set_codeset (edit);
3410 edit->force = REDRAW_PAGE;
3411 send_message ((Widget *) edit, WIDGET_DRAW, 0);
3412 #else
3413 (void) edit;
3414 #endif
3417 /* --------------------------------------------------------------------------------------------- */
3419 void
3420 edit_insert_literal_cmd (WEdit * edit)
3422 int char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3423 _("Press any key:"), 0);
3424 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3427 /* --------------------------------------------------------------------------------------------- */
3429 void
3430 edit_begin_end_macro_cmd (WEdit * edit)
3432 /* edit is a pointer to the widget */
3433 if (edit != NULL)
3435 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3436 edit_execute_key_command (edit, command, -1);
3440 /* --------------------------------------------------------------------------------------------- */
3442 void
3443 edit_begin_end_repeat_cmd (WEdit * edit)
3445 /* edit is a pointer to the widget */
3446 if (edit != NULL)
3448 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3449 edit_execute_key_command (edit, command, -1);
3453 /* --------------------------------------------------------------------------------------------- */
3455 gboolean
3456 edit_load_forward_cmd (WEdit * edit)
3458 if (edit->modified
3459 && edit_query_dialog2 (_("Warning"),
3460 _("Current text was modified without a file save.\n"
3461 "Continue discards these changes"), _("C&ontinue"),
3462 _("&Cancel")) == 1)
3464 edit->force |= REDRAW_COMPLETELY;
3465 return TRUE;
3468 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3469 return FALSE;
3471 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3472 return FALSE;
3474 edit_stack_iterator++;
3475 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3476 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3477 edit_history_moveto[edit_stack_iterator].line);
3479 return FALSE;
3482 /* --------------------------------------------------------------------------------------------- */
3484 gboolean
3485 edit_load_back_cmd (WEdit * edit)
3487 if (edit->modified
3488 && edit_query_dialog2 (_("Warning"),
3489 _("Current text was modified without a file save.\n"
3490 "Continue discards these changes"), _("C&ontinue"),
3491 _("&Cancel")) == 1)
3493 edit->force |= REDRAW_COMPLETELY;
3494 return TRUE;
3497 if (edit_stack_iterator < 0)
3498 return FALSE;
3500 edit_stack_iterator--;
3501 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3502 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3503 edit_history_moveto[edit_stack_iterator].line);
3505 return FALSE;
3508 /* --------------------------------------------------------------------------------------------- */
3510 void
3511 edit_get_match_keyword_cmd (WEdit * edit)
3513 gsize word_len = 0, max_len = 0;
3514 int num_def = 0;
3515 int i;
3516 long word_start = 0;
3517 unsigned char *bufpos;
3518 char *match_expr;
3519 char *path = NULL;
3520 char *ptr = NULL;
3521 char *tagfile = NULL;
3523 etags_hash_t def_hash[MAX_DEFINITIONS];
3525 for (i = 0; i < MAX_DEFINITIONS; i++)
3527 def_hash[i].filename = NULL;
3530 /* search start of word to be completed */
3531 if (!edit_find_word_start (edit, &word_start, &word_len))
3532 return;
3534 /* prepare match expression */
3535 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3536 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3538 ptr = g_get_current_dir ();
3539 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3540 g_free (ptr);
3542 /* Recursive search file 'TAGS' in parent dirs */
3545 ptr = g_path_get_dirname (path);
3546 g_free (path);
3547 path = ptr;
3548 g_free (tagfile);
3549 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3550 if (exist_file (tagfile))
3551 break;
3553 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3555 if (tagfile)
3557 num_def =
3558 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3559 g_free (tagfile);
3561 g_free (path);
3563 max_len = MAX_WIDTH_DEF_DIALOG;
3564 word_len = 0;
3565 if (num_def > 0)
3567 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3568 (etags_hash_t *) & def_hash, num_def);
3570 g_free (match_expr);
3573 /* --------------------------------------------------------------------------------------------- */