Ticket #46 (mcedit: check POSIX newline at end of file)

* added warning dialog (showed before save/save as)
    * added option "editor_check_new_line"
    * changed dialog 'Edit Save Mode' (added check box 'Check POSIX new line')

Signed-off-by: Ilia Maslakov <il.smind@gmail.com>

    * Redesing of 'Edit Save Mode' dialog.
    * Added hotkeys in 'Edit Save Mode' dialog.
    * Modified warning message, minor optimization.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Ilia Maslakov 2009-10-26 16:32:56 +00:00
parent 47ee28f6eb
commit 467d833ac3
5 changed files with 50 additions and 25 deletions

View File

@ -319,7 +319,6 @@ typedef enum {
extern int option_max_undo; extern int option_max_undo;
extern int option_auto_syntax; extern int option_auto_syntax;
extern char *option_syntax_type; extern char *option_syntax_type;
extern int editor_option_check_nl_at_eof;
extern int option_edit_right_extreme; extern int option_edit_right_extreme;
extern int option_edit_left_extreme; extern int option_edit_left_extreme;

View File

@ -81,6 +81,7 @@ int option_edit_left_extreme = 0;
int option_edit_top_extreme = 0; int option_edit_top_extreme = 0;
int option_edit_bottom_extreme = 0; int option_edit_bottom_extreme = 0;
int enable_show_tabs_tws = 1; int enable_show_tabs_tws = 1;
int option_check_nl_at_eof = 0;
const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_"; const char *option_whole_chars_search = "0123456789abcdefghijklmnopqrstuvwxyz_";
char *option_backup_ext = NULL; char *option_backup_ext = NULL;

View File

@ -60,6 +60,7 @@ extern int visible_tabs;
extern int visible_tws; extern int visible_tws;
extern int simple_statusbar; extern int simple_statusbar;
extern int option_check_nl_at_eof;
/* used in main() */ /* used in main() */
void edit_stack_init (void); void edit_stack_init (void);

View File

@ -394,24 +394,29 @@ menu_save_mode_cmd (void)
{ {
/* diaog sizes */ /* diaog sizes */
const int DLG_X = 38; const int DLG_X = 38;
const int DLG_Y = 10; const int DLG_Y = 13;
char *str_result; char *str_result;
const char *str[] = const char *str[] =
{ {
N_("Quick save "), N_("&Quick save"),
N_("Safe save "), N_("&Safe save"),
N_("Do backups -->") N_("&Do backups with following extension:")
}; };
QuickWidget widgets[] = QuickWidget widgets[] =
{ {
/* 0 */ QUICK_BUTTON (18, DLG_X, 7, DLG_Y, N_("&Cancel"), B_CANCEL, NULL), /* 0 */
/* 1 */ QUICK_BUTTON ( 6, DLG_X, 7, DLG_Y, N_("&OK"), B_ENTER, NULL), QUICK_BUTTON (18, DLG_X, DLG_Y - 3, DLG_Y, N_("&Cancel"), B_CANCEL, NULL),
/* 2 */ QUICK_INPUT (23, DLG_X, 5, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result), /* 1 */
/* 3 */ QUICK_LABEL (22, DLG_X, 4, DLG_Y, N_("Extension:")), QUICK_BUTTON ( 6, DLG_X, DLG_Y - 3, DLG_Y, N_("&OK"), B_ENTER, NULL),
/* 4 */ QUICK_RADIO ( 4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode), /* 2 */
QUICK_CHECKBOX ( 4, DLG_X, 8, DLG_Y, N_("Check &POSIX new line"), &option_check_nl_at_eof),
/* 3 */
QUICK_INPUT ( 8, DLG_X, 6, DLG_Y, option_backup_ext, 9, 0, "edit-backup-ext", &str_result),
/* 4 */
QUICK_RADIO ( 4, DLG_X, 3, DLG_Y, 3, str, &option_save_mode),
QUICK_END QUICK_END
}; };
@ -424,36 +429,35 @@ menu_save_mode_cmd (void)
size_t i; size_t i;
size_t maxlen = 0; size_t maxlen = 0;
int dlg_x; int dlg_x;
size_t l1, w0, w1, w3; size_t w0, w1, b_len, w3;
assert (option_backup_ext != NULL); assert (option_backup_ext != NULL);
/* OK/Cancel buttons */ /* OK/Cancel buttons */
w0 = str_term_width1 (_(widgets[0].u.button.text)) + 2; w0 = str_term_width1 (_(widgets[0].u.button.text)) + 3;
w1 = str_term_width1 (_(widgets[1].u.button.text)) + 4; /* default batton */ w1 = str_term_width1 (_(widgets[1].u.button.text)) + 5; /* default button */
b_len = w0 + w1 + 3;
w3 = str_term_width1 (_(widgets[3].u.label.text)); maxlen = max (b_len, str_term_width1 (_(dialog.title)) + 2);
maxlen = l1 = w0 + w1 + 6;
w3 = 0;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
#ifdef ENABLE_NLS #ifdef ENABLE_NLS
str[i] = _(str[i]); str[i] = _(str[i]);
#endif #endif
maxlen = max (maxlen, (size_t) str_term_width1 (str[i]) + 7); w3 = max (w3, (size_t) str_term_width1 (str[i]));
} }
dlg_x = maxlen + w3 + 5 + 2; maxlen = max (maxlen, w3 + 4);
widgets[2].u.input.len = w3; /* input field length */
dlg_x = min (COLS, dlg_x);
dialog.xlen = dlg_x;
widgets[0].relative_x = dlg_x * 2/3 - w0/2; dialog.xlen = min (COLS, maxlen + 8);
widgets[1].relative_x = dlg_x/3 - w1/2;
widgets[2].relative_x = widgets[3].relative_x = maxlen + 3; widgets[3].u.input.len = w3;
widgets[1].relative_x = (dialog.xlen - b_len)/2;
widgets[0].relative_x = widgets[1].relative_x + w0 + 2;
for (i = 0; i < sizeof (widgets)/sizeof (widgets[0]); i++) for (i = 0; i < sizeof (widgets)/sizeof (widgets[0]); i++)
widgets[i].x_divisions = dlg_x; widgets[i].x_divisions = dialog.xlen;
if (quick_dialog (&dialog) != B_CANCEL) { if (quick_dialog (&dialog) != B_CANCEL) {
g_free (option_backup_ext); g_free (option_backup_ext);
@ -476,6 +480,16 @@ edit_set_filename (WEdit *edit, const char *f)
#endif #endif
} }
static gboolean
edit_check_newline (WEdit *edit)
{
return !(option_check_nl_at_eof && edit->last_byte > 0
&& edit_get_byte (edit, edit->last_byte - 1) != '\n'
&& edit_query_dialog2 (_("Warning"),
_("The file you are saving is not finished with a newline"),
_("C&ontinue"), _("&Cancel")));
}
static char * static char *
edit_get_save_file_as (WEdit *edit) edit_get_save_file_as (WEdit *edit)
{ {
@ -534,6 +548,9 @@ edit_save_as_cmd (WEdit *edit)
int save_lock = 0; int save_lock = 0;
int different_filename = 0; int different_filename = 0;
if (!edit_check_newline (edit))
return 0;
exp = edit_get_save_file_as (edit); exp = edit_get_save_file_as (edit);
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_action (edit, KEY_PRESS + edit->start_display);
@ -816,6 +833,9 @@ int edit_save_confirm_cmd (WEdit * edit)
{ {
gchar *f = NULL; gchar *f = NULL;
if (!edit_check_newline (edit))
return 0;
if (edit_confirm_save) { if (edit_confirm_save) {
f = g_strconcat (_(" Confirm save file? : "), edit->filename, " ", NULL); f = g_strconcat (_(" Confirm save file? : "), edit->filename, " ", NULL);
if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel"))){ if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel"))){
@ -1767,6 +1787,9 @@ edit_ok_to_exit (WEdit *edit)
if (!edit->modified) if (!edit->modified)
return 1; return 1;
if (!edit_check_newline (edit))
return 0;
switch (edit_query_dialog3 switch (edit_query_dialog3
(_("Quit"), _(" File was modified, Save with exit? "), (_("Quit"), _(" File was modified, Save with exit? "),
_("&Cancel quit"), _("&Yes"), _("&No"))) { _("&Cancel quit"), _("&Yes"), _("&No"))) {

View File

@ -217,6 +217,7 @@ static const struct {
{ "editor_visible_spaces", &visible_tws }, { "editor_visible_spaces", &visible_tws },
{ "editor_line_state", &option_line_state }, { "editor_line_state", &option_line_state },
{ "editor_simple_statusbar", &simple_statusbar }, { "editor_simple_statusbar", &simple_statusbar },
{ "editor_check_new_line", &option_check_nl_at_eof },
#endif /* USE_INTERNAL_EDIT */ #endif /* USE_INTERNAL_EDIT */
{ "nice_rotating_dash", &nice_rotating_dash }, { "nice_rotating_dash", &nice_rotating_dash },