Ticket #2832: Fixed mcedit: unable to save changes in "Safe save" mode
[midnight-commander.git] / src / editor / editcmd.c
blob536ac7e1f570127f95ceccb6458de2ffc51e37b1
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
11 Ilia Maslakov <il.smind@gmail.com> 2012
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: editor high level editing commands
31 * \author Paul Sheer
32 * \date 1996, 1997
35 /* #define PIPE_BLOCKS_SO_READ_BYTE_BY_BYTE */
37 #include <config.h>
39 #ifdef HAVE_ASSERT_H
40 #include <assert.h>
41 #endif
42 #include <ctype.h>
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
54 #include "lib/global.h"
55 #include "lib/tty/tty.h"
56 #include "lib/tty/key.h" /* XCTRL */
57 #include "lib/mcconfig.h"
58 #include "lib/skin.h"
59 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/lock.h"
61 #include "lib/util.h" /* tilde_expand() */
62 #include "lib/vfs/vfs.h"
63 #include "lib/widget.h"
64 #include "lib/event.h" /* mc_event_raise() */
65 #ifdef HAVE_CHARSET
66 #include "lib/charsets.h"
67 #endif
69 #include "src/history.h"
70 #include "src/setup.h" /* option_tab_spacing */
71 #include "src/main.h" /* mactos_t */
72 #ifdef HAVE_CHARSET
73 #include "src/selcodepage.h"
74 #endif
75 #include "src/keybind-defaults.h"
76 #include "src/util.h" /* check_for_default() */
77 #include "src/filemanager/layout.h" /* mc_refresh() */
79 #include "edit-impl.h"
80 #include "editwidget.h"
81 #include "editcmd_dialogs.h"
82 #ifdef HAVE_ASPELL
83 #include "spell.h"
84 #include "spell_dialogs.h"
85 #endif
86 #include "etags.h"
88 /*** global variables ****************************************************************************/
90 /* search and replace: */
91 int search_create_bookmark = FALSE;
93 /* queries on a save */
94 int edit_confirm_save = 1;
96 /*** file scope macro definitions ****************************************************************/
98 #define space_width 1
100 #define TEMP_BUF_LEN 1024
102 #define INPUT_INDEX 9
104 /* thanks to Liviu Daia <daia@stoilow.imar.ro> for getting this
105 (and the above) routines to work properly - paul */
107 #define is_digit(x) ((x) >= '0' && (x) <= '9')
109 #define MAIL_DLG_HEIGHT 12
111 #define MAX_WORD_COMPLETIONS 100 /* in listbox */
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 /* If 0 (quick save) then a) create/truncate <filename> file,
121 b) save to <filename>;
122 if 1 (safe save) then a) save to <tempnam>,
123 b) rename <tempnam> to <filename>;
124 if 2 (do backups) then a) save to <tempnam>,
125 b) rename <filename> to <filename.backup_ext>,
126 c) rename <tempnam> to <filename>. */
128 /* returns 0 on error, -1 on abort */
130 static int
131 edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
133 char *p;
134 gchar *tmp;
135 off_t filelen = 0;
136 int this_save_mode, fd = -1;
137 vfs_path_t *real_filename_vpath;
138 vfs_path_t *savename_vpath = NULL;
139 const char *start_filename;
140 const vfs_path_element_t *vpath_element;
142 vpath_element = vfs_path_get_by_index (filename_vpath, 0);
143 if (vpath_element == NULL)
144 return 0;
146 start_filename = vpath_element->path;
147 if (*start_filename == '\0')
148 return 0;
150 if (*start_filename != PATH_SEP && edit->dir_vpath != NULL)
152 real_filename_vpath = vfs_path_append_vpath_new (edit->dir_vpath, filename_vpath, NULL);
154 else
156 real_filename_vpath = vfs_path_clone (filename_vpath);
159 this_save_mode = option_save_mode;
160 if (this_save_mode != EDIT_QUICK_SAVE)
162 if (!vfs_file_is_local (real_filename_vpath)
163 || (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
166 * The file does not exists yet, so no safe save or
167 * backup are necessary.
169 this_save_mode = EDIT_QUICK_SAVE;
171 if (fd != -1)
172 mc_close (fd);
175 if (this_save_mode == EDIT_QUICK_SAVE && !edit->skip_detach_prompt)
177 int rv;
178 struct stat sb;
180 rv = mc_stat (real_filename_vpath, &sb);
181 if (rv == 0 && sb.st_nlink > 1)
183 rv = edit_query_dialog3 (_("Warning"),
184 _("File has hard-links. Detach before saving?"),
185 _("&Yes"), _("&No"), _("&Cancel"));
186 switch (rv)
188 case 0:
189 this_save_mode = EDIT_SAFE_SAVE;
190 /* fallthrough */
191 case 1:
192 edit->skip_detach_prompt = 1;
193 break;
194 default:
195 vfs_path_free (real_filename_vpath);
196 return -1;
200 /* Prevent overwriting changes from other editor sessions. */
201 if (rv == 0 && edit->stat1.st_mtime != 0 && edit->stat1.st_mtime != sb.st_mtime)
203 /* The default action is "Cancel". */
204 query_set_sel (1);
206 rv = edit_query_dialog2 (_("Warning"),
207 _("The file has been modified in the meantime. Save anyway?"),
208 _("&Yes"), _("&Cancel"));
209 if (rv != 0)
211 vfs_path_free (real_filename_vpath);
212 return -1;
217 if (this_save_mode != EDIT_QUICK_SAVE)
219 char *savedir, *saveprefix;
221 savedir = vfs_path_tokens_get (real_filename_vpath, 0, -1);
222 if (savedir == NULL)
223 savedir = g_strdup (".");
225 /* Token-related function never return leading slash, so we need add it manually */
226 saveprefix = mc_build_filename ("/", savedir, "cooledit", NULL);
227 g_free (savedir);
228 fd = mc_mkstemps (&savename_vpath, saveprefix, NULL);
229 g_free (saveprefix);
230 if (savename_vpath == NULL)
232 vfs_path_free (real_filename_vpath);
233 return 0;
235 /* FIXME:
236 * Close for now because mc_mkstemps use pure open system call
237 * to create temporary file and it needs to be reopened by
238 * VFS-aware mc_open().
240 close (fd);
242 else
243 savename_vpath = vfs_path_clone (real_filename_vpath);
245 (void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
246 (void) mc_chmod (savename_vpath, edit->stat1.st_mode);
248 fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
249 if (fd == -1)
250 goto error_save;
252 /* pipe save */
253 p = edit_get_write_filter (savename_vpath, real_filename_vpath);
254 if (p != NULL)
256 FILE *file;
258 mc_close (fd);
259 file = (FILE *) popen (p, "w");
261 if (file)
263 filelen = edit_write_stream (edit, file);
264 #if 1
265 pclose (file);
266 #else
267 if (pclose (file) != 0)
269 tmp = g_strdup_printf (_("Error writing to pipe: %s"), p);
270 edit_error_dialog (_("Error"), tmp);
271 g_free (tmp);
272 g_free (p);
273 goto error_save;
275 #endif
277 else
279 tmp = g_strdup_printf (_("Cannot open pipe for writing: %s"), p);
280 edit_error_dialog (_("Error"), get_sys_error (tmp));
281 g_free (p);
282 g_free (tmp);
283 goto error_save;
285 g_free (p);
287 else if (edit->lb == LB_ASIS)
288 { /* do not change line breaks */
289 off_t buf;
290 buf = 0;
291 filelen = edit->last_byte;
292 while (buf <= (edit->curs1 >> S_EDIT_BUF_SIZE) - 1)
294 if (mc_write (fd, (char *) edit->buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
296 mc_close (fd);
297 goto error_save;
299 buf++;
301 if (mc_write
302 (fd, (char *) edit->buffers1[buf],
303 edit->curs1 & M_EDIT_BUF_SIZE) != (edit->curs1 & M_EDIT_BUF_SIZE))
305 filelen = -1;
307 else if (edit->curs2)
309 edit->curs2--;
310 buf = (edit->curs2 >> S_EDIT_BUF_SIZE);
311 if (mc_write
312 (fd,
313 (char *) edit->buffers2[buf] + EDIT_BUF_SIZE -
314 (edit->curs2 & M_EDIT_BUF_SIZE) - 1,
315 1 + (edit->curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->curs2 & M_EDIT_BUF_SIZE))
317 filelen = -1;
319 else
321 while (--buf >= 0)
323 if (mc_write (fd, (char *) edit->buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE)
325 filelen = -1;
326 break;
330 edit->curs2++;
332 if (mc_close (fd))
333 goto error_save;
335 /* Update the file information, especially the mtime. */
336 if (mc_stat (savename_vpath, &edit->stat1) == -1)
337 goto error_save;
339 else
340 { /* change line breaks */
341 FILE *file;
342 const vfs_path_element_t *path_element;
344 mc_close (fd);
346 path_element = vfs_path_get_by_index (savename_vpath, -1);
347 file = (FILE *) fopen (path_element->path, "w");
348 if (file != NULL)
350 filelen = edit_write_stream (edit, file);
351 fclose (file);
353 else
355 char *msg;
357 msg = g_strdup_printf (_("Cannot open file for writing: %s"), path_element->path);
358 edit_error_dialog (_("Error"), msg);
359 g_free (msg);
360 goto error_save;
364 if (filelen != edit->last_byte)
365 goto error_save;
367 if (this_save_mode == EDIT_DO_BACKUP)
369 char *tmp_store_filename;
370 vfs_path_element_t *last_vpath_element;
371 vfs_path_t *tmp_vpath;
372 gboolean ok;
374 #ifdef HAVE_ASSERT_H
375 assert (option_backup_ext != NULL);
376 #endif
377 /* add backup extention to the path */
378 tmp_vpath = vfs_path_clone (real_filename_vpath);
379 last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1);
380 tmp_store_filename = last_vpath_element->path;
381 last_vpath_element->path = g_strdup_printf ("%s%s", tmp_store_filename, option_backup_ext);
382 g_free (tmp_store_filename);
384 ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
385 vfs_path_free (tmp_vpath);
386 if (!ok)
387 goto error_save;
390 if (this_save_mode != EDIT_QUICK_SAVE)
391 if (mc_rename (savename_vpath, real_filename_vpath) == -1)
392 goto error_save;
394 vfs_path_free (real_filename_vpath);
395 vfs_path_free (savename_vpath);
396 return 1;
397 error_save:
398 /* FIXME: Is this safe ?
399 * if (this_save_mode != EDIT_QUICK_SAVE)
400 * mc_unlink (savename);
402 vfs_path_free (real_filename_vpath);
403 vfs_path_free (savename_vpath);
404 return 0;
407 /* --------------------------------------------------------------------------------------------- */
409 static gboolean
410 edit_check_newline (WEdit * edit)
412 return !(option_check_nl_at_eof && edit->last_byte > 0
413 && edit_get_byte (edit, edit->last_byte - 1) != '\n'
414 && edit_query_dialog2 (_("Warning"),
415 _("The file you are saving is not finished with a newline"),
416 _("C&ontinue"), _("&Cancel")));
419 /* --------------------------------------------------------------------------------------------- */
421 static vfs_path_t *
422 edit_get_save_file_as (WEdit * edit)
424 #define DLG_WIDTH 64
425 #define DLG_HEIGHT 14
427 static LineBreaks cur_lb = LB_ASIS;
429 char *filename = vfs_path_to_str (edit->filename_vpath);
430 char *filename_res;
432 const char *lb_names[LB_NAMES] = {
433 N_("&Do not change"),
434 N_("&Unix format (LF)"),
435 N_("&Windows/DOS format (CR LF)"),
436 N_("&Macintosh format (CR)")
439 QuickWidget quick_widgets[] = {
440 QUICK_BUTTON (6, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
441 QUICK_BUTTON (2, 10, DLG_HEIGHT - 3, DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
442 QUICK_RADIO (5, DLG_WIDTH, DLG_HEIGHT - 8, DLG_HEIGHT, LB_NAMES, lb_names, (int *) &cur_lb),
443 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 9, DLG_HEIGHT, N_("Change line breaks to:")),
444 QUICK_INPUT (3, DLG_WIDTH, DLG_HEIGHT - 11, DLG_HEIGHT, filename, DLG_WIDTH - 6, 0,
445 "save-as", &filename_res),
446 QUICK_LABEL (3, DLG_WIDTH, DLG_HEIGHT - 12, DLG_HEIGHT, N_("Enter file name:")),
447 QUICK_END
450 QuickDialog Quick_options = {
451 DLG_WIDTH, DLG_HEIGHT, -1, -1,
452 N_("Save As"), "[Save File As]",
453 quick_widgets, NULL, NULL, FALSE
456 if (quick_dialog (&Quick_options) != B_CANCEL)
458 char *fname;
459 vfs_path_t *ret_vpath;
461 edit->lb = cur_lb;
462 fname = tilde_expand (filename_res);
463 g_free (filename_res);
464 ret_vpath = vfs_path_from_str (fname);
465 g_free (fname);
466 return ret_vpath;
468 g_free (filename);
470 return NULL;
472 #undef DLG_WIDTH
473 #undef DLG_HEIGHT
476 /* --------------------------------------------------------------------------------------------- */
478 /** returns TRUE on success */
480 static gboolean
481 edit_save_cmd (WEdit * edit)
483 int res, save_lock = 0;
485 if (!edit->locked && !edit->delete_file)
486 save_lock = lock_file (edit->filename_vpath);
487 res = edit_save_file (edit, edit->filename_vpath);
489 /* Maintain modify (not save) lock on failure */
490 if ((res > 0 && edit->locked) || save_lock)
491 edit->locked = unlock_file (edit->filename_vpath);
493 /* On failure try 'save as', it does locking on its own */
494 if (res == 0)
495 return edit_save_as_cmd (edit);
496 edit->force |= REDRAW_COMPLETELY;
497 if (res > 0)
499 edit->delete_file = 0;
500 edit->modified = 0;
503 return TRUE;
506 /* --------------------------------------------------------------------------------------------- */
508 * Load file content
510 * @param h screen the owner of editor window
511 * @param vpath vfs file path
512 * @return TRUE if file content was successfully loaded, FALSE otherwise
515 static inline gboolean
516 edit_load_file_from_filename (Dlg_head * h, const vfs_path_t * vpath)
518 return edit_add_window (h, h->y + 1, h->x, h->lines - 2, h->cols, vpath, 0);
521 /* --------------------------------------------------------------------------------------------- */
523 static void
524 edit_delete_column_of_text (WEdit * edit)
526 off_t p, q, r;
527 off_t m1, m2;
528 off_t n;
529 long b, c, d;
531 eval_marks (edit, &m1, &m2);
532 n = edit_move_forward (edit, m1, 0, m2) + 1;
533 c = (long) edit_move_forward3 (edit, edit_bol (edit, m1), 0, m1);
534 d = (long) edit_move_forward3 (edit, edit_bol (edit, m2), 0, m2);
535 b = max (min (c, d), min (edit->column1, edit->column2));
536 c = max (c, max (edit->column1, edit->column2));
538 while (n--)
540 r = edit_bol (edit, edit->curs1);
541 p = edit_move_forward3 (edit, r, b, 0);
542 q = edit_move_forward3 (edit, r, c, 0);
543 if (p < m1)
544 p = m1;
545 if (q > m2)
546 q = m2;
547 edit_cursor_move (edit, p - edit->curs1);
548 while (q > p)
550 /* delete line between margins */
551 if (edit_get_byte (edit, edit->curs1) != '\n')
552 edit_delete (edit, 1);
553 q--;
555 if (n)
556 /* move to next line except on the last delete */
557 edit_cursor_move (edit, edit_move_forward (edit, edit->curs1, 1, 0) - edit->curs1);
561 /* --------------------------------------------------------------------------------------------- */
562 /** if success return 0 */
564 static int
565 edit_block_delete (WEdit * edit)
567 off_t count;
568 off_t start_mark, end_mark;
569 int curs_pos;
570 long curs_line, c1, c2;
572 if (eval_marks (edit, &start_mark, &end_mark))
573 return 0;
574 if (edit->column_highlight && edit->mark2 < 0)
575 edit_mark_cmd (edit, FALSE);
576 if ((end_mark - start_mark) > option_max_undo / 2)
578 /* Warning message with a query to continue or cancel the operation */
579 if (edit_query_dialog2
580 (_("Warning"),
582 ("Block is large, you may not be able to undo this action"),
583 _("C&ontinue"), _("&Cancel")))
585 return 1;
588 c1 = min (edit->column1, edit->column2);
589 c2 = max (edit->column1, edit->column2);
590 edit->column1 = c1;
591 edit->column2 = c2;
593 edit_push_markers (edit);
595 curs_line = edit->curs_line;
597 curs_pos = edit->curs_col + edit->over_col;
599 /* move cursor to start of selection */
600 edit_cursor_move (edit, start_mark - edit->curs1);
601 edit_scroll_screen_over_cursor (edit);
602 count = start_mark;
603 if (start_mark < end_mark)
605 if (edit->column_highlight)
607 long line_width;
609 if (edit->mark2 < 0)
610 edit_mark_cmd (edit, FALSE);
611 edit_delete_column_of_text (edit);
612 /* move cursor to the saved position */
613 edit_move_to_line (edit, curs_line);
614 /* calculate line width and cursor position before cut */
615 line_width = (long) edit_move_forward3 (edit, edit_bol (edit, edit->curs1), 0,
616 edit_eol (edit, edit->curs1));
617 if (option_cursor_beyond_eol && curs_pos > line_width)
618 edit->over_col = curs_pos - line_width;
620 else
622 while (count < end_mark)
624 edit_delete (edit, 1);
625 count++;
629 edit_set_markers (edit, 0, 0, 0, 0);
630 edit->force |= REDRAW_PAGE;
631 return 0;
634 /* --------------------------------------------------------------------------------------------- */
636 * Get EOL symbol for searching.
638 * @param edit editor object
639 * @return EOL symbol
642 static inline char
643 edit_search_get_current_end_line_char (const WEdit * edit)
645 switch (edit->lb)
647 case LB_MAC:
648 return '\r';
649 default:
650 return '\n';
654 /* --------------------------------------------------------------------------------------------- */
656 * Checking if search condition have BOL(^) or EOL ($) regexp special characters.
658 * @param search search object
659 * @return result of checks.
662 static edit_search_line_t
663 edit_get_search_line_type (mc_search_t * search)
665 edit_search_line_t search_line_type = 0;
667 if (search->search_type != MC_SEARCH_T_REGEX)
668 return search_line_type;
670 if (*search->original == '^')
671 search_line_type |= AT_START_LINE;
673 if (search->original[search->original_len - 1] == '$')
674 search_line_type |= AT_END_LINE;
675 return search_line_type;
678 /* --------------------------------------------------------------------------------------------- */
680 * Calculating the start position of next line.
682 * @param edit editor object
683 * @param current_pos current position
684 * @param max_pos max position
685 * @param end_string_symbol end of line symbol
686 * @return start position of next line
689 static off_t
690 edit_calculate_start_of_next_line (WEdit * edit, off_t current_pos, off_t max_pos,
691 char end_string_symbol)
693 off_t i;
695 for (i = current_pos; i < max_pos; i++)
697 current_pos++;
698 if (edit_get_byte (edit, i) == end_string_symbol)
699 break;
702 return current_pos;
705 /* --------------------------------------------------------------------------------------------- */
707 * Calculating the end position of previous line.
709 * @param edit editor object
710 * @param current_pos current position
711 * @param end_string_symbol end of line symbol
712 * @return end position of previous line
715 static off_t
716 edit_calculate_end_of_previous_line (WEdit * edit, off_t current_pos, char end_string_symbol)
718 off_t i;
720 for (i = current_pos - 1; i >= 0; i--)
721 if (edit_get_byte (edit, i) == end_string_symbol)
722 break;
724 return i;
727 /* --------------------------------------------------------------------------------------------- */
729 * Calculating the start position of previous 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 previous line
737 static inline off_t
738 edit_calculate_start_of_previous_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);
741 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
743 return (current_pos + 1);
746 /* --------------------------------------------------------------------------------------------- */
748 * Calculating the start position of current line.
750 * @param edit editor object
751 * @param current_pos current position
752 * @param end_string_symbol end of line symbol
753 * @return start position of current line
756 static inline off_t
757 edit_calculate_start_of_current_line (WEdit * edit, off_t current_pos, char end_string_symbol)
759 current_pos = edit_calculate_end_of_previous_line (edit, current_pos, end_string_symbol);
761 return (current_pos + 1);
764 /* --------------------------------------------------------------------------------------------- */
766 * Fixing (if needed) search start position if 'only in selection' option present.
768 * @param edit editor object
771 static void
772 edit_search_fix_search_start_if_selection (WEdit * edit)
774 off_t start_mark = 0;
775 off_t end_mark = 0;
777 if (!edit_search_options.only_in_selection)
778 return;
780 if (eval_marks (edit, &start_mark, &end_mark) != 0)
781 return;
783 if (edit_search_options.backwards)
785 if (edit->search_start > end_mark || edit->search_start <= start_mark)
786 edit->search_start = end_mark;
788 else
790 if (edit->search_start < start_mark || edit->search_start >= end_mark)
791 edit->search_start = start_mark;
795 /* --------------------------------------------------------------------------------------------- */
797 static gboolean
798 editcmd_find (WEdit * edit, gsize * len)
800 off_t search_start = edit->search_start;
801 off_t search_end;
802 off_t start_mark = 0;
803 off_t end_mark = edit->last_byte;
804 int mark_res = 0;
805 char end_string_symbol;
807 end_string_symbol = edit_search_get_current_end_line_char (edit);
809 /* prepare for search */
810 if (edit_search_options.only_in_selection)
812 mark_res = eval_marks (edit, &start_mark, &end_mark);
813 if (mark_res != 0)
815 edit->search->error = MC_SEARCH_E_NOTFOUND;
816 edit->search->error_str = g_strdup (_("Search string not found"));
817 return FALSE;
820 /* fix the start and the end of search block positions */
821 if ((edit->search_line_type & AT_START_LINE) != 0
822 && (start_mark != 0 || edit_get_byte (edit, start_mark - 1) != end_string_symbol))
824 start_mark =
825 edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte,
826 end_string_symbol);
828 if ((edit->search_line_type & AT_END_LINE) != 0
829 && (end_mark - 1 != edit->last_byte
830 || edit_get_byte (edit, end_mark) != end_string_symbol))
832 end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol);
834 if (start_mark >= end_mark)
836 edit->search->error = MC_SEARCH_E_NOTFOUND;
837 edit->search->error_str = g_strdup (_("Search string not found"));
838 return FALSE;
841 else
843 if (edit_search_options.backwards)
844 end_mark = max (1, edit->curs1) - 1;
847 /* search */
848 if (edit_search_options.backwards)
850 /* backward search */
851 search_end = end_mark;
853 if ((edit->search_line_type & AT_START_LINE) != 0)
854 search_start =
855 edit_calculate_start_of_current_line (edit, search_start, end_string_symbol);
857 while ((int) search_start >= start_mark)
859 if (search_end > (off_t) (search_start + edit->search->original_len)
860 && mc_search_is_fixed_search_str (edit->search))
862 search_end = search_start + edit->search->original_len;
864 if (mc_search_run (edit->search, (void *) edit, search_start, search_end, len)
865 && edit->search->normal_offset == search_start)
867 return TRUE;
871 if ((edit->search_line_type & AT_START_LINE) != 0)
872 search_start =
873 edit_calculate_start_of_previous_line (edit, search_start, end_string_symbol);
874 else
875 search_start--;
877 edit->search->error_str = g_strdup (_("Search string not found"));
879 else
881 /* forward search */
882 if ((edit->search_line_type & AT_START_LINE) != 0 && search_start != start_mark)
883 search_start =
884 edit_calculate_start_of_next_line (edit, search_start, end_mark, end_string_symbol);
885 return mc_search_run (edit->search, (void *) edit, search_start, end_mark, len);
887 return FALSE;
890 /* --------------------------------------------------------------------------------------------- */
892 static char *
893 edit_replace_cmd__conv_to_display (char *str)
895 #ifdef HAVE_CHARSET
896 GString *tmp;
898 tmp = str_convert_to_display (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 char *
912 edit_replace_cmd__conv_to_input (char *str)
914 #ifdef HAVE_CHARSET
915 GString *tmp;
917 tmp = str_convert_to_input (str);
918 if (tmp != NULL)
920 if (tmp->len != 0)
921 return g_string_free (tmp, FALSE);
922 g_string_free (tmp, TRUE);
924 #endif
925 return g_strdup (str);
928 /* --------------------------------------------------------------------------------------------- */
930 static void
931 edit_do_search (WEdit * edit)
933 gsize len = 0;
935 if (edit->search == NULL)
936 edit->search_start = edit->curs1;
938 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
940 if (search_create_bookmark)
942 int found = 0, books = 0;
943 long l = 0, l_last = -1;
944 long q = 0;
946 search_create_bookmark = FALSE;
947 book_mark_flush (edit, -1);
949 while (TRUE)
951 if (!mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
952 break;
953 if (found == 0)
954 edit->search_start = edit->search->normal_offset;
955 found++;
956 l += edit_count_lines (edit, q, edit->search->normal_offset);
957 if (l != l_last)
959 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
960 books++;
962 l_last = l;
963 q = edit->search->normal_offset + 1;
966 if (found == 0)
967 edit_error_dialog (_("Search"), _("Search string not found"));
968 else
969 edit_cursor_move (edit, edit->search_start - edit->curs1);
971 else
973 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
974 && edit_search_options.backwards)
975 edit->search_start--;
977 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
978 && !edit_search_options.backwards)
979 edit->search_start++;
981 if (editcmd_find (edit, &len))
983 edit->found_start = edit->search_start = edit->search->normal_offset;
984 edit->found_len = len;
985 edit->over_col = 0;
986 edit_cursor_move (edit, edit->search_start - edit->curs1);
987 edit_scroll_screen_over_cursor (edit);
988 if (edit_search_options.backwards)
989 edit->search_start--;
990 else
991 edit->search_start++;
993 else
995 edit->search_start = edit->curs1;
996 if (edit->search->error_str != NULL)
997 edit_error_dialog (_("Search"), edit->search->error_str);
1001 edit->force |= REDRAW_COMPLETELY;
1002 edit_scroll_screen_over_cursor (edit);
1005 /* --------------------------------------------------------------------------------------------- */
1007 static void
1008 edit_search (WEdit * edit)
1010 if (editcmd_dialog_search_show (edit))
1012 edit->search_line_type = edit_get_search_line_type (edit->search);
1013 edit_search_fix_search_start_if_selection (edit);
1014 edit_do_search (edit);
1018 /* --------------------------------------------------------------------------------------------- */
1019 /** Return a null terminated length of text. Result must be g_free'd */
1021 static unsigned char *
1022 edit_get_block (WEdit * edit, off_t start, off_t finish, int *l)
1024 unsigned char *s, *r;
1025 r = s = g_malloc0 (finish - start + 1);
1026 if (edit->column_highlight)
1028 *l = 0;
1029 /* copy from buffer, excluding chars that are out of the column 'margins' */
1030 while (start < finish)
1032 int c;
1033 off_t x;
1035 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1036 c = edit_get_byte (edit, start);
1037 if ((x >= edit->column1 && x < edit->column2)
1038 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1040 *s++ = c;
1041 (*l)++;
1043 start++;
1046 else
1048 *l = finish - start;
1049 while (start < finish)
1050 *s++ = edit_get_byte (edit, start++);
1052 *s = 0;
1053 return r;
1056 /* --------------------------------------------------------------------------------------------- */
1057 /** copies a block to clipboard file */
1059 static gboolean
1060 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1062 gboolean ret;
1063 gchar *tmp;
1065 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1066 ret = edit_save_block (edit, tmp, start, finish);
1067 g_free (tmp);
1068 return ret;
1071 /* --------------------------------------------------------------------------------------------- */
1073 static void
1074 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1076 FILE *p = 0;
1077 char *s;
1079 to = name_quote (to, 0);
1080 subject = name_quote (subject, 0);
1081 cc = name_quote (cc, 0);
1082 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1083 g_free (to);
1084 g_free (subject);
1085 g_free (cc);
1087 if (s)
1089 p = popen (s, "w");
1090 g_free (s);
1093 if (p)
1095 off_t i;
1096 for (i = 0; i < edit->last_byte; i++)
1097 fputc (edit_get_byte (edit, i), p);
1098 pclose (p);
1102 /* --------------------------------------------------------------------------------------------- */
1103 /** find first character of current word */
1105 static gboolean
1106 edit_find_word_start (WEdit * edit, off_t * word_start, gsize * word_len)
1108 int c, last;
1109 gsize i;
1111 /* return if at begin of file */
1112 if (edit->curs1 <= 0)
1113 return FALSE;
1115 c = edit_get_byte (edit, edit->curs1 - 1);
1116 /* return if not at end or in word */
1117 if (is_break_char (c))
1118 return FALSE;
1120 /* search start of word to be completed */
1121 for (i = 2;; i++)
1123 /* return if at begin of file */
1124 if ((gsize) edit->curs1 < i)
1125 return FALSE;
1127 last = c;
1128 c = edit_get_byte (edit, edit->curs1 - i);
1130 if (is_break_char (c))
1132 /* return if word starts with digit */
1133 if (isdigit (last))
1134 return FALSE;
1136 *word_start = edit->curs1 - (i - 1); /* start found */
1137 *word_len = i - 1;
1138 break;
1141 /* success */
1142 return TRUE;
1145 /* --------------------------------------------------------------------------------------------- */
1147 * Get current word under cursor
1149 * @param edit editor object
1150 * @param srch mc_search object
1151 * @param word_start start word position
1153 * @return newly allocated string or NULL if no any words under cursor
1156 static char *
1157 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1159 gsize len = 0;
1160 off_t i;
1161 GString *temp;
1163 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1164 return NULL;
1166 temp = g_string_sized_new (len);
1168 for (i = 0; i < (off_t) len; i++)
1170 int chr;
1172 chr = edit_get_byte (edit, word_start + i);
1173 if (!isspace (chr))
1174 g_string_append_c (temp, chr);
1177 return g_string_free (temp, temp->len == 0);
1180 /* --------------------------------------------------------------------------------------------- */
1181 /** collect the possible completions */
1183 static gsize
1184 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1185 char *match_expr, struct selection *compl, gsize * num)
1187 gsize len = 0;
1188 gsize max_len = 0;
1189 gsize i;
1190 int skip;
1191 GString *temp;
1192 mc_search_t *srch;
1193 off_t last_byte, start = -1;
1194 char *current_word;
1196 srch = mc_search_new (match_expr, -1);
1197 if (srch == NULL)
1198 return 0;
1200 if (mc_config_get_bool
1201 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1203 last_byte = edit->last_byte;
1205 else
1207 last_byte = word_start;
1210 srch->search_type = MC_SEARCH_T_REGEX;
1211 srch->is_case_sensitive = TRUE;
1212 srch->search_fn = edit_search_cmd_callback;
1214 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1216 temp = g_string_new ("");
1218 /* collect max MAX_WORD_COMPLETIONS completions */
1219 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1221 g_string_set_size (temp, 0);
1222 start = srch->normal_offset;
1224 /* add matched completion if not yet added */
1225 for (i = 0; i < len; i++)
1227 skip = edit_get_byte (edit, start + i);
1228 if (isspace (skip))
1229 continue;
1231 /* skip current word */
1232 if (start + (off_t) i == word_start)
1233 break;
1235 g_string_append_c (temp, skip);
1238 if (temp->len == 0)
1239 continue;
1241 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1242 continue;
1244 skip = 0;
1246 for (i = 0; i < *num; i++)
1248 if (strncmp
1249 ((char *) &compl[i].text[word_len],
1250 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1252 struct selection this = compl[i];
1253 for (++i; i < *num; i++)
1255 compl[i - 1] = compl[i];
1257 compl[*num - 1] = this;
1258 skip = 1;
1259 break; /* skip it, already added */
1262 if (skip != 0)
1263 continue;
1265 if (*num == MAX_WORD_COMPLETIONS)
1267 g_free (compl[0].text);
1268 for (i = 1; i < *num; i++)
1270 compl[i - 1] = compl[i];
1272 (*num)--;
1274 #ifdef HAVE_CHARSET
1276 GString *recoded;
1277 recoded = str_convert_to_display (temp->str);
1279 if (recoded && recoded->len)
1280 g_string_assign (temp, recoded->str);
1282 g_string_free (recoded, TRUE);
1284 #endif
1285 compl[*num].text = g_strdup (temp->str);
1286 compl[*num].len = temp->len;
1287 (*num)++;
1288 start += len;
1290 /* note the maximal length needed for the completion dialog */
1291 if (len > max_len)
1292 max_len = len;
1295 mc_search_free (srch);
1296 g_string_free (temp, TRUE);
1297 g_free (current_word);
1299 return max_len;
1302 /* --------------------------------------------------------------------------------------------- */
1304 static void
1305 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1306 off_t * start_pos, off_t * end_pos, int *col1, int *col2)
1308 off_t cursor;
1309 long i, col;
1311 cursor = edit->curs1;
1312 col = edit_get_col (edit);
1314 for (i = 0; i < size; i++)
1316 if (data[i] != '\n')
1317 edit_insert (edit, data[i]);
1318 else
1319 { /* fill in and move to next line */
1320 int l;
1321 off_t p;
1323 if (edit_get_byte (edit, edit->curs1) != '\n')
1325 l = width - (edit_get_col (edit) - col);
1326 while (l > 0)
1328 edit_insert (edit, ' ');
1329 l -= space_width;
1332 for (p = edit->curs1;; p++)
1334 if (p == edit->last_byte)
1336 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1337 edit_insert_ahead (edit, '\n');
1338 p++;
1339 break;
1341 if (edit_get_byte (edit, p) == '\n')
1343 p++;
1344 break;
1347 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1348 l = col - edit_get_col (edit);
1349 while (l >= space_width)
1351 edit_insert (edit, ' ');
1352 l -= space_width;
1357 *col1 = col;
1358 *col2 = col + width;
1359 *start_pos = cursor;
1360 *end_pos = edit->curs1;
1361 edit_cursor_move (edit, cursor - edit->curs1);
1364 /* --------------------------------------------------------------------------------------------- */
1366 static int
1367 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1369 const macros_t *m1 = (const macros_t *) macro1;
1370 const macros_t *m2 = (const macros_t *) macro2;
1372 return m1->hotkey - m2->hotkey;
1375 /* --------------------------------------------------------------------------------------------- */
1377 static void
1378 edit_macro_sort_by_hotkey (void)
1380 if (macros_list != NULL && macros_list->len != 0)
1381 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1384 /* --------------------------------------------------------------------------------------------- */
1386 static gboolean
1387 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1389 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1390 macros_t *result;
1391 macros_t search_macro;
1393 (void) edit;
1395 search_macro.hotkey = hotkey;
1396 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1397 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1399 if (result != NULL && result->macro != NULL)
1401 *indx = (result - array_start);
1402 *macros = result;
1403 return TRUE;
1405 *indx = 0;
1406 return FALSE;
1409 /* --------------------------------------------------------------------------------------------- */
1410 /** returns FALSE on error */
1412 static gboolean
1413 edit_delete_macro (WEdit * edit, int hotkey)
1415 mc_config_t *macros_config = NULL;
1416 const char *section_name = "editor";
1417 gchar *macros_fname;
1418 guint indx;
1419 char *skeyname;
1420 const macros_t *macros = NULL;
1422 /* clear array of actions for current hotkey */
1423 while (edit_get_macro (edit, hotkey, &macros, &indx))
1425 if (macros->macro != NULL)
1426 g_array_free (macros->macro, TRUE);
1427 macros = NULL;
1428 g_array_remove_index (macros_list, indx);
1429 edit_macro_sort_by_hotkey ();
1432 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1433 macros_config = mc_config_init (macros_fname, FALSE);
1434 g_free (macros_fname);
1436 if (macros_config == NULL)
1437 return FALSE;
1439 skeyname = lookup_key_by_code (hotkey);
1440 while (mc_config_del_key (macros_config, section_name, skeyname))
1442 g_free (skeyname);
1443 mc_config_save_file (macros_config, NULL);
1444 mc_config_deinit (macros_config);
1445 return TRUE;
1448 /* --------------------------------------------------------------------------------------------- */
1450 * Callback for the iteration of objects in the 'editors' array.
1451 * Toggle syntax highlighting in editor object.
1453 * @param data probably WEdit object
1454 * @param user_data unused
1457 static void
1458 edit_syntax_onoff_cb (void *data, void *user_data)
1460 (void) user_data;
1462 if (edit_widget_is_editor ((const Widget *) data))
1464 WEdit *edit = (WEdit *) data;
1466 if (option_syntax_highlighting)
1467 edit_load_syntax (edit, NULL, edit->syntax_type);
1468 edit->force |= REDRAW_PAGE;
1472 /* --------------------------------------------------------------------------------------------- */
1474 * Callback for the iteration of objects in the 'editors' array.
1475 * Redraw editor object.
1477 * @param data probably WEdit object
1478 * @param user_data unused
1481 static void
1482 edit_redraw_page_cb (void *data, void *user_data)
1484 (void) user_data;
1486 if (edit_widget_is_editor ((const Widget *) data))
1487 ((WEdit *) data)->force |= REDRAW_PAGE;
1490 /* --------------------------------------------------------------------------------------------- */
1491 /*** public functions ****************************************************************************/
1492 /* --------------------------------------------------------------------------------------------- */
1494 void
1495 edit_refresh_cmd (void)
1497 clr_scr ();
1498 repaint_screen ();
1499 tty_keypad (TRUE);
1502 /* --------------------------------------------------------------------------------------------- */
1504 * Toggle syntax highlighting in all editor windows.
1506 * @param h root widget for all windows
1509 void
1510 edit_syntax_onoff_cmd (Dlg_head * h)
1512 option_syntax_highlighting = !option_syntax_highlighting;
1513 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1514 dlg_redraw (h);
1517 /* --------------------------------------------------------------------------------------------- */
1519 * Toggle tabs showing in all editor windows.
1521 * @param h root widget for all windows
1524 void
1525 edit_show_tabs_tws_cmd (Dlg_head * h)
1527 enable_show_tabs_tws = !enable_show_tabs_tws;
1528 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1529 dlg_redraw (h);
1532 /* --------------------------------------------------------------------------------------------- */
1534 * Toggle right margin showing in all editor windows.
1536 * @param h root widget for all windows
1539 void
1540 edit_show_margin_cmd (Dlg_head * h)
1542 show_right_margin = !show_right_margin;
1543 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1544 dlg_redraw (h);
1547 /* --------------------------------------------------------------------------------------------- */
1549 * Toggle line numbers showing in all editor windows.
1551 * @param h root widget for all windows
1554 void
1555 edit_show_numbers_cmd (Dlg_head * h)
1557 option_line_state = !option_line_state;
1558 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1559 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1560 dlg_redraw (h);
1563 /* --------------------------------------------------------------------------------------------- */
1565 void
1566 edit_save_mode_cmd (void)
1568 /* diaog sizes */
1569 const int DLG_X = 38;
1570 const int DLG_Y = 13;
1572 char *str_result;
1574 const char *str[] = {
1575 N_("&Quick save"),
1576 N_("&Safe save"),
1577 N_("&Do backups with following extension:")
1580 QuickWidget widgets[] = {
1581 /* 0 */
1582 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1583 /* 1 */
1584 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1585 /* 2 */
1586 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1587 /* 3 */
1588 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1589 /* 4 */
1590 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1591 QUICK_END
1594 QuickDialog dialog = {
1595 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1596 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1599 size_t i;
1600 size_t maxlen = 0;
1601 size_t w0, w1, b_len, w3;
1603 #ifdef HAVE_ASSERT_H
1604 assert (option_backup_ext != NULL);
1605 #endif
1607 /* OK/Cancel buttons */
1608 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1609 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1610 b_len = w0 + w1 + 3;
1612 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1614 w3 = 0;
1615 for (i = 0; i < 3; i++)
1617 #ifdef ENABLE_NLS
1618 str[i] = _(str[i]);
1619 #endif
1620 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1623 maxlen = max (maxlen, w3 + 4);
1625 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1627 widgets[3].u.input.len = w3;
1628 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1629 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1631 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1632 widgets[i].x_divisions = dialog.xlen;
1634 if (quick_dialog (&dialog) != B_CANCEL)
1636 g_free (option_backup_ext);
1637 option_backup_ext = str_result;
1641 /* --------------------------------------------------------------------------------------------- */
1643 void
1644 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1646 vfs_path_free (edit->filename_vpath);
1647 edit->filename_vpath = vfs_path_clone (name_vpath);
1649 if (edit->dir_vpath == NULL)
1650 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1653 /* --------------------------------------------------------------------------------------------- */
1654 /* Here we want to warn the users of overwriting an existing file,
1655 but only if they have made a change to the filename */
1656 /* returns TRUE on success */
1657 gboolean
1658 edit_save_as_cmd (WEdit * edit)
1660 /* This heads the 'Save As' dialog box */
1661 vfs_path_t *exp_vpath;
1662 int save_lock = 0;
1663 int different_filename = 0;
1665 if (!edit_check_newline (edit))
1666 return FALSE;
1668 exp_vpath = edit_get_save_file_as (edit);
1669 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1671 if (exp_vpath != NULL)
1673 if (vfs_path_len (exp_vpath) == 0)
1674 goto ret;
1675 else
1677 int rv;
1679 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1681 int file;
1682 struct stat sb;
1684 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1686 edit_error_dialog (_("Save as"),
1687 get_sys_error (_
1688 ("Cannot save: destination is not a regular file")));
1689 goto ret;
1692 different_filename = 1;
1693 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1695 if (file != -1)
1697 /* the file exists */
1698 mc_close (file);
1699 /* Overwrite the current file or cancel the operation */
1700 if (edit_query_dialog2
1701 (_("Warning"),
1702 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1703 goto ret;
1705 else
1707 edit->stat1.st_mode |= S_IWUSR;
1709 save_lock = lock_file (exp_vpath);
1711 else
1713 /* filenames equal, check if already locked */
1714 if (!edit->locked && !edit->delete_file)
1715 save_lock = lock_file (exp_vpath);
1718 if (different_filename)
1721 * Allow user to write into saved (under another name) file
1722 * even if original file had r/o user permissions.
1724 edit->stat1.st_mode |= S_IWRITE;
1727 rv = edit_save_file (edit, exp_vpath);
1728 switch (rv)
1730 case 1:
1731 /* Succesful, so unlock both files */
1732 if (different_filename)
1734 if (save_lock)
1735 unlock_file (exp_vpath);
1736 if (edit->locked)
1737 edit->locked = unlock_file (edit->filename_vpath);
1739 else
1741 if (edit->locked || save_lock)
1742 edit->locked = unlock_file (edit->filename_vpath);
1745 edit_set_filename (edit, exp_vpath);
1746 if (edit->lb != LB_ASIS)
1747 edit_reload (edit, exp_vpath);
1748 edit->modified = 0;
1749 edit->delete_file = 0;
1750 if (different_filename)
1751 edit_load_syntax (edit, NULL, edit->syntax_type);
1752 vfs_path_free (exp_vpath);
1753 edit->force |= REDRAW_COMPLETELY;
1754 return TRUE;
1755 default:
1756 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1757 /* fallthrough */
1758 case -1:
1759 /* Failed, so maintain modify (not save) lock */
1760 if (save_lock)
1761 unlock_file (exp_vpath);
1762 break;
1767 ret:
1768 vfs_path_free (exp_vpath);
1769 edit->force |= REDRAW_COMPLETELY;
1770 return FALSE;
1773 /* {{{ Macro stuff starts here */
1774 /* --------------------------------------------------------------------------------------------- */
1776 void
1777 edit_delete_macro_cmd (WEdit * edit)
1779 int hotkey;
1781 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1783 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1784 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1787 /* --------------------------------------------------------------------------------------------- */
1789 /** returns FALSE on error */
1790 gboolean
1791 edit_execute_macro (WEdit * edit, int hotkey)
1793 gboolean res = FALSE;
1795 if (hotkey != 0)
1797 const macros_t *macros;
1798 guint indx;
1800 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1801 macros->macro != NULL && macros->macro->len != 0)
1803 guint i;
1805 edit->force |= REDRAW_PAGE;
1807 for (i = 0; i < macros->macro->len; i++)
1809 const macro_action_t *m_act;
1811 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1812 edit_execute_cmd (edit, m_act->action, m_act->ch);
1813 res = TRUE;
1818 return res;
1821 /* --------------------------------------------------------------------------------------------- */
1823 /** returns FALSE on error */
1824 gboolean
1825 edit_store_macro_cmd (WEdit * edit)
1827 int i;
1828 int hotkey;
1829 GString *marcros_string;
1830 mc_config_t *macros_config = NULL;
1831 const char *section_name = "editor";
1832 gchar *macros_fname;
1833 GArray *macros; /* current macro */
1834 int tmp_act;
1835 gboolean have_macro = FALSE;
1836 char *skeyname = NULL;
1838 hotkey =
1839 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1840 if (hotkey == ESC_CHAR)
1841 return FALSE;
1843 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1845 /* return FALSE if try assign macro into restricted hotkeys */
1846 if (tmp_act == CK_MacroStartRecord
1847 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1848 return FALSE;
1850 edit_delete_macro (edit, hotkey);
1852 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1853 macros_config = mc_config_init (macros_fname, FALSE);
1854 g_free (macros_fname);
1856 if (macros_config == NULL)
1857 return FALSE;
1859 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1861 marcros_string = g_string_sized_new (250);
1862 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1864 skeyname = lookup_key_by_code (hotkey);
1866 for (i = 0; i < macro_index; i++)
1868 macro_action_t m_act;
1869 const char *action_name;
1871 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1873 if (action_name == NULL)
1874 break;
1876 m_act.action = record_macro_buf[i].action;
1877 m_act.ch = record_macro_buf[i].ch;
1878 g_array_append_val (macros, m_act);
1879 have_macro = TRUE;
1880 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1881 (int) record_macro_buf[i].ch);
1883 if (have_macro)
1885 macros_t macro;
1886 macro.hotkey = hotkey;
1887 macro.macro = macros;
1888 g_array_append_val (macros_list, macro);
1889 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1891 else
1892 mc_config_del_key (macros_config, section_name, skeyname);
1894 g_free (skeyname);
1895 edit_macro_sort_by_hotkey ();
1897 g_string_free (marcros_string, TRUE);
1898 mc_config_save_file (macros_config, NULL);
1899 mc_config_deinit (macros_config);
1900 return TRUE;
1903 /* --------------------------------------------------------------------------------------------- */
1905 gboolean
1906 edit_repeat_macro_cmd (WEdit * edit)
1908 int i, j;
1909 char *f;
1910 long count_repeat;
1911 char *error = NULL;
1913 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1914 if (f == NULL || *f == '\0')
1916 g_free (f);
1917 return FALSE;
1920 count_repeat = strtol (f, &error, 0);
1922 if (*error != '\0')
1924 g_free (f);
1925 return FALSE;
1928 g_free (f);
1930 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1931 edit->force |= REDRAW_PAGE;
1933 for (j = 0; j < count_repeat; j++)
1934 for (i = 0; i < macro_index; i++)
1935 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1936 edit_update_screen (edit);
1937 return TRUE;
1940 /* --------------------------------------------------------------------------------------------- */
1941 /** return FALSE on error */
1943 gboolean
1944 edit_load_macro_cmd (WEdit * edit)
1946 mc_config_t *macros_config = NULL;
1947 gchar **profile_keys, **keys;
1948 gchar **values, **curr_values;
1949 gsize len, values_len;
1950 const char *section_name = "editor";
1951 gchar *macros_fname;
1952 int hotkey;
1954 (void) edit;
1956 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1957 macros_config = mc_config_init (macros_fname, TRUE);
1958 g_free (macros_fname);
1960 if (macros_config == NULL)
1961 return FALSE;
1963 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1964 while (*profile_keys != NULL)
1966 gboolean have_macro;
1967 GArray *macros;
1968 macros_t macro;
1970 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1972 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1973 *profile_keys, &values_len);
1974 hotkey = lookup_key (*profile_keys, NULL);
1975 have_macro = FALSE;
1977 while (*curr_values != NULL && *curr_values[0] != '\0')
1979 char **macro_pair = NULL;
1981 macro_pair = g_strsplit (*curr_values, ":", 2);
1983 if (macro_pair != NULL)
1985 macro_action_t m_act;
1986 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1987 m_act.action = 0;
1988 else
1990 m_act.action = keybind_lookup_action (macro_pair[0]);
1991 g_free (macro_pair[0]);
1992 macro_pair[0] = NULL;
1994 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1995 m_act.ch = -1;
1996 else
1998 m_act.ch = strtol (macro_pair[1], NULL, 0);
1999 g_free (macro_pair[1]);
2000 macro_pair[1] = NULL;
2002 if (m_act.action != 0)
2004 /* a shell command */
2005 if ((m_act.action / CK_PipeBlock (0)) == 1)
2007 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2008 m_act.ch = -1;
2010 g_array_append_val (macros, m_act);
2011 have_macro = TRUE;
2013 g_strfreev (macro_pair);
2014 macro_pair = NULL;
2016 curr_values++;
2018 if (have_macro)
2020 macro.hotkey = hotkey;
2021 macro.macro = macros;
2022 g_array_append_val (macros_list, macro);
2024 profile_keys++;
2025 g_strfreev (values);
2027 g_strfreev (keys);
2028 mc_config_deinit (macros_config);
2029 edit_macro_sort_by_hotkey ();
2030 return TRUE;
2033 /* }}} Macro stuff end here */
2035 /* --------------------------------------------------------------------------------------------- */
2036 /** returns TRUE on success */
2038 gboolean
2039 edit_save_confirm_cmd (WEdit * edit)
2041 char *f = NULL;
2043 if (edit->filename_vpath == NULL)
2044 return edit_save_as_cmd (edit);
2046 if (!edit_check_newline (edit))
2047 return FALSE;
2049 if (edit_confirm_save)
2051 char *filename;
2052 gboolean ok;
2054 filename = vfs_path_to_str (edit->filename_vpath);
2055 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2056 g_free (filename);
2057 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2058 g_free (f);
2059 if (!ok)
2060 return FALSE;
2062 return edit_save_cmd (edit);
2065 /* --------------------------------------------------------------------------------------------- */
2067 * Ask file to edit and load it.
2069 * @returns TRUE on success or cancel of ask.
2072 gboolean
2073 edit_load_cmd (Dlg_head * h)
2075 char *exp;
2076 gboolean ret = TRUE; /* possible cancel */
2078 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2079 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2081 if (exp != NULL && *exp != '\0')
2083 vfs_path_t *exp_vpath;
2085 exp_vpath = vfs_path_from_str (exp);
2086 ret = edit_load_file_from_filename (h, exp_vpath);
2087 vfs_path_free (exp_vpath);
2090 g_free (exp);
2092 return ret;
2095 /* --------------------------------------------------------------------------------------------- */
2097 * Load syntax file to edit.
2099 * @returns TRUE on success
2102 gboolean
2103 edit_load_syntax_file (Dlg_head * h)
2105 vfs_path_t *extdir_vpath;
2106 int dir = 0;
2107 gboolean ret = FALSE;
2109 if (geteuid () == 0)
2110 dir = query_dialog (_("Syntax file edit"),
2111 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2112 _("&User"), _("&System wide"));
2114 extdir_vpath =
2115 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2116 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2118 vfs_path_free (extdir_vpath);
2119 extdir_vpath =
2120 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2123 if (dir == 0)
2125 vfs_path_t *user_syntax_file_vpath;
2127 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2128 check_for_default (extdir_vpath, user_syntax_file_vpath);
2129 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2130 vfs_path_free (user_syntax_file_vpath);
2132 else if (dir == 1)
2133 ret = edit_load_file_from_filename (h, extdir_vpath);
2135 vfs_path_free (extdir_vpath);
2137 return ret;
2140 /* --------------------------------------------------------------------------------------------- */
2142 * Load menu file to edit.
2144 * @returns TRUE on success
2147 gboolean
2148 edit_load_menu_file (Dlg_head * h)
2150 vfs_path_t *buffer_vpath;
2151 vfs_path_t *menufile_vpath;
2152 int dir;
2153 gboolean ret;
2155 dir = query_dialog (_("Menu edit"),
2156 _("Which menu file do you want to edit?"), D_NORMAL,
2157 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2159 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2160 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2162 vfs_path_free (menufile_vpath);
2163 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2166 switch (dir)
2168 case 0:
2169 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2170 check_for_default (menufile_vpath, buffer_vpath);
2171 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2172 break;
2174 case 1:
2175 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2176 check_for_default (menufile_vpath, buffer_vpath);
2177 break;
2179 case 2:
2180 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2181 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2183 vfs_path_free (buffer_vpath);
2184 buffer_vpath =
2185 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2187 break;
2189 default:
2190 vfs_path_free (menufile_vpath);
2191 return FALSE;
2194 ret = edit_load_file_from_filename (h, buffer_vpath);
2196 vfs_path_free (buffer_vpath);
2197 vfs_path_free (menufile_vpath);
2199 return ret;
2202 /* --------------------------------------------------------------------------------------------- */
2204 * Close window with opened file.
2206 * @returns TRUE if file was closed.
2209 gboolean
2210 edit_close_cmd (WEdit * edit)
2212 gboolean ret;
2214 ret = (edit != NULL) && edit_ok_to_exit (edit);
2216 if (ret)
2218 Dlg_head *h = ((Widget *) edit)->owner;
2220 if (edit->locked != 0)
2221 unlock_file (edit->filename_vpath);
2223 del_widget (edit);
2225 if (edit_widget_is_editor ((Widget *) h->current->data))
2226 edit = (WEdit *) h->current->data;
2227 else
2229 edit = find_editor (h);
2230 if (edit != NULL)
2231 dlg_set_top_widget (edit);
2235 if (edit != NULL)
2236 edit->force |= REDRAW_COMPLETELY;
2238 return ret;
2241 /* --------------------------------------------------------------------------------------------- */
2243 if mark2 is -1 then marking is from mark1 to the cursor.
2244 Otherwise its between the markers. This handles this.
2245 Returns 1 if no text is marked.
2249 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2251 if (edit->mark1 != edit->mark2)
2253 off_t start_bol, start_eol;
2254 off_t end_bol, end_eol;
2255 off_t diff1, diff2;
2256 long col1, col2;
2257 long end_mark_curs;
2259 if (edit->end_mark_curs < 0)
2260 end_mark_curs = edit->curs1;
2261 else
2262 end_mark_curs = edit->end_mark_curs;
2264 if (edit->mark2 >= 0)
2266 *start_mark = min (edit->mark1, edit->mark2);
2267 *end_mark = max (edit->mark1, edit->mark2);
2269 else
2271 *start_mark = min (edit->mark1, end_mark_curs);
2272 *end_mark = max (edit->mark1, end_mark_curs);
2273 edit->column2 = edit->curs_col + edit->over_col;
2276 if (edit->column_highlight
2277 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2278 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2280 start_bol = edit_bol (edit, *start_mark);
2281 start_eol = edit_eol (edit, start_bol - 1) + 1;
2282 end_bol = edit_bol (edit, *end_mark);
2283 end_eol = edit_eol (edit, *end_mark);
2284 col1 = min (edit->column1, edit->column2);
2285 col2 = max (edit->column1, edit->column2);
2287 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2288 edit_move_forward3 (edit, start_bol, col1, 0);
2289 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2290 edit_move_forward3 (edit, end_bol, col1, 0);
2292 *start_mark -= diff1;
2293 *end_mark += diff2;
2294 *start_mark = max (*start_mark, start_eol);
2295 *end_mark = min (*end_mark, end_eol);
2297 return 0;
2299 else
2301 *start_mark = *end_mark = 0;
2302 edit->column2 = edit->column1 = 0;
2303 return 1;
2307 /* --------------------------------------------------------------------------------------------- */
2309 void
2310 edit_insert_over (WEdit * edit)
2312 int i;
2314 for (i = 0; i < edit->over_col; i++)
2316 edit_insert (edit, ' ');
2318 edit->over_col = 0;
2321 /* --------------------------------------------------------------------------------------------- */
2323 off_t
2324 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2325 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
2327 off_t cursor;
2328 int col;
2329 off_t blocklen = -1, width = 0;
2330 unsigned char *data;
2332 cursor = edit->curs1;
2333 col = edit_get_col (edit);
2334 data = g_malloc0 (TEMP_BUF_LEN);
2336 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2338 off_t i;
2339 for (width = 0; width < blocklen; width++)
2341 if (data[width] == '\n')
2342 break;
2344 for (i = 0; i < blocklen; i++)
2346 if (data[i] == '\n')
2347 { /* fill in and move to next line */
2348 long l;
2349 off_t p;
2350 if (edit_get_byte (edit, edit->curs1) != '\n')
2352 l = width - (edit_get_col (edit) - col);
2353 while (l > 0)
2355 edit_insert (edit, ' ');
2356 l -= space_width;
2359 for (p = edit->curs1;; p++)
2361 if (p == edit->last_byte)
2363 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2364 edit_insert_ahead (edit, '\n');
2365 p++;
2366 break;
2368 if (edit_get_byte (edit, p) == '\n')
2370 p++;
2371 break;
2374 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2375 l = col - edit_get_col (edit);
2376 while (l >= space_width)
2378 edit_insert (edit, ' ');
2379 l -= space_width;
2381 continue;
2383 edit_insert (edit, data[i]);
2386 *col1 = col;
2387 *col2 = col + width;
2388 *start_pos = cursor;
2389 *end_pos = edit->curs1;
2390 edit_cursor_move (edit, cursor - edit->curs1);
2391 g_free (data);
2393 return blocklen;
2396 /* --------------------------------------------------------------------------------------------- */
2398 void
2399 edit_block_copy_cmd (WEdit * edit)
2401 off_t start_mark, end_mark, current = edit->curs1;
2402 long col_delta = 0;
2403 off_t mark1, mark2;
2404 int c1, c2;
2405 int size;
2406 unsigned char *copy_buf;
2408 edit_update_curs_col (edit);
2409 if (eval_marks (edit, &start_mark, &end_mark))
2410 return;
2412 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2414 /* all that gets pushed are deletes hence little space is used on the stack */
2416 edit_push_markers (edit);
2418 if (edit->column_highlight)
2420 col_delta = abs (edit->column2 - edit->column1);
2421 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2423 else
2425 while (size--)
2426 edit_insert_ahead (edit, copy_buf[size]);
2429 g_free (copy_buf);
2430 edit_scroll_screen_over_cursor (edit);
2432 if (edit->column_highlight)
2433 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2434 else if (start_mark < current && end_mark > current)
2435 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2437 edit->force |= REDRAW_PAGE;
2441 /* --------------------------------------------------------------------------------------------- */
2443 void
2444 edit_block_move_cmd (WEdit * edit)
2446 off_t current;
2447 unsigned char *copy_buf = NULL;
2448 off_t start_mark, end_mark;
2449 long line;
2451 if (eval_marks (edit, &start_mark, &end_mark))
2452 return;
2454 line = edit->curs_line;
2455 if (edit->mark2 < 0)
2456 edit_mark_cmd (edit, FALSE);
2457 edit_push_markers (edit);
2459 if (edit->column_highlight)
2461 off_t mark1, mark2;
2462 int size;
2463 int b_width = 0;
2464 int c1, c2;
2465 int x, x2;
2467 c1 = min (edit->column1, edit->column2);
2468 c2 = max (edit->column1, edit->column2);
2469 b_width = (c2 - c1);
2471 edit_update_curs_col (edit);
2473 x = edit->curs_col;
2474 x2 = x + edit->over_col;
2476 /* do nothing when cursor inside first line of selected area */
2477 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2478 return;
2480 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2482 if (x > c2)
2483 x -= b_width;
2484 else if (x > c1 && x <= c2)
2485 x = c1;
2487 /* save current selection into buffer */
2488 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2490 /* remove current selection */
2491 edit_block_delete_cmd (edit);
2493 edit->over_col = max (0, edit->over_col - b_width);
2494 /* calculate the cursor pos after delete block */
2495 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2496 edit_cursor_move (edit, current - edit->curs1);
2497 edit_scroll_screen_over_cursor (edit);
2499 /* add TWS if need before block insertion */
2500 if (option_cursor_beyond_eol && edit->over_col > 0)
2501 edit_insert_over (edit);
2503 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2504 edit_set_markers (edit, mark1, mark2, c1, c2);
2506 else
2508 off_t count;
2510 current = edit->curs1;
2511 copy_buf = g_malloc0 (end_mark - start_mark);
2512 edit_cursor_move (edit, start_mark - edit->curs1);
2513 edit_scroll_screen_over_cursor (edit);
2514 count = start_mark;
2515 while (count < end_mark)
2517 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2518 count++;
2520 edit_scroll_screen_over_cursor (edit);
2521 edit_cursor_move (edit,
2522 current - edit->curs1 -
2523 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2524 edit_scroll_screen_over_cursor (edit);
2525 while (count-- > start_mark)
2526 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2527 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2530 edit_scroll_screen_over_cursor (edit);
2531 g_free (copy_buf);
2532 edit->force |= REDRAW_PAGE;
2535 /* --------------------------------------------------------------------------------------------- */
2536 /** returns 1 if canceelled by user */
2539 edit_block_delete_cmd (WEdit * edit)
2541 off_t start_mark, end_mark;
2542 if (eval_marks (edit, &start_mark, &end_mark))
2544 edit_delete_line (edit);
2545 return 0;
2547 return edit_block_delete (edit);
2550 /* --------------------------------------------------------------------------------------------- */
2551 /** call with edit = 0 before shutdown to close memory leaks */
2553 void
2554 edit_replace_cmd (WEdit * edit, int again)
2556 /* 1 = search string, 2 = replace with */
2557 static char *saved1 = NULL; /* saved default[123] */
2558 static char *saved2 = NULL;
2559 char *input1 = NULL; /* user input from the dialog */
2560 char *input2 = NULL;
2561 GString *input2_str = NULL;
2562 char *disp1 = NULL;
2563 char *disp2 = NULL;
2564 long times_replaced = 0;
2565 gboolean once_found = FALSE;
2567 if (!edit)
2569 g_free (saved1), saved1 = NULL;
2570 g_free (saved2), saved2 = NULL;
2571 return;
2574 edit->force |= REDRAW_COMPLETELY;
2576 if (again && !saved1 && !saved2)
2577 again = 0;
2579 if (again)
2581 input1 = g_strdup (saved1 ? saved1 : "");
2582 input2 = g_strdup (saved2 ? saved2 : "");
2584 else
2586 char *tmp_inp1, *tmp_inp2;
2588 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2589 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2591 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2593 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2595 g_free (disp1);
2596 g_free (disp2);
2598 if (input1 == NULL || *input1 == '\0')
2600 edit->force = REDRAW_COMPLETELY;
2601 goto cleanup;
2604 tmp_inp1 = input1;
2605 tmp_inp2 = input2;
2606 input1 = edit_replace_cmd__conv_to_input (input1);
2607 input2 = edit_replace_cmd__conv_to_input (input2);
2608 g_free (tmp_inp1);
2609 g_free (tmp_inp2);
2611 g_free (saved1), saved1 = g_strdup (input1);
2612 g_free (saved2), saved2 = g_strdup (input2);
2614 mc_search_free (edit->search);
2615 edit->search = NULL;
2618 input2_str = g_string_new (input2);
2620 if (!edit->search)
2622 edit->search = mc_search_new (input1, -1);
2623 if (edit->search == NULL)
2625 edit->search_start = edit->curs1;
2626 goto cleanup;
2628 edit->search->search_type = edit_search_options.type;
2629 edit->search->is_all_charsets = edit_search_options.all_codepages;
2630 edit->search->is_case_sensitive = edit_search_options.case_sens;
2631 edit->search->whole_words = edit_search_options.whole_words;
2632 edit->search->search_fn = edit_search_cmd_callback;
2633 edit->search_line_type = edit_get_search_line_type (edit->search);
2634 edit_search_fix_search_start_if_selection (edit);
2637 if (edit->found_len && edit->search_start == edit->found_start + 1
2638 && edit_search_options.backwards)
2639 edit->search_start--;
2641 if (edit->found_len && edit->search_start == edit->found_start - 1
2642 && !edit_search_options.backwards)
2643 edit->search_start++;
2647 gsize len = 0;
2649 if (!editcmd_find (edit, &len))
2651 if (!(edit->search->error == MC_SEARCH_E_OK ||
2652 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2654 edit_error_dialog (_("Search"), edit->search->error_str);
2656 break;
2658 once_found = TRUE;
2660 edit->search_start = edit->search->normal_offset;
2661 /*returns negative on not found or error in pattern */
2663 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2665 gsize i;
2666 GString *repl_str;
2668 edit->found_start = edit->search_start;
2669 i = edit->found_len = len;
2671 edit_cursor_move (edit, edit->search_start - edit->curs1);
2672 edit_scroll_screen_over_cursor (edit);
2674 if (edit->replace_mode == 0)
2676 long l;
2677 int prompt;
2679 l = (long) (edit->curs_row - edit->widget.lines / 3);
2680 if (l > 0)
2681 edit_scroll_downward (edit, l);
2682 if (l < 0)
2683 edit_scroll_upward (edit, -l);
2685 edit_scroll_screen_over_cursor (edit);
2686 edit->force |= REDRAW_PAGE;
2687 edit_render_keypress (edit);
2689 /*so that undo stops at each query */
2690 edit_push_key_press (edit);
2691 /* and prompt 2/3 down */
2692 disp1 = edit_replace_cmd__conv_to_display (saved1);
2693 disp2 = edit_replace_cmd__conv_to_display (saved2);
2694 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2695 g_free (disp1);
2696 g_free (disp2);
2698 if (prompt == B_REPLACE_ALL)
2699 edit->replace_mode = 1;
2700 else if (prompt == B_SKIP_REPLACE)
2702 if (edit_search_options.backwards)
2703 edit->search_start--;
2704 else
2705 edit->search_start++;
2706 continue; /* loop */
2708 else if (prompt == B_CANCEL)
2710 edit->replace_mode = -1;
2711 break; /* loop */
2715 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2717 if (edit->search->error != MC_SEARCH_E_OK)
2719 edit_error_dialog (_("Replace"), edit->search->error_str);
2720 g_string_free (repl_str, TRUE);
2721 break;
2724 /* delete then insert new */
2725 for (i = 0; i < len; i++)
2726 edit_delete (edit, 1);
2728 for (i = 0; i < repl_str->len; i++)
2729 edit_insert (edit, repl_str->str[i]);
2731 edit->found_len = repl_str->len;
2732 g_string_free (repl_str, TRUE);
2733 times_replaced++;
2735 /* so that we don't find the same string again */
2736 if (edit_search_options.backwards)
2738 edit->search_start--;
2740 else
2742 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2744 if (edit->search_start >= edit->last_byte)
2745 break;
2748 edit_scroll_screen_over_cursor (edit);
2750 else
2752 /* try and find from right here for next search */
2753 edit->search_start = edit->curs1;
2754 edit_update_curs_col (edit);
2756 edit->force |= REDRAW_PAGE;
2757 edit_render_keypress (edit);
2759 if (times_replaced == 0)
2760 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2761 break;
2764 while (edit->replace_mode >= 0);
2766 edit_scroll_screen_over_cursor (edit);
2767 edit->force |= REDRAW_COMPLETELY;
2768 edit_render_keypress (edit);
2770 if ((edit->replace_mode == 1) && (times_replaced != 0))
2771 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2773 cleanup:
2774 g_free (input1);
2775 g_free (input2);
2776 if (input2_str != NULL)
2777 g_string_free (input2_str, TRUE);
2780 /* --------------------------------------------------------------------------------------------- */
2782 mc_search_cbret_t
2783 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2785 *current_char = edit_get_byte ((WEdit *) user_data, (off_t) char_offset);
2786 return MC_SEARCH_CB_OK;
2789 /* --------------------------------------------------------------------------------------------- */
2791 void
2792 edit_search_cmd (WEdit * edit, gboolean again)
2795 if (edit == NULL)
2796 return;
2798 if (!again)
2799 edit_search (edit);
2800 else if (edit->last_search_string != NULL)
2801 edit_do_search (edit);
2802 else
2804 /* find last search string in history */
2805 GList *history;
2807 history = history_get (MC_HISTORY_SHARED_SEARCH);
2808 if (history != NULL && history->data != NULL)
2810 edit->last_search_string = (char *) history->data;
2811 history->data = NULL;
2812 history = g_list_first (history);
2813 g_list_foreach (history, (GFunc) g_free, NULL);
2814 g_list_free (history);
2816 edit->search = mc_search_new (edit->last_search_string, -1);
2817 if (edit->search == NULL)
2819 /* if not... then ask for an expression */
2820 g_free (edit->last_search_string);
2821 edit->last_search_string = NULL;
2822 edit_search (edit);
2824 else
2826 edit->search->search_type = edit_search_options.type;
2827 edit->search->is_all_charsets = edit_search_options.all_codepages;
2828 edit->search->is_case_sensitive = edit_search_options.case_sens;
2829 edit->search->whole_words = edit_search_options.whole_words;
2830 edit->search->search_fn = edit_search_cmd_callback;
2831 edit->search_line_type = edit_get_search_line_type (edit->search);
2832 edit_do_search (edit);
2835 else
2837 /* if not... then ask for an expression */
2838 g_free (edit->last_search_string);
2839 edit->last_search_string = NULL;
2840 edit_search (edit);
2846 /* --------------------------------------------------------------------------------------------- */
2848 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2850 * @returns TRUE if it's OK to exit, FALSE to continue editing.
2853 gboolean
2854 edit_ok_to_exit (WEdit * edit)
2856 char *fname = (char *) N_("[NoName]");
2857 char *msg;
2858 int act;
2860 if (!edit->modified)
2861 return TRUE;
2863 if (edit->filename_vpath != NULL)
2864 fname = vfs_path_to_str (edit->filename_vpath);
2865 #ifdef ENABLE_NLS
2866 else
2867 fname = g_strdup (_(fname));
2868 #else
2869 else
2870 fname = g_strdup (fname);
2871 #endif
2873 if (!mc_global.midnight_shutdown)
2875 if (!edit_check_newline (edit))
2877 g_free (fname);
2878 return FALSE;
2881 query_set_sel (2);
2883 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2884 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2886 else
2888 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2889 fname);
2890 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2892 /* Esc is No */
2893 if (act == -1)
2894 act = 1;
2897 g_free (msg);
2898 g_free (fname);
2900 switch (act)
2902 case 0: /* Yes */
2903 edit_push_markers (edit);
2904 edit_set_markers (edit, 0, 0, 0, 0);
2905 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2906 return mc_global.midnight_shutdown;
2907 break;
2908 case 1: /* No */
2909 break;
2910 case 2: /* Cancel quit */
2911 case -1: /* Esc */
2912 return FALSE;
2915 return TRUE;
2918 /* --------------------------------------------------------------------------------------------- */
2919 /** save block, returns TRUE on success */
2921 gboolean
2922 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2924 int len, file;
2925 vfs_path_t *vpath;
2927 vpath = vfs_path_from_str (filename);
2928 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2929 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2930 vfs_path_free (vpath);
2931 if (file == -1)
2932 return FALSE;
2934 if (edit->column_highlight)
2936 int r;
2938 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2939 if (r > 0)
2941 unsigned char *block, *p;
2943 p = block = edit_get_block (edit, start, finish, &len);
2944 while (len)
2946 r = mc_write (file, p, len);
2947 if (r < 0)
2948 break;
2949 p += r;
2950 len -= r;
2952 g_free (block);
2955 else
2957 unsigned char *buf;
2958 off_t i = start;
2959 off_t end;
2961 len = finish - start;
2962 buf = g_malloc0 (TEMP_BUF_LEN);
2963 while (start != finish)
2965 end = min (finish, start + TEMP_BUF_LEN);
2966 for (; i < end; i++)
2967 buf[i - start] = edit_get_byte (edit, i);
2968 len -= mc_write (file, (char *) buf, end - start);
2969 start = end;
2971 g_free (buf);
2973 mc_close (file);
2975 return (len == 0);
2978 /* --------------------------------------------------------------------------------------------- */
2980 void
2981 edit_paste_from_history (WEdit * edit)
2983 (void) edit;
2984 edit_error_dialog (_("Error"), _("This function is not implemented"));
2987 /* --------------------------------------------------------------------------------------------- */
2989 gboolean
2990 edit_copy_to_X_buf_cmd (WEdit * edit)
2992 off_t start_mark, end_mark;
2994 if (eval_marks (edit, &start_mark, &end_mark))
2995 return TRUE;
2996 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2998 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2999 return FALSE;
3001 /* try use external clipboard utility */
3002 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3004 return TRUE;
3007 /* --------------------------------------------------------------------------------------------- */
3009 gboolean
3010 edit_cut_to_X_buf_cmd (WEdit * edit)
3012 off_t start_mark, end_mark;
3014 if (eval_marks (edit, &start_mark, &end_mark))
3015 return TRUE;
3016 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3018 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3019 return FALSE;
3021 /* try use external clipboard utility */
3022 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3024 edit_block_delete_cmd (edit);
3025 edit_mark_cmd (edit, TRUE);
3027 return TRUE;
3030 /* --------------------------------------------------------------------------------------------- */
3032 gboolean
3033 edit_paste_from_X_buf_cmd (WEdit * edit)
3035 vfs_path_t *tmp;
3036 gboolean ret;
3038 /* try use external clipboard utility */
3039 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3040 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3041 ret = (edit_insert_file (edit, tmp) >= 0);
3042 vfs_path_free (tmp);
3044 return ret;
3047 /* --------------------------------------------------------------------------------------------- */
3049 * Ask user for the line and go to that line.
3050 * Negative numbers mean line from the end (i.e. -1 is the last line).
3053 void
3054 edit_goto_cmd (WEdit * edit)
3056 char *f;
3057 static long line = 0; /* line as typed, saved as default */
3058 long l;
3059 char *error;
3060 char s[32];
3062 g_snprintf (s, sizeof (s), "%ld", line);
3063 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3064 if (!f)
3065 return;
3067 if (!*f)
3069 g_free (f);
3070 return;
3073 l = strtol (f, &error, 0);
3074 if (*error)
3076 g_free (f);
3077 return;
3080 line = l;
3081 if (l < 0)
3082 l = edit->total_lines + l + 2;
3083 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3084 edit_move_to_line (edit, l - 1);
3085 edit->force |= REDRAW_COMPLETELY;
3086 g_free (f);
3090 /* --------------------------------------------------------------------------------------------- */
3091 /** Return TRUE on success */
3093 gboolean
3094 edit_save_block_cmd (WEdit * edit)
3096 off_t start_mark, end_mark;
3097 char *exp, *tmp;
3098 gboolean ret = FALSE;
3100 if (eval_marks (edit, &start_mark, &end_mark))
3101 return TRUE;
3103 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3104 exp =
3105 input_expand_dialog (_("Save block"), _("Enter file name:"),
3106 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3107 g_free (tmp);
3108 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3110 if (exp != NULL && *exp != '\0')
3112 if (edit_save_block (edit, exp, start_mark, end_mark))
3113 ret = TRUE;
3114 else
3115 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3117 edit->force |= REDRAW_COMPLETELY;
3120 g_free (exp);
3122 return ret;
3126 /* --------------------------------------------------------------------------------------------- */
3128 /** returns TRUE on success */
3129 gboolean
3130 edit_insert_file_cmd (WEdit * edit)
3132 char *tmp;
3133 char *exp;
3134 gboolean ret = FALSE;
3136 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3137 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3138 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3139 g_free (tmp);
3141 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3143 if (exp != NULL && *exp != '\0')
3145 vfs_path_t *exp_vpath;
3147 exp_vpath = vfs_path_from_str (exp);
3148 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3149 vfs_path_free (exp_vpath);
3151 if (!ret)
3152 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3155 g_free (exp);
3157 edit->force |= REDRAW_COMPLETELY;
3158 return ret;
3161 /* --------------------------------------------------------------------------------------------- */
3162 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3165 edit_sort_cmd (WEdit * edit)
3167 static char *old = 0;
3168 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3169 off_t start_mark, end_mark;
3170 int e;
3172 if (eval_marks (edit, &start_mark, &end_mark))
3174 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3175 return 0;
3178 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3179 edit_save_block (edit, tmp, start_mark, end_mark);
3180 g_free (tmp);
3182 exp = input_dialog (_("Run sort"),
3183 _("Enter sort options (see manpage) separated by whitespace:"),
3184 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3186 if (!exp)
3187 return 1;
3188 g_free (old);
3189 old = exp;
3190 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3191 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3192 tmp =
3193 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3194 " > ", tmp_edit_temp_name, (char *) NULL);
3195 g_free (tmp_edit_temp_name);
3196 g_free (tmp_edit_block_name);
3198 e = system (tmp);
3199 g_free (tmp);
3200 if (e)
3202 if (e == -1 || e == 127)
3204 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3206 else
3208 char q[8];
3209 sprintf (q, "%d ", e);
3210 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3211 edit_error_dialog (_("Sort"), tmp);
3212 g_free (tmp);
3214 return -1;
3217 edit->force |= REDRAW_COMPLETELY;
3219 if (edit_block_delete_cmd (edit))
3220 return 1;
3223 vfs_path_t *tmp_vpath;
3225 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3226 edit_insert_file (edit, tmp_vpath);
3227 vfs_path_free (tmp_vpath);
3229 return 0;
3232 /* --------------------------------------------------------------------------------------------- */
3234 * Ask user for a command, execute it and paste its output back to the
3235 * editor.
3239 edit_ext_cmd (WEdit * edit)
3241 char *exp, *tmp, *tmp_edit_temp_file;
3242 int e;
3244 exp =
3245 input_dialog (_("Paste output of external command"),
3246 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3248 if (!exp)
3249 return 1;
3251 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3252 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3253 g_free (tmp_edit_temp_file);
3254 e = system (tmp);
3255 g_free (tmp);
3256 g_free (exp);
3258 if (e)
3260 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3261 return -1;
3264 edit->force |= REDRAW_COMPLETELY;
3267 vfs_path_t *tmp_vpath;
3269 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3270 edit_insert_file (edit, tmp_vpath);
3271 vfs_path_free (tmp_vpath);
3273 return 0;
3276 /* --------------------------------------------------------------------------------------------- */
3277 /** if block is 1, a block must be highlighted and the shell command
3278 processes it. If block is 0 the shell command is a straight system
3279 command, that just produces some output which is to be inserted */
3281 void
3282 edit_block_process_cmd (WEdit * edit, int macro_number)
3284 char *fname;
3285 char *macros_fname = NULL;
3287 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3288 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3289 user_menu (edit, macros_fname, 0);
3290 g_free (fname);
3291 g_free (macros_fname);
3292 edit->force |= REDRAW_COMPLETELY;
3295 /* --------------------------------------------------------------------------------------------- */
3297 void
3298 edit_mail_dialog (WEdit * edit)
3300 char *tmail_to;
3301 char *tmail_subject;
3302 char *tmail_cc;
3304 static char *mail_cc_last = 0;
3305 static char *mail_subject_last = 0;
3306 static char *mail_to_last = 0;
3308 QuickWidget quick_widgets[] = {
3309 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3310 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3311 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3312 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3313 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3314 &tmail_subject),
3315 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3316 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3317 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3318 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3319 QUICK_END
3322 QuickDialog Quick_input = {
3323 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3324 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3327 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3328 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3329 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3331 if (quick_dialog (&Quick_input) != B_CANCEL)
3333 g_free (mail_cc_last);
3334 g_free (mail_subject_last);
3335 g_free (mail_to_last);
3336 mail_cc_last = tmail_cc;
3337 mail_subject_last = tmail_subject;
3338 mail_to_last = tmail_to;
3339 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3344 /*******************/
3345 /* Word Completion */
3346 /*******************/
3348 /* --------------------------------------------------------------------------------------------- */
3350 * Complete current word using regular expression search
3351 * backwards beginning at the current cursor position.
3354 void
3355 edit_complete_word_cmd (WEdit * edit)
3357 gsize i, max_len, word_len = 0, num_compl = 0;
3358 off_t word_start = 0;
3359 unsigned char *bufpos;
3360 char *match_expr;
3361 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3363 /* search start of word to be completed */
3364 if (!edit_find_word_start (edit, &word_start, &word_len))
3365 return;
3367 /* prepare match expression */
3368 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3370 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3371 match_expr =
3372 g_strdup_printf
3373 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3374 (int) word_len, bufpos);
3376 /* collect the possible completions */
3377 /* start search from begin to end of file */
3378 max_len =
3379 edit_collect_completions (edit, word_start, word_len, match_expr,
3380 (struct selection *) &compl, &num_compl);
3382 if (num_compl > 0)
3384 /* insert completed word if there is only one match */
3385 if (num_compl == 1)
3387 for (i = word_len; i < compl[0].len; i++)
3388 edit_insert (edit, *(compl[0].text + i));
3390 /* more than one possible completion => ask the user */
3391 else
3393 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3394 /* !!! pressed again the selection dialog pops up, but that !!! */
3395 /* !!! seems to require a further internal state !!! */
3396 /*tty_beep (); */
3398 /* let the user select the preferred completion */
3399 editcmd_dialog_completion_show (edit, max_len, word_len,
3400 (struct selection *) &compl, num_compl);
3404 g_free (match_expr);
3405 /* release memory before return */
3406 for (i = 0; i < num_compl; i++)
3407 g_free (compl[i].text);
3410 /* --------------------------------------------------------------------------------------------- */
3412 #ifdef HAVE_CHARSET
3413 void
3414 edit_select_codepage_cmd (WEdit * edit)
3416 if (do_select_codepage ())
3417 edit_set_codeset (edit);
3419 edit->force = REDRAW_PAGE;
3420 send_message ((Widget *) edit, WIDGET_DRAW, 0);
3422 #endif
3424 /* --------------------------------------------------------------------------------------------- */
3426 void
3427 edit_insert_literal_cmd (WEdit * edit)
3429 int char_for_insertion;
3431 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3432 _("Press any key:"), FALSE);
3433 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3436 /* --------------------------------------------------------------------------------------------- */
3438 void
3439 edit_begin_end_macro_cmd (WEdit * edit)
3441 /* edit is a pointer to the widget */
3442 if (edit != NULL)
3444 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3445 edit_execute_key_command (edit, command, -1);
3449 /* --------------------------------------------------------------------------------------------- */
3451 void
3452 edit_begin_end_repeat_cmd (WEdit * edit)
3454 /* edit is a pointer to the widget */
3455 if (edit != NULL)
3457 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3458 edit_execute_key_command (edit, command, -1);
3462 /* --------------------------------------------------------------------------------------------- */
3464 gboolean
3465 edit_load_forward_cmd (WEdit * edit)
3467 if (edit->modified
3468 && edit_query_dialog2 (_("Warning"),
3469 _("Current text was modified without a file save.\n"
3470 "Continue discards these changes"), _("C&ontinue"),
3471 _("&Cancel")) == 1)
3473 edit->force |= REDRAW_COMPLETELY;
3474 return TRUE;
3477 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3478 return FALSE;
3480 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3481 return FALSE;
3483 edit_stack_iterator++;
3484 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3485 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3486 edit_history_moveto[edit_stack_iterator].line);
3488 return FALSE;
3491 /* --------------------------------------------------------------------------------------------- */
3493 gboolean
3494 edit_load_back_cmd (WEdit * edit)
3496 if (edit->modified
3497 && edit_query_dialog2 (_("Warning"),
3498 _("Current text was modified without a file save.\n"
3499 "Continue discards these changes"), _("C&ontinue"),
3500 _("&Cancel")) == 1)
3502 edit->force |= REDRAW_COMPLETELY;
3503 return TRUE;
3506 /* we are in the bottom of the stack, NO WAY! */
3507 if (edit_stack_iterator == 0)
3508 return FALSE;
3510 edit_stack_iterator--;
3511 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3512 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3513 edit_history_moveto[edit_stack_iterator].line);
3515 return FALSE;
3518 /* --------------------------------------------------------------------------------------------- */
3520 void
3521 edit_get_match_keyword_cmd (WEdit * edit)
3523 gsize word_len = 0, max_len = 0;
3524 int num_def = 0;
3525 int i;
3526 off_t word_start = 0;
3527 unsigned char *bufpos;
3528 char *match_expr;
3529 char *path = NULL;
3530 char *ptr = NULL;
3531 char *tagfile = NULL;
3533 etags_hash_t def_hash[MAX_DEFINITIONS];
3535 for (i = 0; i < MAX_DEFINITIONS; i++)
3537 def_hash[i].filename = NULL;
3540 /* search start of word to be completed */
3541 if (!edit_find_word_start (edit, &word_start, &word_len))
3542 return;
3544 /* prepare match expression */
3545 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3546 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3548 ptr = g_get_current_dir ();
3549 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3550 g_free (ptr);
3552 /* Recursive search file 'TAGS' in parent dirs */
3555 ptr = g_path_get_dirname (path);
3556 g_free (path);
3557 path = ptr;
3558 g_free (tagfile);
3559 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3560 if (exist_file (tagfile))
3561 break;
3563 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3565 if (tagfile)
3567 num_def =
3568 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3569 g_free (tagfile);
3571 g_free (path);
3573 max_len = MAX_WIDTH_DEF_DIALOG;
3574 word_len = 0;
3575 if (num_def > 0)
3577 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3578 (etags_hash_t *) & def_hash, num_def);
3580 g_free (match_expr);
3583 /* --------------------------------------------------------------------------------------------- */
3585 #ifdef HAVE_ASPELL
3587 edit_suggest_current_word (WEdit * edit)
3589 gsize cut_len = 0;
3590 gsize word_len = 0;
3591 off_t word_start = 0;
3592 int retval = B_SKIP_WORD;
3593 char *match_word;
3595 /* search start of word to spell check */
3596 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3598 #ifdef HAVE_CHARSET
3599 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3601 GString *tmp_word;
3603 tmp_word = str_convert_to_display (match_word);
3604 g_free (match_word);
3605 match_word = g_string_free (tmp_word, FALSE);
3607 #endif
3608 if (!aspell_check (match_word, (int) word_len))
3610 GArray *suggest;
3611 unsigned int res;
3613 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3615 res = aspell_suggest (suggest, match_word, (int) word_len);
3616 if (res != 0)
3618 char *new_word = NULL;
3620 edit->found_start = word_start;
3621 edit->found_len = word_len;
3622 edit->force |= REDRAW_PAGE;
3623 edit_scroll_screen_over_cursor (edit);
3624 edit_render_keypress (edit);
3626 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3627 edit_cursor_move (edit, word_len - cut_len);
3629 if (retval == B_ENTER && new_word != NULL)
3631 guint i;
3632 char *cp_word;
3634 #ifdef HAVE_CHARSET
3635 if (mc_global.source_codepage >= 0 &&
3636 (mc_global.source_codepage != mc_global.display_codepage))
3638 GString *tmp_word;
3640 tmp_word = str_convert_to_input (new_word);
3641 g_free (new_word);
3642 new_word = g_string_free (tmp_word, FALSE);
3644 #endif
3645 cp_word = new_word;
3646 for (i = 0; i < word_len; i++)
3647 edit_backspace (edit, 1);
3648 for (; *new_word; new_word++)
3649 edit_insert (edit, *new_word);
3650 g_free (cp_word);
3652 else if (retval == B_ADD_WORD && match_word != NULL)
3653 aspell_add_to_dict (match_word, (int) word_len);
3656 g_array_free (suggest, TRUE);
3657 edit->found_start = 0;
3658 edit->found_len = 0;
3660 g_free (match_word);
3661 return retval;
3664 /* --------------------------------------------------------------------------------------------- */
3666 void
3667 edit_spellcheck_file (WEdit * edit)
3669 if (edit->curs_line > 0)
3671 edit_cursor_move (edit, -edit->curs1);
3672 edit_move_to_prev_col (edit, 0);
3673 edit_update_curs_row (edit);
3678 int c1, c2;
3680 c2 = edit_get_byte (edit, edit->curs1);
3684 if (edit->curs1 >= edit->last_byte)
3685 return;
3687 c1 = c2;
3688 edit_cursor_move (edit, 1);
3689 c2 = edit_get_byte (edit, edit->curs1);
3691 while (is_break_char (c1) || is_break_char (c2));
3693 while (edit_suggest_current_word (edit) != B_CANCEL);
3696 /* --------------------------------------------------------------------------------------------- */
3698 void
3699 edit_set_spell_lang (void)
3701 GArray *lang_list;
3703 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3704 if (aspell_get_lang_list (lang_list) != 0)
3706 char *lang;
3708 lang = spell_dialog_lang_list_show (lang_list);
3709 if (lang != NULL)
3710 (void) aspell_set_lang (lang);
3712 aspell_array_clean (lang_list);
3714 #endif /* HAVE_ASPELL */
3716 /* --------------------------------------------------------------------------------------------- */