Use g_strndup() instead of g_strdup()
[midnight-commander.git] / src / editor / editcmd.c
blobcb6611a71f31c64739fd0c754fe19051b4f5d422
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 (mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len))
951 if (found == 0)
952 edit->search_start = edit->search->normal_offset;
953 found++;
954 l += edit_count_lines (edit, q, edit->search->normal_offset);
955 if (l != l_last)
957 book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR);
958 books++;
960 l_last = l;
961 q = edit->search->normal_offset + 1;
964 if (found == 0)
965 edit_error_dialog (_("Search"), _("Search string not found"));
966 else
967 edit_cursor_move (edit, edit->search_start - edit->curs1);
969 else
971 if (edit->found_len != 0 && edit->search_start == edit->found_start + 1
972 && edit_search_options.backwards)
973 edit->search_start--;
975 if (edit->found_len != 0 && edit->search_start == edit->found_start - 1
976 && !edit_search_options.backwards)
977 edit->search_start++;
979 if (editcmd_find (edit, &len))
981 edit->found_start = edit->search_start = edit->search->normal_offset;
982 edit->found_len = len;
983 edit->over_col = 0;
984 edit_cursor_move (edit, edit->search_start - edit->curs1);
985 edit_scroll_screen_over_cursor (edit);
986 if (edit_search_options.backwards)
987 edit->search_start--;
988 else
989 edit->search_start++;
991 else
993 edit->search_start = edit->curs1;
994 if (edit->search->error_str != NULL)
995 edit_error_dialog (_("Search"), edit->search->error_str);
999 edit->force |= REDRAW_COMPLETELY;
1000 edit_scroll_screen_over_cursor (edit);
1003 /* --------------------------------------------------------------------------------------------- */
1005 static void
1006 edit_search (WEdit * edit)
1008 if (editcmd_dialog_search_show (edit))
1010 edit->search_line_type = edit_get_search_line_type (edit->search);
1011 edit_search_fix_search_start_if_selection (edit);
1012 edit_do_search (edit);
1016 /* --------------------------------------------------------------------------------------------- */
1017 /** Return a null terminated length of text. Result must be g_free'd */
1019 static unsigned char *
1020 edit_get_block (WEdit * edit, off_t start, off_t finish, int *l)
1022 unsigned char *s, *r;
1023 r = s = g_malloc0 (finish - start + 1);
1024 if (edit->column_highlight)
1026 *l = 0;
1027 /* copy from buffer, excluding chars that are out of the column 'margins' */
1028 while (start < finish)
1030 int c;
1031 off_t x;
1033 x = edit_move_forward3 (edit, edit_bol (edit, start), 0, start);
1034 c = edit_get_byte (edit, start);
1035 if ((x >= edit->column1 && x < edit->column2)
1036 || (x >= edit->column2 && x < edit->column1) || c == '\n')
1038 *s++ = c;
1039 (*l)++;
1041 start++;
1044 else
1046 *l = finish - start;
1047 while (start < finish)
1048 *s++ = edit_get_byte (edit, start++);
1050 *s = 0;
1051 return r;
1054 /* --------------------------------------------------------------------------------------------- */
1055 /** copies a block to clipboard file */
1057 static gboolean
1058 edit_save_block_to_clip_file (WEdit * edit, off_t start, off_t finish)
1060 gboolean ret;
1061 gchar *tmp;
1063 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
1064 ret = edit_save_block (edit, tmp, start, finish);
1065 g_free (tmp);
1066 return ret;
1069 /* --------------------------------------------------------------------------------------------- */
1071 static void
1072 pipe_mail (WEdit * edit, char *to, char *subject, char *cc)
1074 FILE *p = 0;
1075 char *s;
1077 to = name_quote (to, 0);
1078 subject = name_quote (subject, 0);
1079 cc = name_quote (cc, 0);
1080 s = g_strconcat ("mail -s ", subject, *cc ? " -c " : "", cc, " ", to, (char *) NULL);
1081 g_free (to);
1082 g_free (subject);
1083 g_free (cc);
1085 if (s)
1087 p = popen (s, "w");
1088 g_free (s);
1091 if (p)
1093 off_t i;
1094 for (i = 0; i < edit->last_byte; i++)
1095 fputc (edit_get_byte (edit, i), p);
1096 pclose (p);
1100 /* --------------------------------------------------------------------------------------------- */
1101 /** find first character of current word */
1103 static gboolean
1104 edit_find_word_start (WEdit * edit, off_t * word_start, gsize * word_len)
1106 int c, last;
1107 gsize i;
1109 /* return if at begin of file */
1110 if (edit->curs1 <= 0)
1111 return FALSE;
1113 c = edit_get_byte (edit, edit->curs1 - 1);
1114 /* return if not at end or in word */
1115 if (is_break_char (c))
1116 return FALSE;
1118 /* search start of word to be completed */
1119 for (i = 2;; i++)
1121 /* return if at begin of file */
1122 if ((gsize) edit->curs1 < i)
1123 return FALSE;
1125 last = c;
1126 c = edit_get_byte (edit, edit->curs1 - i);
1128 if (is_break_char (c))
1130 /* return if word starts with digit */
1131 if (isdigit (last))
1132 return FALSE;
1134 *word_start = edit->curs1 - (i - 1); /* start found */
1135 *word_len = i - 1;
1136 break;
1139 /* success */
1140 return TRUE;
1143 /* --------------------------------------------------------------------------------------------- */
1145 * Get current word under cursor
1147 * @param edit editor object
1148 * @param srch mc_search object
1149 * @param word_start start word position
1151 * @return newly allocated string or NULL if no any words under cursor
1154 static char *
1155 edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off_t word_start)
1157 gsize len = 0;
1158 off_t i;
1159 GString *temp;
1161 if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len))
1162 return NULL;
1164 temp = g_string_sized_new (len);
1166 for (i = 0; i < (off_t) len; i++)
1168 int chr;
1170 chr = edit_get_byte (edit, word_start + i);
1171 if (!isspace (chr))
1172 g_string_append_c (temp, chr);
1175 return g_string_free (temp, temp->len == 0);
1178 /* --------------------------------------------------------------------------------------------- */
1179 /** collect the possible completions */
1181 static gsize
1182 edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len,
1183 char *match_expr, struct selection *compl, gsize * num)
1185 gsize len = 0;
1186 gsize max_len = 0;
1187 gsize i;
1188 int skip;
1189 GString *temp;
1190 mc_search_t *srch;
1191 off_t last_byte, start = -1;
1192 char *current_word;
1194 srch = mc_search_new (match_expr, -1);
1195 if (srch == NULL)
1196 return 0;
1198 if (mc_config_get_bool
1199 (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0))
1201 last_byte = edit->last_byte;
1203 else
1205 last_byte = word_start;
1208 srch->search_type = MC_SEARCH_T_REGEX;
1209 srch->is_case_sensitive = TRUE;
1210 srch->search_fn = edit_search_cmd_callback;
1212 current_word = edit_collect_completions_get_current_word (edit, srch, word_start);
1214 temp = g_string_new ("");
1216 /* collect max MAX_WORD_COMPLETIONS completions */
1217 while (mc_search_run (srch, (void *) edit, start + 1, last_byte, &len))
1219 g_string_set_size (temp, 0);
1220 start = srch->normal_offset;
1222 /* add matched completion if not yet added */
1223 for (i = 0; i < len; i++)
1225 skip = edit_get_byte (edit, start + i);
1226 if (isspace (skip))
1227 continue;
1229 /* skip current word */
1230 if (start + (off_t) i == word_start)
1231 break;
1233 g_string_append_c (temp, skip);
1236 if (temp->len == 0)
1237 continue;
1239 if (current_word != NULL && strcmp (current_word, temp->str) == 0)
1240 continue;
1242 skip = 0;
1244 for (i = 0; i < *num; i++)
1246 if (strncmp
1247 ((char *) &compl[i].text[word_len],
1248 (char *) &temp->str[word_len], max (len, compl[i].len) - word_len) == 0)
1250 struct selection this = compl[i];
1251 for (++i; i < *num; i++)
1253 compl[i - 1] = compl[i];
1255 compl[*num - 1] = this;
1256 skip = 1;
1257 break; /* skip it, already added */
1260 if (skip != 0)
1261 continue;
1263 if (*num == MAX_WORD_COMPLETIONS)
1265 g_free (compl[0].text);
1266 for (i = 1; i < *num; i++)
1268 compl[i - 1] = compl[i];
1270 (*num)--;
1272 #ifdef HAVE_CHARSET
1274 GString *recoded;
1275 recoded = str_convert_to_display (temp->str);
1277 if (recoded && recoded->len)
1278 g_string_assign (temp, recoded->str);
1280 g_string_free (recoded, TRUE);
1282 #endif
1283 compl[*num].text = g_strndup (temp->str, temp->len);
1284 compl[*num].len = temp->len;
1285 (*num)++;
1286 start += len;
1288 /* note the maximal length needed for the completion dialog */
1289 if (len > max_len)
1290 max_len = len;
1293 mc_search_free (srch);
1294 g_string_free (temp, TRUE);
1295 g_free (current_word);
1297 return max_len;
1300 /* --------------------------------------------------------------------------------------------- */
1302 static void
1303 edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width,
1304 off_t * start_pos, off_t * end_pos, int *col1, int *col2)
1306 off_t cursor;
1307 long i, col;
1309 cursor = edit->curs1;
1310 col = edit_get_col (edit);
1312 for (i = 0; i < size; i++)
1314 if (data[i] != '\n')
1315 edit_insert (edit, data[i]);
1316 else
1317 { /* fill in and move to next line */
1318 int l;
1319 off_t p;
1321 if (edit_get_byte (edit, edit->curs1) != '\n')
1323 l = width - (edit_get_col (edit) - col);
1324 while (l > 0)
1326 edit_insert (edit, ' ');
1327 l -= space_width;
1330 for (p = edit->curs1;; p++)
1332 if (p == edit->last_byte)
1334 edit_cursor_move (edit, edit->last_byte - edit->curs1);
1335 edit_insert_ahead (edit, '\n');
1336 p++;
1337 break;
1339 if (edit_get_byte (edit, p) == '\n')
1341 p++;
1342 break;
1345 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
1346 l = col - edit_get_col (edit);
1347 while (l >= space_width)
1349 edit_insert (edit, ' ');
1350 l -= space_width;
1355 *col1 = col;
1356 *col2 = col + width;
1357 *start_pos = cursor;
1358 *end_pos = edit->curs1;
1359 edit_cursor_move (edit, cursor - edit->curs1);
1362 /* --------------------------------------------------------------------------------------------- */
1364 static int
1365 edit_macro_comparator (gconstpointer * macro1, gconstpointer * macro2)
1367 const macros_t *m1 = (const macros_t *) macro1;
1368 const macros_t *m2 = (const macros_t *) macro2;
1370 return m1->hotkey - m2->hotkey;
1373 /* --------------------------------------------------------------------------------------------- */
1375 static void
1376 edit_macro_sort_by_hotkey (void)
1378 if (macros_list != NULL && macros_list->len != 0)
1379 g_array_sort (macros_list, (GCompareFunc) edit_macro_comparator);
1382 /* --------------------------------------------------------------------------------------------- */
1384 static gboolean
1385 edit_get_macro (WEdit * edit, int hotkey, const macros_t ** macros, guint * indx)
1387 const macros_t *array_start = &g_array_index (macros_list, struct macros_t, 0);
1388 macros_t *result;
1389 macros_t search_macro;
1391 (void) edit;
1393 search_macro.hotkey = hotkey;
1394 result = bsearch (&search_macro, macros_list->data, macros_list->len,
1395 sizeof (macros_t), (GCompareFunc) edit_macro_comparator);
1397 if (result != NULL && result->macro != NULL)
1399 *indx = (result - array_start);
1400 *macros = result;
1401 return TRUE;
1403 *indx = 0;
1404 return FALSE;
1407 /* --------------------------------------------------------------------------------------------- */
1408 /** returns FALSE on error */
1410 static gboolean
1411 edit_delete_macro (WEdit * edit, int hotkey)
1413 mc_config_t *macros_config = NULL;
1414 const char *section_name = "editor";
1415 gchar *macros_fname;
1416 guint indx;
1417 char *skeyname;
1418 const macros_t *macros = NULL;
1420 /* clear array of actions for current hotkey */
1421 while (edit_get_macro (edit, hotkey, &macros, &indx))
1423 if (macros->macro != NULL)
1424 g_array_free (macros->macro, TRUE);
1425 macros = NULL;
1426 g_array_remove_index (macros_list, indx);
1427 edit_macro_sort_by_hotkey ();
1430 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1431 macros_config = mc_config_init (macros_fname, FALSE);
1432 g_free (macros_fname);
1434 if (macros_config == NULL)
1435 return FALSE;
1437 skeyname = lookup_key_by_code (hotkey);
1438 while (mc_config_del_key (macros_config, section_name, skeyname))
1440 g_free (skeyname);
1441 mc_config_save_file (macros_config, NULL);
1442 mc_config_deinit (macros_config);
1443 return TRUE;
1446 /* --------------------------------------------------------------------------------------------- */
1448 * Callback for the iteration of objects in the 'editors' array.
1449 * Toggle syntax highlighting in editor object.
1451 * @param data probably WEdit object
1452 * @param user_data unused
1455 static void
1456 edit_syntax_onoff_cb (void *data, void *user_data)
1458 (void) user_data;
1460 if (edit_widget_is_editor ((const Widget *) data))
1462 WEdit *edit = (WEdit *) data;
1464 if (option_syntax_highlighting)
1465 edit_load_syntax (edit, NULL, edit->syntax_type);
1466 edit->force |= REDRAW_PAGE;
1470 /* --------------------------------------------------------------------------------------------- */
1472 * Callback for the iteration of objects in the 'editors' array.
1473 * Redraw editor object.
1475 * @param data probably WEdit object
1476 * @param user_data unused
1479 static void
1480 edit_redraw_page_cb (void *data, void *user_data)
1482 (void) user_data;
1484 if (edit_widget_is_editor ((const Widget *) data))
1485 ((WEdit *) data)->force |= REDRAW_PAGE;
1488 /* --------------------------------------------------------------------------------------------- */
1489 /*** public functions ****************************************************************************/
1490 /* --------------------------------------------------------------------------------------------- */
1492 void
1493 edit_refresh_cmd (void)
1495 clr_scr ();
1496 repaint_screen ();
1497 tty_keypad (TRUE);
1500 /* --------------------------------------------------------------------------------------------- */
1502 * Toggle syntax highlighting in all editor windows.
1504 * @param h root widget for all windows
1507 void
1508 edit_syntax_onoff_cmd (Dlg_head * h)
1510 option_syntax_highlighting = !option_syntax_highlighting;
1511 g_list_foreach (h->widgets, edit_syntax_onoff_cb, NULL);
1512 dlg_redraw (h);
1515 /* --------------------------------------------------------------------------------------------- */
1517 * Toggle tabs showing in all editor windows.
1519 * @param h root widget for all windows
1522 void
1523 edit_show_tabs_tws_cmd (Dlg_head * h)
1525 enable_show_tabs_tws = !enable_show_tabs_tws;
1526 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1527 dlg_redraw (h);
1530 /* --------------------------------------------------------------------------------------------- */
1532 * Toggle right margin showing in all editor windows.
1534 * @param h root widget for all windows
1537 void
1538 edit_show_margin_cmd (Dlg_head * h)
1540 show_right_margin = !show_right_margin;
1541 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1542 dlg_redraw (h);
1545 /* --------------------------------------------------------------------------------------------- */
1547 * Toggle line numbers showing in all editor windows.
1549 * @param h root widget for all windows
1552 void
1553 edit_show_numbers_cmd (Dlg_head * h)
1555 option_line_state = !option_line_state;
1556 option_line_state_width = option_line_state ? LINE_STATE_WIDTH : 0;
1557 g_list_foreach (h->widgets, edit_redraw_page_cb, NULL);
1558 dlg_redraw (h);
1561 /* --------------------------------------------------------------------------------------------- */
1563 void
1564 edit_save_mode_cmd (void)
1566 /* diaog sizes */
1567 const int DLG_X = 38;
1568 const int DLG_Y = 13;
1570 char *str_result;
1572 const char *str[] = {
1573 N_("&Quick save"),
1574 N_("&Safe save"),
1575 N_("&Do backups with following extension:")
1578 QuickWidget widgets[] = {
1579 /* 0 */
1580 QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
1581 /* 1 */
1582 QUICK_BUTTON (6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
1583 /* 2 */
1584 QUICK_CHECKBOX (4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
1585 /* 3 */
1586 QUICK_INPUT (8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
1587 /* 4 */
1588 QUICK_RADIO (4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
1589 QUICK_END
1592 QuickDialog dialog = {
1593 DLG_X, DLG_Y, -1, -1, N_("Edit Save Mode"),
1594 "[Edit Save Mode]", widgets, NULL, NULL, FALSE
1597 size_t i;
1598 size_t maxlen = 0;
1599 size_t w0, w1, b_len, w3;
1601 #ifdef HAVE_ASSERT_H
1602 assert (option_backup_ext != NULL);
1603 #endif
1605 /* OK/Cancel buttons */
1606 w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
1607 w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
1608 b_len = w0 + w1 + 3;
1610 maxlen = max (b_len, (size_t) str_term_width1 (_(dialog.title)) + 2);
1612 w3 = 0;
1613 for (i = 0; i < 3; i++)
1615 #ifdef ENABLE_NLS
1616 str[i] = _(str[i]);
1617 #endif
1618 w3 = max (w3, (size_t) str_term_width1 (str[i]));
1621 maxlen = max (maxlen, w3 + 4);
1623 dialog.xlen = min ((size_t) COLS, maxlen + 8);
1625 widgets[3].u.input.len = w3;
1626 widgets[1].relative_x = (dialog.xlen - b_len) / 2;
1627 widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
1629 for (i = 0; i < sizeof (widgets) / sizeof (widgets[0]); i++)
1630 widgets[i].x_divisions = dialog.xlen;
1632 if (quick_dialog (&dialog) != B_CANCEL)
1634 g_free (option_backup_ext);
1635 option_backup_ext = str_result;
1639 /* --------------------------------------------------------------------------------------------- */
1641 void
1642 edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath)
1644 vfs_path_free (edit->filename_vpath);
1645 edit->filename_vpath = vfs_path_clone (name_vpath);
1647 if (edit->dir_vpath == NULL)
1648 edit->dir_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
1651 /* --------------------------------------------------------------------------------------------- */
1652 /* Here we want to warn the users of overwriting an existing file,
1653 but only if they have made a change to the filename */
1654 /* returns TRUE on success */
1655 gboolean
1656 edit_save_as_cmd (WEdit * edit)
1658 /* This heads the 'Save As' dialog box */
1659 vfs_path_t *exp_vpath;
1660 int save_lock = 0;
1661 int different_filename = 0;
1663 if (!edit_check_newline (edit))
1664 return FALSE;
1666 exp_vpath = edit_get_save_file_as (edit);
1667 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1669 if (exp_vpath != NULL)
1671 if (vfs_path_len (exp_vpath) == 0)
1672 goto ret;
1673 else
1675 int rv;
1677 if (vfs_path_cmp (edit->filename_vpath, exp_vpath) != 0)
1679 int file;
1680 struct stat sb;
1682 if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
1684 edit_error_dialog (_("Save as"),
1685 get_sys_error (_
1686 ("Cannot save: destination is not a regular file")));
1687 goto ret;
1690 different_filename = 1;
1691 file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
1693 if (file != -1)
1695 /* the file exists */
1696 mc_close (file);
1697 /* Overwrite the current file or cancel the operation */
1698 if (edit_query_dialog2
1699 (_("Warning"),
1700 _("A file already exists with this name"), _("&Overwrite"), _("&Cancel")))
1701 goto ret;
1703 else
1705 edit->stat1.st_mode |= S_IWUSR;
1707 save_lock = lock_file (exp_vpath);
1709 else
1711 /* filenames equal, check if already locked */
1712 if (!edit->locked && !edit->delete_file)
1713 save_lock = lock_file (exp_vpath);
1716 if (different_filename)
1719 * Allow user to write into saved (under another name) file
1720 * even if original file had r/o user permissions.
1722 edit->stat1.st_mode |= S_IWRITE;
1725 rv = edit_save_file (edit, exp_vpath);
1726 switch (rv)
1728 case 1:
1729 /* Succesful, so unlock both files */
1730 if (different_filename)
1732 if (save_lock)
1733 unlock_file (exp_vpath);
1734 if (edit->locked)
1735 edit->locked = unlock_file (edit->filename_vpath);
1737 else
1739 if (edit->locked || save_lock)
1740 edit->locked = unlock_file (edit->filename_vpath);
1743 edit_set_filename (edit, exp_vpath);
1744 if (edit->lb != LB_ASIS)
1745 edit_reload (edit, exp_vpath);
1746 edit->modified = 0;
1747 edit->delete_file = 0;
1748 if (different_filename)
1749 edit_load_syntax (edit, NULL, edit->syntax_type);
1750 vfs_path_free (exp_vpath);
1751 edit->force |= REDRAW_COMPLETELY;
1752 return TRUE;
1753 default:
1754 edit_error_dialog (_("Save as"), get_sys_error (_("Cannot save file")));
1755 /* fallthrough */
1756 case -1:
1757 /* Failed, so maintain modify (not save) lock */
1758 if (save_lock)
1759 unlock_file (exp_vpath);
1760 break;
1765 ret:
1766 vfs_path_free (exp_vpath);
1767 edit->force |= REDRAW_COMPLETELY;
1768 return FALSE;
1771 /* {{{ Macro stuff starts here */
1772 /* --------------------------------------------------------------------------------------------- */
1774 void
1775 edit_delete_macro_cmd (WEdit * edit)
1777 int hotkey;
1779 hotkey = editcmd_dialog_raw_key_query (_("Delete macro"), _("Press macro hotkey:"), TRUE);
1781 if (hotkey != 0 && !edit_delete_macro (edit, hotkey))
1782 message (D_ERROR, _("Delete macro"), _("Macro not deleted"));
1785 /* --------------------------------------------------------------------------------------------- */
1787 /** returns FALSE on error */
1788 gboolean
1789 edit_execute_macro (WEdit * edit, int hotkey)
1791 gboolean res = FALSE;
1793 if (hotkey != 0)
1795 const macros_t *macros;
1796 guint indx;
1798 if (edit_get_macro (edit, hotkey, &macros, &indx) &&
1799 macros->macro != NULL && macros->macro->len != 0)
1801 guint i;
1803 edit->force |= REDRAW_PAGE;
1805 for (i = 0; i < macros->macro->len; i++)
1807 const macro_action_t *m_act;
1809 m_act = &g_array_index (macros->macro, struct macro_action_t, i);
1810 edit_execute_cmd (edit, m_act->action, m_act->ch);
1811 res = TRUE;
1816 return res;
1819 /* --------------------------------------------------------------------------------------------- */
1821 /** returns FALSE on error */
1822 gboolean
1823 edit_store_macro_cmd (WEdit * edit)
1825 int i;
1826 int hotkey;
1827 GString *marcros_string;
1828 mc_config_t *macros_config = NULL;
1829 const char *section_name = "editor";
1830 gchar *macros_fname;
1831 GArray *macros; /* current macro */
1832 int tmp_act;
1833 gboolean have_macro = FALSE;
1834 char *skeyname = NULL;
1836 hotkey =
1837 editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), TRUE);
1838 if (hotkey == ESC_CHAR)
1839 return FALSE;
1841 tmp_act = keybind_lookup_keymap_command (editor_map, hotkey);
1843 /* return FALSE if try assign macro into restricted hotkeys */
1844 if (tmp_act == CK_MacroStartRecord
1845 || tmp_act == CK_MacroStopRecord || tmp_act == CK_MacroStartStopRecord)
1846 return FALSE;
1848 edit_delete_macro (edit, hotkey);
1850 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1851 macros_config = mc_config_init (macros_fname, FALSE);
1852 g_free (macros_fname);
1854 if (macros_config == NULL)
1855 return FALSE;
1857 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1859 marcros_string = g_string_sized_new (250);
1860 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1862 skeyname = lookup_key_by_code (hotkey);
1864 for (i = 0; i < macro_index; i++)
1866 macro_action_t m_act;
1867 const char *action_name;
1869 action_name = keybind_lookup_actionname (record_macro_buf[i].action);
1871 if (action_name == NULL)
1872 break;
1874 m_act.action = record_macro_buf[i].action;
1875 m_act.ch = record_macro_buf[i].ch;
1876 g_array_append_val (macros, m_act);
1877 have_macro = TRUE;
1878 g_string_append_printf (marcros_string, "%s:%i;", action_name,
1879 (int) record_macro_buf[i].ch);
1881 if (have_macro)
1883 macros_t macro;
1884 macro.hotkey = hotkey;
1885 macro.macro = macros;
1886 g_array_append_val (macros_list, macro);
1887 mc_config_set_string (macros_config, section_name, skeyname, marcros_string->str);
1889 else
1890 mc_config_del_key (macros_config, section_name, skeyname);
1892 g_free (skeyname);
1893 edit_macro_sort_by_hotkey ();
1895 g_string_free (marcros_string, TRUE);
1896 mc_config_save_file (macros_config, NULL);
1897 mc_config_deinit (macros_config);
1898 return TRUE;
1901 /* --------------------------------------------------------------------------------------------- */
1903 gboolean
1904 edit_repeat_macro_cmd (WEdit * edit)
1906 int i, j;
1907 char *f;
1908 long count_repeat;
1909 char *error = NULL;
1911 f = input_dialog (_("Repeat last commands"), _("Repeat times:"), MC_HISTORY_EDIT_REPEAT, NULL);
1912 if (f == NULL || *f == '\0')
1914 g_free (f);
1915 return FALSE;
1918 count_repeat = strtol (f, &error, 0);
1920 if (*error != '\0')
1922 g_free (f);
1923 return FALSE;
1926 g_free (f);
1928 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
1929 edit->force |= REDRAW_PAGE;
1931 for (j = 0; j < count_repeat; j++)
1932 for (i = 0; i < macro_index; i++)
1933 edit_execute_cmd (edit, record_macro_buf[i].action, record_macro_buf[i].ch);
1934 edit_update_screen (edit);
1935 return TRUE;
1938 /* --------------------------------------------------------------------------------------------- */
1939 /** return FALSE on error */
1941 gboolean
1942 edit_load_macro_cmd (WEdit * edit)
1944 mc_config_t *macros_config = NULL;
1945 gchar **profile_keys, **keys;
1946 gchar **values, **curr_values;
1947 gsize len, values_len;
1948 const char *section_name = "editor";
1949 gchar *macros_fname;
1950 int hotkey;
1952 (void) edit;
1954 macros_fname = mc_config_get_full_path (MC_MACRO_FILE);
1955 macros_config = mc_config_init (macros_fname, TRUE);
1956 g_free (macros_fname);
1958 if (macros_config == NULL)
1959 return FALSE;
1961 profile_keys = keys = mc_config_get_keys (macros_config, section_name, &len);
1962 while (*profile_keys != NULL)
1964 gboolean have_macro;
1965 GArray *macros;
1966 macros_t macro;
1968 macros = g_array_new (TRUE, FALSE, sizeof (macro_action_t));
1970 curr_values = values = mc_config_get_string_list (macros_config, section_name,
1971 *profile_keys, &values_len);
1972 hotkey = lookup_key (*profile_keys, NULL);
1973 have_macro = FALSE;
1975 while (*curr_values != NULL && *curr_values[0] != '\0')
1977 char **macro_pair = NULL;
1979 macro_pair = g_strsplit (*curr_values, ":", 2);
1981 if (macro_pair != NULL)
1983 macro_action_t m_act;
1984 if (macro_pair[0] == NULL || macro_pair[0][0] == '\0')
1985 m_act.action = 0;
1986 else
1988 m_act.action = keybind_lookup_action (macro_pair[0]);
1989 g_free (macro_pair[0]);
1990 macro_pair[0] = NULL;
1992 if (macro_pair[1] == NULL || macro_pair[1][0] == '\0')
1993 m_act.ch = -1;
1994 else
1996 m_act.ch = strtol (macro_pair[1], NULL, 0);
1997 g_free (macro_pair[1]);
1998 macro_pair[1] = NULL;
2000 if (m_act.action != 0)
2002 /* a shell command */
2003 if ((m_act.action / CK_PipeBlock (0)) == 1)
2005 m_act.action = CK_PipeBlock (0) + (m_act.ch > 0 ? m_act.ch : 0);
2006 m_act.ch = -1;
2008 g_array_append_val (macros, m_act);
2009 have_macro = TRUE;
2011 g_strfreev (macro_pair);
2012 macro_pair = NULL;
2014 curr_values++;
2016 if (have_macro)
2018 macro.hotkey = hotkey;
2019 macro.macro = macros;
2020 g_array_append_val (macros_list, macro);
2022 profile_keys++;
2023 g_strfreev (values);
2025 g_strfreev (keys);
2026 mc_config_deinit (macros_config);
2027 edit_macro_sort_by_hotkey ();
2028 return TRUE;
2031 /* }}} Macro stuff end here */
2033 /* --------------------------------------------------------------------------------------------- */
2034 /** returns TRUE on success */
2036 gboolean
2037 edit_save_confirm_cmd (WEdit * edit)
2039 char *f = NULL;
2041 if (edit->filename_vpath == NULL)
2042 return edit_save_as_cmd (edit);
2044 if (!edit_check_newline (edit))
2045 return FALSE;
2047 if (edit_confirm_save)
2049 char *filename;
2050 gboolean ok;
2052 filename = vfs_path_to_str (edit->filename_vpath);
2053 f = g_strdup_printf (_("Confirm save file: \"%s\""), filename);
2054 g_free (filename);
2055 ok = (edit_query_dialog2 (_("Save file"), f, _("&Save"), _("&Cancel")) == 0);
2056 g_free (f);
2057 if (!ok)
2058 return FALSE;
2060 return edit_save_cmd (edit);
2063 /* --------------------------------------------------------------------------------------------- */
2065 * Ask file to edit and load it.
2067 * @returns TRUE on success or cancel of ask.
2070 gboolean
2071 edit_load_cmd (Dlg_head * h)
2073 char *exp;
2074 gboolean ret = TRUE; /* possible cancel */
2076 exp = input_expand_dialog (_("Load"), _("Enter file name:"),
2077 MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT);
2079 if (exp != NULL && *exp != '\0')
2081 vfs_path_t *exp_vpath;
2083 exp_vpath = vfs_path_from_str (exp);
2084 ret = edit_load_file_from_filename (h, exp_vpath);
2085 vfs_path_free (exp_vpath);
2088 g_free (exp);
2090 return ret;
2093 /* --------------------------------------------------------------------------------------------- */
2095 * Load syntax file to edit.
2097 * @returns TRUE on success
2100 gboolean
2101 edit_load_syntax_file (Dlg_head * h)
2103 vfs_path_t *extdir_vpath;
2104 int dir = 0;
2105 gboolean ret = FALSE;
2107 if (geteuid () == 0)
2108 dir = query_dialog (_("Syntax file edit"),
2109 _("Which syntax file you want to edit?"), D_NORMAL, 2,
2110 _("&User"), _("&System wide"));
2112 extdir_vpath =
2113 vfs_path_build_filename (mc_global.sysconfig_dir, "syntax", "Syntax", (char *) NULL);
2114 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
2116 vfs_path_free (extdir_vpath);
2117 extdir_vpath =
2118 vfs_path_build_filename (mc_global.share_data_dir, "syntax", "Syntax", (char *) NULL);
2121 if (dir == 0)
2123 vfs_path_t *user_syntax_file_vpath;
2125 user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
2126 check_for_default (extdir_vpath, user_syntax_file_vpath);
2127 ret = edit_load_file_from_filename (h, user_syntax_file_vpath);
2128 vfs_path_free (user_syntax_file_vpath);
2130 else if (dir == 1)
2131 ret = edit_load_file_from_filename (h, extdir_vpath);
2133 vfs_path_free (extdir_vpath);
2135 return ret;
2138 /* --------------------------------------------------------------------------------------------- */
2140 * Load menu file to edit.
2142 * @returns TRUE on success
2145 gboolean
2146 edit_load_menu_file (Dlg_head * h)
2148 vfs_path_t *buffer_vpath;
2149 vfs_path_t *menufile_vpath;
2150 int dir;
2151 gboolean ret;
2153 dir = query_dialog (_("Menu edit"),
2154 _("Which menu file do you want to edit?"), D_NORMAL,
2155 geteuid () != 0 ? 2 : 3, _("&Local"), _("&User"), _("&System wide"));
2157 menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2158 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
2160 vfs_path_free (menufile_vpath);
2161 menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2164 switch (dir)
2166 case 0:
2167 buffer_vpath = vfs_path_from_str (EDIT_LOCAL_MENU);
2168 check_for_default (menufile_vpath, buffer_vpath);
2169 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
2170 break;
2172 case 1:
2173 buffer_vpath = mc_config_get_full_vpath (EDIT_HOME_MENU);
2174 check_for_default (menufile_vpath, buffer_vpath);
2175 break;
2177 case 2:
2178 buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, EDIT_GLOBAL_MENU, NULL);
2179 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
2181 vfs_path_free (buffer_vpath);
2182 buffer_vpath =
2183 vfs_path_build_filename (mc_global.share_data_dir, EDIT_GLOBAL_MENU, NULL);
2185 break;
2187 default:
2188 vfs_path_free (menufile_vpath);
2189 return FALSE;
2192 ret = edit_load_file_from_filename (h, buffer_vpath);
2194 vfs_path_free (buffer_vpath);
2195 vfs_path_free (menufile_vpath);
2197 return ret;
2200 /* --------------------------------------------------------------------------------------------- */
2202 * Close window with opened file.
2204 * @returns TRUE if file was closed.
2207 gboolean
2208 edit_close_cmd (WEdit * edit)
2210 gboolean ret;
2212 ret = (edit != NULL) && edit_ok_to_exit (edit);
2214 if (ret)
2216 Dlg_head *h = ((Widget *) edit)->owner;
2218 if (edit->locked != 0)
2219 unlock_file (edit->filename_vpath);
2221 del_widget (edit);
2223 if (edit_widget_is_editor ((Widget *) h->current->data))
2224 edit = (WEdit *) h->current->data;
2225 else
2227 edit = find_editor (h);
2228 if (edit != NULL)
2229 dlg_set_top_widget (edit);
2233 if (edit != NULL)
2234 edit->force |= REDRAW_COMPLETELY;
2236 return ret;
2239 /* --------------------------------------------------------------------------------------------- */
2241 if mark2 is -1 then marking is from mark1 to the cursor.
2242 Otherwise its between the markers. This handles this.
2243 Returns 1 if no text is marked.
2247 eval_marks (WEdit * edit, off_t * start_mark, off_t * end_mark)
2249 if (edit->mark1 != edit->mark2)
2251 off_t start_bol, start_eol;
2252 off_t end_bol, end_eol;
2253 off_t diff1, diff2;
2254 long col1, col2;
2255 long end_mark_curs;
2257 if (edit->end_mark_curs < 0)
2258 end_mark_curs = edit->curs1;
2259 else
2260 end_mark_curs = edit->end_mark_curs;
2262 if (edit->mark2 >= 0)
2264 *start_mark = min (edit->mark1, edit->mark2);
2265 *end_mark = max (edit->mark1, edit->mark2);
2267 else
2269 *start_mark = min (edit->mark1, end_mark_curs);
2270 *end_mark = max (edit->mark1, end_mark_curs);
2271 edit->column2 = edit->curs_col + edit->over_col;
2274 if (edit->column_highlight
2275 && (((edit->mark1 > end_mark_curs) && (edit->column1 < edit->column2))
2276 || ((edit->mark1 < end_mark_curs) && (edit->column1 > edit->column2))))
2278 start_bol = edit_bol (edit, *start_mark);
2279 start_eol = edit_eol (edit, start_bol - 1) + 1;
2280 end_bol = edit_bol (edit, *end_mark);
2281 end_eol = edit_eol (edit, *end_mark);
2282 col1 = min (edit->column1, edit->column2);
2283 col2 = max (edit->column1, edit->column2);
2285 diff1 = edit_move_forward3 (edit, start_bol, col2, 0) -
2286 edit_move_forward3 (edit, start_bol, col1, 0);
2287 diff2 = edit_move_forward3 (edit, end_bol, col2, 0) -
2288 edit_move_forward3 (edit, end_bol, col1, 0);
2290 *start_mark -= diff1;
2291 *end_mark += diff2;
2292 *start_mark = max (*start_mark, start_eol);
2293 *end_mark = min (*end_mark, end_eol);
2295 return 0;
2297 else
2299 *start_mark = *end_mark = 0;
2300 edit->column2 = edit->column1 = 0;
2301 return 1;
2305 /* --------------------------------------------------------------------------------------------- */
2307 void
2308 edit_insert_over (WEdit * edit)
2310 int i;
2312 for (i = 0; i < edit->over_col; i++)
2314 edit_insert (edit, ' ');
2316 edit->over_col = 0;
2319 /* --------------------------------------------------------------------------------------------- */
2321 off_t
2322 edit_insert_column_of_text_from_file (WEdit * edit, int file,
2323 off_t * start_pos, off_t * end_pos, long *col1, long *col2)
2325 off_t cursor;
2326 int col;
2327 off_t blocklen = -1, width = 0;
2328 unsigned char *data;
2330 cursor = edit->curs1;
2331 col = edit_get_col (edit);
2332 data = g_malloc0 (TEMP_BUF_LEN);
2334 while ((blocklen = mc_read (file, (char *) data, TEMP_BUF_LEN)) > 0)
2336 off_t i;
2337 for (width = 0; width < blocklen; width++)
2339 if (data[width] == '\n')
2340 break;
2342 for (i = 0; i < blocklen; i++)
2344 if (data[i] == '\n')
2345 { /* fill in and move to next line */
2346 long l;
2347 off_t p;
2348 if (edit_get_byte (edit, edit->curs1) != '\n')
2350 l = width - (edit_get_col (edit) - col);
2351 while (l > 0)
2353 edit_insert (edit, ' ');
2354 l -= space_width;
2357 for (p = edit->curs1;; p++)
2359 if (p == edit->last_byte)
2361 edit_cursor_move (edit, edit->last_byte - edit->curs1);
2362 edit_insert_ahead (edit, '\n');
2363 p++;
2364 break;
2366 if (edit_get_byte (edit, p) == '\n')
2368 p++;
2369 break;
2372 edit_cursor_move (edit, edit_move_forward3 (edit, p, col, 0) - edit->curs1);
2373 l = col - edit_get_col (edit);
2374 while (l >= space_width)
2376 edit_insert (edit, ' ');
2377 l -= space_width;
2379 continue;
2381 edit_insert (edit, data[i]);
2384 *col1 = col;
2385 *col2 = col + width;
2386 *start_pos = cursor;
2387 *end_pos = edit->curs1;
2388 edit_cursor_move (edit, cursor - edit->curs1);
2389 g_free (data);
2391 return blocklen;
2394 /* --------------------------------------------------------------------------------------------- */
2396 void
2397 edit_block_copy_cmd (WEdit * edit)
2399 off_t start_mark, end_mark, current = edit->curs1;
2400 long col_delta = 0;
2401 off_t mark1, mark2;
2402 int c1, c2;
2403 int size;
2404 unsigned char *copy_buf;
2406 edit_update_curs_col (edit);
2407 if (eval_marks (edit, &start_mark, &end_mark))
2408 return;
2410 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2412 /* all that gets pushed are deletes hence little space is used on the stack */
2414 edit_push_markers (edit);
2416 if (edit->column_highlight)
2418 col_delta = abs (edit->column2 - edit->column1);
2419 edit_insert_column_of_text (edit, copy_buf, size, col_delta, &mark1, &mark2, &c1, &c2);
2421 else
2423 while (size--)
2424 edit_insert_ahead (edit, copy_buf[size]);
2427 g_free (copy_buf);
2428 edit_scroll_screen_over_cursor (edit);
2430 if (edit->column_highlight)
2431 edit_set_markers (edit, edit->curs1, mark2, c1, c2);
2432 else if (start_mark < current && end_mark > current)
2433 edit_set_markers (edit, start_mark, end_mark + end_mark - start_mark, 0, 0);
2435 edit->force |= REDRAW_PAGE;
2439 /* --------------------------------------------------------------------------------------------- */
2441 void
2442 edit_block_move_cmd (WEdit * edit)
2444 off_t current;
2445 unsigned char *copy_buf = NULL;
2446 off_t start_mark, end_mark;
2447 long line;
2449 if (eval_marks (edit, &start_mark, &end_mark))
2450 return;
2452 line = edit->curs_line;
2453 if (edit->mark2 < 0)
2454 edit_mark_cmd (edit, FALSE);
2455 edit_push_markers (edit);
2457 if (edit->column_highlight)
2459 off_t mark1, mark2;
2460 int size;
2461 int b_width = 0;
2462 int c1, c2;
2463 int x, x2;
2465 c1 = min (edit->column1, edit->column2);
2466 c2 = max (edit->column1, edit->column2);
2467 b_width = (c2 - c1);
2469 edit_update_curs_col (edit);
2471 x = edit->curs_col;
2472 x2 = x + edit->over_col;
2474 /* do nothing when cursor inside first line of selected area */
2475 if ((edit_eol (edit, edit->curs1) == edit_eol (edit, start_mark)) && (x2 > c1 && x2 <= c2))
2476 return;
2478 if (edit->curs1 > start_mark && edit->curs1 < edit_eol (edit, end_mark))
2480 if (x > c2)
2481 x -= b_width;
2482 else if (x > c1 && x <= c2)
2483 x = c1;
2485 /* save current selection into buffer */
2486 copy_buf = edit_get_block (edit, start_mark, end_mark, &size);
2488 /* remove current selection */
2489 edit_block_delete_cmd (edit);
2491 edit->over_col = max (0, edit->over_col - b_width);
2492 /* calculate the cursor pos after delete block */
2493 current = edit_move_forward3 (edit, edit_bol (edit, edit->curs1), x, 0);
2494 edit_cursor_move (edit, current - edit->curs1);
2495 edit_scroll_screen_over_cursor (edit);
2497 /* add TWS if need before block insertion */
2498 if (option_cursor_beyond_eol && edit->over_col > 0)
2499 edit_insert_over (edit);
2501 edit_insert_column_of_text (edit, copy_buf, size, b_width, &mark1, &mark2, &c1, &c2);
2502 edit_set_markers (edit, mark1, mark2, c1, c2);
2504 else
2506 off_t count;
2508 current = edit->curs1;
2509 copy_buf = g_malloc0 (end_mark - start_mark);
2510 edit_cursor_move (edit, start_mark - edit->curs1);
2511 edit_scroll_screen_over_cursor (edit);
2512 count = start_mark;
2513 while (count < end_mark)
2515 copy_buf[end_mark - count - 1] = edit_delete (edit, 1);
2516 count++;
2518 edit_scroll_screen_over_cursor (edit);
2519 edit_cursor_move (edit,
2520 current - edit->curs1 -
2521 (((current - edit->curs1) > 0) ? end_mark - start_mark : 0));
2522 edit_scroll_screen_over_cursor (edit);
2523 while (count-- > start_mark)
2524 edit_insert_ahead (edit, copy_buf[end_mark - count - 1]);
2525 edit_set_markers (edit, edit->curs1, edit->curs1 + end_mark - start_mark, 0, 0);
2528 edit_scroll_screen_over_cursor (edit);
2529 g_free (copy_buf);
2530 edit->force |= REDRAW_PAGE;
2533 /* --------------------------------------------------------------------------------------------- */
2534 /** returns 1 if canceelled by user */
2537 edit_block_delete_cmd (WEdit * edit)
2539 off_t start_mark, end_mark;
2540 if (eval_marks (edit, &start_mark, &end_mark))
2542 edit_delete_line (edit);
2543 return 0;
2545 return edit_block_delete (edit);
2548 /* --------------------------------------------------------------------------------------------- */
2549 /** call with edit = 0 before shutdown to close memory leaks */
2551 void
2552 edit_replace_cmd (WEdit * edit, int again)
2554 /* 1 = search string, 2 = replace with */
2555 static char *saved1 = NULL; /* saved default[123] */
2556 static char *saved2 = NULL;
2557 char *input1 = NULL; /* user input from the dialog */
2558 char *input2 = NULL;
2559 GString *input2_str = NULL;
2560 char *disp1 = NULL;
2561 char *disp2 = NULL;
2562 long times_replaced = 0;
2563 gboolean once_found = FALSE;
2565 if (!edit)
2567 g_free (saved1), saved1 = NULL;
2568 g_free (saved2), saved2 = NULL;
2569 return;
2572 edit->force |= REDRAW_COMPLETELY;
2574 if (again && !saved1 && !saved2)
2575 again = 0;
2577 if (again)
2579 input1 = g_strdup (saved1 ? saved1 : "");
2580 input2 = g_strdup (saved2 ? saved2 : "");
2582 else
2584 char *tmp_inp1, *tmp_inp2;
2586 disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
2587 disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
2589 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
2591 editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
2593 g_free (disp1);
2594 g_free (disp2);
2596 if (input1 == NULL || *input1 == '\0')
2598 edit->force = REDRAW_COMPLETELY;
2599 goto cleanup;
2602 tmp_inp1 = input1;
2603 tmp_inp2 = input2;
2604 input1 = edit_replace_cmd__conv_to_input (input1);
2605 input2 = edit_replace_cmd__conv_to_input (input2);
2606 g_free (tmp_inp1);
2607 g_free (tmp_inp2);
2609 g_free (saved1), saved1 = g_strdup (input1);
2610 g_free (saved2), saved2 = g_strdup (input2);
2612 mc_search_free (edit->search);
2613 edit->search = NULL;
2616 input2_str = g_string_new (input2);
2618 if (!edit->search)
2620 edit->search = mc_search_new (input1, -1);
2621 if (edit->search == NULL)
2623 edit->search_start = edit->curs1;
2624 goto cleanup;
2626 edit->search->search_type = edit_search_options.type;
2627 edit->search->is_all_charsets = edit_search_options.all_codepages;
2628 edit->search->is_case_sensitive = edit_search_options.case_sens;
2629 edit->search->whole_words = edit_search_options.whole_words;
2630 edit->search->search_fn = edit_search_cmd_callback;
2631 edit->search_line_type = edit_get_search_line_type (edit->search);
2632 edit_search_fix_search_start_if_selection (edit);
2635 if (edit->found_len && edit->search_start == edit->found_start + 1
2636 && edit_search_options.backwards)
2637 edit->search_start--;
2639 if (edit->found_len && edit->search_start == edit->found_start - 1
2640 && !edit_search_options.backwards)
2641 edit->search_start++;
2645 gsize len = 0;
2647 if (!editcmd_find (edit, &len))
2649 if (!(edit->search->error == MC_SEARCH_E_OK ||
2650 (once_found && edit->search->error == MC_SEARCH_E_NOTFOUND)))
2652 edit_error_dialog (_("Search"), edit->search->error_str);
2654 break;
2656 once_found = TRUE;
2658 edit->search_start = edit->search->normal_offset;
2659 /*returns negative on not found or error in pattern */
2661 if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte))
2663 gsize i;
2664 GString *repl_str;
2666 edit->found_start = edit->search_start;
2667 i = edit->found_len = len;
2669 edit_cursor_move (edit, edit->search_start - edit->curs1);
2670 edit_scroll_screen_over_cursor (edit);
2672 if (edit->replace_mode == 0)
2674 long l;
2675 int prompt;
2677 l = (long) (edit->curs_row - edit->widget.lines / 3);
2678 if (l > 0)
2679 edit_scroll_downward (edit, l);
2680 if (l < 0)
2681 edit_scroll_upward (edit, -l);
2683 edit_scroll_screen_over_cursor (edit);
2684 edit->force |= REDRAW_PAGE;
2685 edit_render_keypress (edit);
2687 /*so that undo stops at each query */
2688 edit_push_key_press (edit);
2689 /* and prompt 2/3 down */
2690 disp1 = edit_replace_cmd__conv_to_display (saved1);
2691 disp2 = edit_replace_cmd__conv_to_display (saved2);
2692 prompt = editcmd_dialog_replace_prompt_show (edit, disp1, disp2, -1, -1);
2693 g_free (disp1);
2694 g_free (disp2);
2696 if (prompt == B_REPLACE_ALL)
2697 edit->replace_mode = 1;
2698 else if (prompt == B_SKIP_REPLACE)
2700 if (edit_search_options.backwards)
2701 edit->search_start--;
2702 else
2703 edit->search_start++;
2704 continue; /* loop */
2706 else if (prompt == B_CANCEL)
2708 edit->replace_mode = -1;
2709 break; /* loop */
2713 repl_str = mc_search_prepare_replace_str (edit->search, input2_str);
2715 if (edit->search->error != MC_SEARCH_E_OK)
2717 edit_error_dialog (_("Replace"), edit->search->error_str);
2718 g_string_free (repl_str, TRUE);
2719 break;
2722 /* delete then insert new */
2723 for (i = 0; i < len; i++)
2724 edit_delete (edit, 1);
2726 for (i = 0; i < repl_str->len; i++)
2727 edit_insert (edit, repl_str->str[i]);
2729 edit->found_len = repl_str->len;
2730 g_string_free (repl_str, TRUE);
2731 times_replaced++;
2733 /* so that we don't find the same string again */
2734 if (edit_search_options.backwards)
2736 edit->search_start--;
2738 else
2740 edit->search_start += edit->found_len + (len == 0 ? 1 : 0);
2742 if (edit->search_start >= edit->last_byte)
2743 break;
2746 edit_scroll_screen_over_cursor (edit);
2748 else
2750 /* try and find from right here for next search */
2751 edit->search_start = edit->curs1;
2752 edit_update_curs_col (edit);
2754 edit->force |= REDRAW_PAGE;
2755 edit_render_keypress (edit);
2757 if (times_replaced == 0)
2758 query_dialog (_("Replace"), _("Search string not found"), D_NORMAL, 1, _("&OK"));
2759 break;
2762 while (edit->replace_mode >= 0);
2764 edit_scroll_screen_over_cursor (edit);
2765 edit->force |= REDRAW_COMPLETELY;
2766 edit_render_keypress (edit);
2768 if ((edit->replace_mode == 1) && (times_replaced != 0))
2769 message (D_NORMAL, _("Replace"), _("%ld replacements made"), times_replaced);
2771 cleanup:
2772 g_free (input1);
2773 g_free (input2);
2774 if (input2_str != NULL)
2775 g_string_free (input2_str, TRUE);
2778 /* --------------------------------------------------------------------------------------------- */
2780 mc_search_cbret_t
2781 edit_search_cmd_callback (const void *user_data, gsize char_offset, int *current_char)
2783 *current_char = edit_get_byte ((WEdit *) user_data, (off_t) char_offset);
2784 return MC_SEARCH_CB_OK;
2787 /* --------------------------------------------------------------------------------------------- */
2789 void
2790 edit_search_cmd (WEdit * edit, gboolean again)
2793 if (edit == NULL)
2794 return;
2796 if (!again)
2797 edit_search (edit);
2798 else if (edit->last_search_string != NULL)
2799 edit_do_search (edit);
2800 else
2802 /* find last search string in history */
2803 GList *history;
2805 history = history_get (MC_HISTORY_SHARED_SEARCH);
2806 if (history != NULL && history->data != NULL)
2808 edit->last_search_string = (char *) history->data;
2809 history->data = NULL;
2810 history = g_list_first (history);
2811 g_list_foreach (history, (GFunc) g_free, NULL);
2812 g_list_free (history);
2814 edit->search = mc_search_new (edit->last_search_string, -1);
2815 if (edit->search == NULL)
2817 /* if not... then ask for an expression */
2818 g_free (edit->last_search_string);
2819 edit->last_search_string = NULL;
2820 edit_search (edit);
2822 else
2824 edit->search->search_type = edit_search_options.type;
2825 edit->search->is_all_charsets = edit_search_options.all_codepages;
2826 edit->search->is_case_sensitive = edit_search_options.case_sens;
2827 edit->search->whole_words = edit_search_options.whole_words;
2828 edit->search->search_fn = edit_search_cmd_callback;
2829 edit->search_line_type = edit_get_search_line_type (edit->search);
2830 edit_do_search (edit);
2833 else
2835 /* if not... then ask for an expression */
2836 g_free (edit->last_search_string);
2837 edit->last_search_string = NULL;
2838 edit_search (edit);
2844 /* --------------------------------------------------------------------------------------------- */
2846 * Check if it's OK to close the file. If there are unsaved changes, ask user.
2848 * @returns TRUE if it's OK to exit, FALSE to continue editing.
2851 gboolean
2852 edit_ok_to_exit (WEdit * edit)
2854 char *fname = (char *) N_("[NoName]");
2855 char *msg;
2856 int act;
2858 if (!edit->modified)
2859 return TRUE;
2861 if (edit->filename_vpath != NULL)
2862 fname = vfs_path_to_str (edit->filename_vpath);
2863 #ifdef ENABLE_NLS
2864 else
2865 fname = g_strdup (_(fname));
2866 #else
2867 else
2868 fname = g_strdup (fname);
2869 #endif
2871 if (!mc_global.midnight_shutdown)
2873 if (!edit_check_newline (edit))
2875 g_free (fname);
2876 return FALSE;
2879 query_set_sel (2);
2881 msg = g_strdup_printf (_("File %s was modified.\nSave before close?"), fname);
2882 act = edit_query_dialog3 (_("Close file"), msg, _("&Yes"), _("&No"), _("&Cancel"));
2884 else
2886 msg = g_strdup_printf (_("Midnight Commander is being shut down.\nSave modified file %s?"),
2887 fname);
2888 act = edit_query_dialog2 (_("Quit"), msg, _("&Yes"), _("&No"));
2890 /* Esc is No */
2891 if (act == -1)
2892 act = 1;
2895 g_free (msg);
2896 g_free (fname);
2898 switch (act)
2900 case 0: /* Yes */
2901 edit_push_markers (edit);
2902 edit_set_markers (edit, 0, 0, 0, 0);
2903 if (!edit_save_cmd (edit) || mc_global.midnight_shutdown)
2904 return mc_global.midnight_shutdown;
2905 break;
2906 case 1: /* No */
2907 break;
2908 case 2: /* Cancel quit */
2909 case -1: /* Esc */
2910 return FALSE;
2913 return TRUE;
2916 /* --------------------------------------------------------------------------------------------- */
2917 /** save block, returns TRUE on success */
2919 gboolean
2920 edit_save_block (WEdit * edit, const char *filename, off_t start, off_t finish)
2922 int len, file;
2923 vfs_path_t *vpath;
2925 vpath = vfs_path_from_str (filename);
2926 file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
2927 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
2928 vfs_path_free (vpath);
2929 if (file == -1)
2930 return FALSE;
2932 if (edit->column_highlight)
2934 int r;
2936 r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
2937 if (r > 0)
2939 unsigned char *block, *p;
2941 p = block = edit_get_block (edit, start, finish, &len);
2942 while (len)
2944 r = mc_write (file, p, len);
2945 if (r < 0)
2946 break;
2947 p += r;
2948 len -= r;
2950 g_free (block);
2953 else
2955 unsigned char *buf;
2956 off_t i = start;
2957 off_t end;
2959 len = finish - start;
2960 buf = g_malloc0 (TEMP_BUF_LEN);
2961 while (start != finish)
2963 end = min (finish, start + TEMP_BUF_LEN);
2964 for (; i < end; i++)
2965 buf[i - start] = edit_get_byte (edit, i);
2966 len -= mc_write (file, (char *) buf, end - start);
2967 start = end;
2969 g_free (buf);
2971 mc_close (file);
2973 return (len == 0);
2976 /* --------------------------------------------------------------------------------------------- */
2978 void
2979 edit_paste_from_history (WEdit * edit)
2981 (void) edit;
2982 edit_error_dialog (_("Error"), _("This function is not implemented"));
2985 /* --------------------------------------------------------------------------------------------- */
2987 gboolean
2988 edit_copy_to_X_buf_cmd (WEdit * edit)
2990 off_t start_mark, end_mark;
2992 if (eval_marks (edit, &start_mark, &end_mark))
2993 return TRUE;
2994 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
2996 edit_error_dialog (_("Copy to clipboard"), get_sys_error (_("Unable to save to file")));
2997 return FALSE;
2999 /* try use external clipboard utility */
3000 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3002 return TRUE;
3005 /* --------------------------------------------------------------------------------------------- */
3007 gboolean
3008 edit_cut_to_X_buf_cmd (WEdit * edit)
3010 off_t start_mark, end_mark;
3012 if (eval_marks (edit, &start_mark, &end_mark))
3013 return TRUE;
3014 if (!edit_save_block_to_clip_file (edit, start_mark, end_mark))
3016 edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file"));
3017 return FALSE;
3019 /* try use external clipboard utility */
3020 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
3022 edit_block_delete_cmd (edit);
3023 edit_mark_cmd (edit, TRUE);
3025 return TRUE;
3028 /* --------------------------------------------------------------------------------------------- */
3030 gboolean
3031 edit_paste_from_X_buf_cmd (WEdit * edit)
3033 vfs_path_t *tmp;
3034 gboolean ret;
3036 /* try use external clipboard utility */
3037 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
3038 tmp = mc_config_get_full_vpath (EDIT_CLIP_FILE);
3039 ret = (edit_insert_file (edit, tmp) >= 0);
3040 vfs_path_free (tmp);
3042 return ret;
3045 /* --------------------------------------------------------------------------------------------- */
3047 * Ask user for the line and go to that line.
3048 * Negative numbers mean line from the end (i.e. -1 is the last line).
3051 void
3052 edit_goto_cmd (WEdit * edit)
3054 char *f;
3055 static long line = 0; /* line as typed, saved as default */
3056 long l;
3057 char *error;
3058 char s[32];
3060 g_snprintf (s, sizeof (s), "%ld", line);
3061 f = input_dialog (_("Goto line"), _("Enter line:"), MC_HISTORY_EDIT_GOTO_LINE, line ? s : "");
3062 if (!f)
3063 return;
3065 if (!*f)
3067 g_free (f);
3068 return;
3071 l = strtol (f, &error, 0);
3072 if (*error)
3074 g_free (f);
3075 return;
3078 line = l;
3079 if (l < 0)
3080 l = edit->total_lines + l + 2;
3081 edit_move_display (edit, l - edit->widget.lines / 2 - 1);
3082 edit_move_to_line (edit, l - 1);
3083 edit->force |= REDRAW_COMPLETELY;
3084 g_free (f);
3088 /* --------------------------------------------------------------------------------------------- */
3089 /** Return TRUE on success */
3091 gboolean
3092 edit_save_block_cmd (WEdit * edit)
3094 off_t start_mark, end_mark;
3095 char *exp, *tmp;
3096 gboolean ret = FALSE;
3098 if (eval_marks (edit, &start_mark, &end_mark))
3099 return TRUE;
3101 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3102 exp =
3103 input_expand_dialog (_("Save block"), _("Enter file name:"),
3104 MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
3105 g_free (tmp);
3106 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3108 if (exp != NULL && *exp != '\0')
3110 if (edit_save_block (edit, exp, start_mark, end_mark))
3111 ret = TRUE;
3112 else
3113 edit_error_dialog (_("Save block"), get_sys_error (_("Cannot save file")));
3115 edit->force |= REDRAW_COMPLETELY;
3118 g_free (exp);
3120 return ret;
3124 /* --------------------------------------------------------------------------------------------- */
3126 /** returns TRUE on success */
3127 gboolean
3128 edit_insert_file_cmd (WEdit * edit)
3130 char *tmp;
3131 char *exp;
3132 gboolean ret = FALSE;
3134 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
3135 exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
3136 MC_HISTORY_EDIT_INSERT_FILE, tmp);
3137 g_free (tmp);
3139 edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
3141 if (exp != NULL && *exp != '\0')
3143 vfs_path_t *exp_vpath;
3145 exp_vpath = vfs_path_from_str (exp);
3146 ret = (edit_insert_file (edit, exp_vpath) >= 0);
3147 vfs_path_free (exp_vpath);
3149 if (!ret)
3150 edit_error_dialog (_("Insert file"), get_sys_error (_("Cannot insert file")));
3153 g_free (exp);
3155 edit->force |= REDRAW_COMPLETELY;
3156 return ret;
3159 /* --------------------------------------------------------------------------------------------- */
3160 /** sorts a block, returns -1 on system fail, 1 on cancel and 0 on success */
3163 edit_sort_cmd (WEdit * edit)
3165 static char *old = 0;
3166 char *exp, *tmp, *tmp_edit_block_name, *tmp_edit_temp_name;
3167 off_t start_mark, end_mark;
3168 int e;
3170 if (eval_marks (edit, &start_mark, &end_mark))
3172 edit_error_dialog (_("Sort block"), _("You must first highlight a block of text"));
3173 return 0;
3176 tmp = mc_config_get_full_path (EDIT_BLOCK_FILE);
3177 edit_save_block (edit, tmp, start_mark, end_mark);
3178 g_free (tmp);
3180 exp = input_dialog (_("Run sort"),
3181 _("Enter sort options (see manpage) separated by whitespace:"),
3182 MC_HISTORY_EDIT_SORT, (old != NULL) ? old : "");
3184 if (!exp)
3185 return 1;
3186 g_free (old);
3187 old = exp;
3188 tmp_edit_block_name = mc_config_get_full_path (EDIT_BLOCK_FILE);
3189 tmp_edit_temp_name = mc_config_get_full_path (EDIT_TEMP_FILE);
3190 tmp =
3191 g_strconcat (" sort ", exp, " ", tmp_edit_block_name,
3192 " > ", tmp_edit_temp_name, (char *) NULL);
3193 g_free (tmp_edit_temp_name);
3194 g_free (tmp_edit_block_name);
3196 e = system (tmp);
3197 g_free (tmp);
3198 if (e)
3200 if (e == -1 || e == 127)
3202 edit_error_dialog (_("Sort"), get_sys_error (_("Cannot execute sort command")));
3204 else
3206 char q[8];
3207 sprintf (q, "%d ", e);
3208 tmp = g_strdup_printf (_("Sort returned non-zero: %s"), q);
3209 edit_error_dialog (_("Sort"), tmp);
3210 g_free (tmp);
3212 return -1;
3215 edit->force |= REDRAW_COMPLETELY;
3217 if (edit_block_delete_cmd (edit))
3218 return 1;
3221 vfs_path_t *tmp_vpath;
3223 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3224 edit_insert_file (edit, tmp_vpath);
3225 vfs_path_free (tmp_vpath);
3227 return 0;
3230 /* --------------------------------------------------------------------------------------------- */
3232 * Ask user for a command, execute it and paste its output back to the
3233 * editor.
3237 edit_ext_cmd (WEdit * edit)
3239 char *exp, *tmp, *tmp_edit_temp_file;
3240 int e;
3242 exp =
3243 input_dialog (_("Paste output of external command"),
3244 _("Enter shell command(s):"), MC_HISTORY_EDIT_PASTE_EXTCMD, NULL);
3246 if (!exp)
3247 return 1;
3249 tmp_edit_temp_file = mc_config_get_full_path (EDIT_TEMP_FILE);
3250 tmp = g_strconcat (exp, " > ", tmp_edit_temp_file, (char *) NULL);
3251 g_free (tmp_edit_temp_file);
3252 e = system (tmp);
3253 g_free (tmp);
3254 g_free (exp);
3256 if (e)
3258 edit_error_dialog (_("External command"), get_sys_error (_("Cannot execute command")));
3259 return -1;
3262 edit->force |= REDRAW_COMPLETELY;
3265 vfs_path_t *tmp_vpath;
3267 tmp_vpath = mc_config_get_full_vpath (EDIT_TEMP_FILE);
3268 edit_insert_file (edit, tmp_vpath);
3269 vfs_path_free (tmp_vpath);
3271 return 0;
3274 /* --------------------------------------------------------------------------------------------- */
3275 /** if block is 1, a block must be highlighted and the shell command
3276 processes it. If block is 0 the shell command is a straight system
3277 command, that just produces some output which is to be inserted */
3279 void
3280 edit_block_process_cmd (WEdit * edit, int macro_number)
3282 char *fname;
3283 char *macros_fname = NULL;
3285 fname = g_strdup_printf ("%s.%i.sh", MC_EXTMACRO_FILE, macro_number);
3286 macros_fname = g_build_filename (mc_config_get_data_path (), fname, (char *) NULL);
3287 user_menu (edit, macros_fname, 0);
3288 g_free (fname);
3289 g_free (macros_fname);
3290 edit->force |= REDRAW_COMPLETELY;
3293 /* --------------------------------------------------------------------------------------------- */
3295 void
3296 edit_mail_dialog (WEdit * edit)
3298 char *tmail_to;
3299 char *tmail_subject;
3300 char *tmail_cc;
3302 static char *mail_cc_last = 0;
3303 static char *mail_subject_last = 0;
3304 static char *mail_to_last = 0;
3306 QuickWidget quick_widgets[] = {
3307 /* 0 */ QUICK_BUTTON (6, 10, 9, MAIL_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL),
3308 /* 1 */ QUICK_BUTTON (2, 10, 9, MAIL_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL),
3309 /* 2 */ QUICK_INPUT (3, 50, 8, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input", &tmail_cc),
3310 /* 3 */ QUICK_LABEL (3, 50, 7, MAIL_DLG_HEIGHT, N_("Copies to")),
3311 /* 4 */ QUICK_INPUT (3, 50, 6, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-2",
3312 &tmail_subject),
3313 /* 5 */ QUICK_LABEL (3, 50, 5, MAIL_DLG_HEIGHT, N_("Subject")),
3314 /* 6 */ QUICK_INPUT (3, 50, 4, MAIL_DLG_HEIGHT, "", 44, 0, "mail-dlg-input-3", &tmail_to),
3315 /* 7 */ QUICK_LABEL (3, 50, 3, MAIL_DLG_HEIGHT, N_("To")),
3316 /* 8 */ QUICK_LABEL (3, 50, 2, MAIL_DLG_HEIGHT, N_("mail -s <subject> -c <cc> <to>")),
3317 QUICK_END
3320 QuickDialog Quick_input = {
3321 50, MAIL_DLG_HEIGHT, -1, -1, N_("Mail"),
3322 "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE
3325 quick_widgets[2].u.input.text = mail_cc_last ? mail_cc_last : "";
3326 quick_widgets[4].u.input.text = mail_subject_last ? mail_subject_last : "";
3327 quick_widgets[6].u.input.text = mail_to_last ? mail_to_last : "";
3329 if (quick_dialog (&Quick_input) != B_CANCEL)
3331 g_free (mail_cc_last);
3332 g_free (mail_subject_last);
3333 g_free (mail_to_last);
3334 mail_cc_last = tmail_cc;
3335 mail_subject_last = tmail_subject;
3336 mail_to_last = tmail_to;
3337 pipe_mail (edit, mail_to_last, mail_subject_last, mail_cc_last);
3342 /*******************/
3343 /* Word Completion */
3344 /*******************/
3346 /* --------------------------------------------------------------------------------------------- */
3348 * Complete current word using regular expression search
3349 * backwards beginning at the current cursor position.
3352 void
3353 edit_complete_word_cmd (WEdit * edit)
3355 gsize i, max_len, word_len = 0, num_compl = 0;
3356 off_t word_start = 0;
3357 unsigned char *bufpos;
3358 char *match_expr;
3359 struct selection compl[MAX_WORD_COMPLETIONS]; /* completions */
3361 /* search start of word to be completed */
3362 if (!edit_find_word_start (edit, &word_start, &word_len))
3363 return;
3365 /* prepare match expression */
3366 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3368 /* match_expr = g_strdup_printf ("\\b%.*s[a-zA-Z_0-9]+", word_len, bufpos); */
3369 match_expr =
3370 g_strdup_printf
3371 ("(^|\\s+|\\b)%.*s[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+",
3372 (int) word_len, bufpos);
3374 /* collect the possible completions */
3375 /* start search from begin to end of file */
3376 max_len =
3377 edit_collect_completions (edit, word_start, word_len, match_expr,
3378 (struct selection *) &compl, &num_compl);
3380 if (num_compl > 0)
3382 /* insert completed word if there is only one match */
3383 if (num_compl == 1)
3385 for (i = word_len; i < compl[0].len; i++)
3386 edit_insert (edit, *(compl[0].text + i));
3388 /* more than one possible completion => ask the user */
3389 else
3391 /* !!! usually only a beep is expected and when <ALT-TAB> is !!! */
3392 /* !!! pressed again the selection dialog pops up, but that !!! */
3393 /* !!! seems to require a further internal state !!! */
3394 /*tty_beep (); */
3396 /* let the user select the preferred completion */
3397 editcmd_dialog_completion_show (edit, max_len, word_len,
3398 (struct selection *) &compl, num_compl);
3402 g_free (match_expr);
3403 /* release memory before return */
3404 for (i = 0; i < num_compl; i++)
3405 g_free (compl[i].text);
3408 /* --------------------------------------------------------------------------------------------- */
3410 #ifdef HAVE_CHARSET
3411 void
3412 edit_select_codepage_cmd (WEdit * edit)
3414 if (do_select_codepage ())
3415 edit_set_codeset (edit);
3417 edit->force = REDRAW_PAGE;
3418 send_message ((Widget *) edit, WIDGET_DRAW, 0);
3420 #endif
3422 /* --------------------------------------------------------------------------------------------- */
3424 void
3425 edit_insert_literal_cmd (WEdit * edit)
3427 int char_for_insertion;
3429 char_for_insertion = editcmd_dialog_raw_key_query (_("Insert literal"),
3430 _("Press any key:"), FALSE);
3431 edit_execute_key_command (edit, -1, ascii_alpha_to_cntrl (char_for_insertion));
3434 /* --------------------------------------------------------------------------------------------- */
3436 void
3437 edit_begin_end_macro_cmd (WEdit * edit)
3439 /* edit is a pointer to the widget */
3440 if (edit != NULL)
3442 unsigned long command = macro_index < 0 ? CK_MacroStartRecord : CK_MacroStopRecord;
3443 edit_execute_key_command (edit, command, -1);
3447 /* --------------------------------------------------------------------------------------------- */
3449 void
3450 edit_begin_end_repeat_cmd (WEdit * edit)
3452 /* edit is a pointer to the widget */
3453 if (edit != NULL)
3455 unsigned long command = macro_index < 0 ? CK_RepeatStartRecord : CK_RepeatStopRecord;
3456 edit_execute_key_command (edit, command, -1);
3460 /* --------------------------------------------------------------------------------------------- */
3462 gboolean
3463 edit_load_forward_cmd (WEdit * edit)
3465 if (edit->modified
3466 && edit_query_dialog2 (_("Warning"),
3467 _("Current text was modified without a file save.\n"
3468 "Continue discards these changes"), _("C&ontinue"),
3469 _("&Cancel")) == 1)
3471 edit->force |= REDRAW_COMPLETELY;
3472 return TRUE;
3475 if (edit_stack_iterator + 1 >= MAX_HISTORY_MOVETO)
3476 return FALSE;
3478 if (edit_history_moveto[edit_stack_iterator + 1].line < 1)
3479 return FALSE;
3481 edit_stack_iterator++;
3482 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3483 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3484 edit_history_moveto[edit_stack_iterator].line);
3486 return FALSE;
3489 /* --------------------------------------------------------------------------------------------- */
3491 gboolean
3492 edit_load_back_cmd (WEdit * edit)
3494 if (edit->modified
3495 && edit_query_dialog2 (_("Warning"),
3496 _("Current text was modified without a file save.\n"
3497 "Continue discards these changes"), _("C&ontinue"),
3498 _("&Cancel")) == 1)
3500 edit->force |= REDRAW_COMPLETELY;
3501 return TRUE;
3504 /* we are in the bottom of the stack, NO WAY! */
3505 if (edit_stack_iterator == 0)
3506 return FALSE;
3508 edit_stack_iterator--;
3509 if (edit_history_moveto[edit_stack_iterator].filename_vpath != NULL)
3510 return edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
3511 edit_history_moveto[edit_stack_iterator].line);
3513 return FALSE;
3516 /* --------------------------------------------------------------------------------------------- */
3518 void
3519 edit_get_match_keyword_cmd (WEdit * edit)
3521 gsize word_len = 0, max_len = 0;
3522 int num_def = 0;
3523 int i;
3524 off_t word_start = 0;
3525 unsigned char *bufpos;
3526 char *match_expr;
3527 char *path = NULL;
3528 char *ptr = NULL;
3529 char *tagfile = NULL;
3531 etags_hash_t def_hash[MAX_DEFINITIONS];
3533 for (i = 0; i < MAX_DEFINITIONS; i++)
3535 def_hash[i].filename = NULL;
3538 /* search start of word to be completed */
3539 if (!edit_find_word_start (edit, &word_start, &word_len))
3540 return;
3542 /* prepare match expression */
3543 bufpos = &edit->buffers1[word_start >> S_EDIT_BUF_SIZE][word_start & M_EDIT_BUF_SIZE];
3544 match_expr = g_strdup_printf ("%.*s", (int) word_len, bufpos);
3546 ptr = g_get_current_dir ();
3547 path = g_strconcat (ptr, G_DIR_SEPARATOR_S, (char *) NULL);
3548 g_free (ptr);
3550 /* Recursive search file 'TAGS' in parent dirs */
3553 ptr = g_path_get_dirname (path);
3554 g_free (path);
3555 path = ptr;
3556 g_free (tagfile);
3557 tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL);
3558 if (exist_file (tagfile))
3559 break;
3561 while (strcmp (path, G_DIR_SEPARATOR_S) != 0);
3563 if (tagfile)
3565 num_def =
3566 etags_set_definition_hash (tagfile, path, match_expr, (etags_hash_t *) & def_hash);
3567 g_free (tagfile);
3569 g_free (path);
3571 max_len = MAX_WIDTH_DEF_DIALOG;
3572 word_len = 0;
3573 if (num_def > 0)
3575 editcmd_dialog_select_definition_show (edit, match_expr, max_len, word_len,
3576 (etags_hash_t *) & def_hash, num_def);
3578 g_free (match_expr);
3581 /* --------------------------------------------------------------------------------------------- */
3583 #ifdef HAVE_ASPELL
3585 edit_suggest_current_word (WEdit * edit)
3587 gsize cut_len = 0;
3588 gsize word_len = 0;
3589 off_t word_start = 0;
3590 int retval = B_SKIP_WORD;
3591 char *match_word;
3593 /* search start of word to spell check */
3594 match_word = edit_get_word_from_pos (edit, edit->curs1, &word_start, &word_len, &cut_len);
3596 #ifdef HAVE_CHARSET
3597 if (mc_global.source_codepage >= 0 && (mc_global.source_codepage != mc_global.display_codepage))
3599 GString *tmp_word;
3601 tmp_word = str_convert_to_display (match_word);
3602 g_free (match_word);
3603 match_word = g_string_free (tmp_word, FALSE);
3605 #endif
3606 if (!aspell_check (match_word, (int) word_len))
3608 GArray *suggest;
3609 unsigned int res;
3611 suggest = g_array_new (TRUE, FALSE, sizeof (char *));
3613 res = aspell_suggest (suggest, match_word, (int) word_len);
3614 if (res != 0)
3616 char *new_word = NULL;
3618 edit->found_start = word_start;
3619 edit->found_len = word_len;
3620 edit->force |= REDRAW_PAGE;
3621 edit_scroll_screen_over_cursor (edit);
3622 edit_render_keypress (edit);
3624 retval = spell_dialog_spell_suggest_show (edit, match_word, &new_word, suggest);
3625 edit_cursor_move (edit, word_len - cut_len);
3627 if (retval == B_ENTER && new_word != NULL)
3629 guint i;
3630 char *cp_word;
3632 #ifdef HAVE_CHARSET
3633 if (mc_global.source_codepage >= 0 &&
3634 (mc_global.source_codepage != mc_global.display_codepage))
3636 GString *tmp_word;
3638 tmp_word = str_convert_to_input (new_word);
3639 g_free (new_word);
3640 new_word = g_string_free (tmp_word, FALSE);
3642 #endif
3643 cp_word = new_word;
3644 for (i = 0; i < word_len; i++)
3645 edit_backspace (edit, 1);
3646 for (; *new_word; new_word++)
3647 edit_insert (edit, *new_word);
3648 g_free (cp_word);
3650 else if (retval == B_ADD_WORD && match_word != NULL)
3651 aspell_add_to_dict (match_word, (int) word_len);
3654 g_array_free (suggest, TRUE);
3655 edit->found_start = 0;
3656 edit->found_len = 0;
3658 g_free (match_word);
3659 return retval;
3662 /* --------------------------------------------------------------------------------------------- */
3664 void
3665 edit_spellcheck_file (WEdit * edit)
3667 if (edit->curs_line > 0)
3669 edit_cursor_move (edit, -edit->curs1);
3670 edit_move_to_prev_col (edit, 0);
3671 edit_update_curs_row (edit);
3676 int c1, c2;
3678 c2 = edit_get_byte (edit, edit->curs1);
3682 if (edit->curs1 >= edit->last_byte)
3683 return;
3685 c1 = c2;
3686 edit_cursor_move (edit, 1);
3687 c2 = edit_get_byte (edit, edit->curs1);
3689 while (is_break_char (c1) || is_break_char (c2));
3691 while (edit_suggest_current_word (edit) != B_CANCEL);
3694 /* --------------------------------------------------------------------------------------------- */
3696 void
3697 edit_set_spell_lang (void)
3699 GArray *lang_list;
3701 lang_list = g_array_new (TRUE, FALSE, sizeof (char *));
3702 if (aspell_get_lang_list (lang_list) != 0)
3704 char *lang;
3706 lang = spell_dialog_lang_list_show (lang_list);
3707 if (lang != NULL)
3708 (void) aspell_set_lang (lang);
3710 aspell_array_clean (lang_list);
3712 #endif /* HAVE_ASPELL */
3714 /* --------------------------------------------------------------------------------------------- */