Merge branch '402_select_codepage'

* 402_select_codepage:
  Fixed source_codepage handling in editor (#402).
  Fixed type of variables that store result of get_codepage_id().
  Fixed codepage selection (#402).
This commit is contained in:
Andrew Borodin 2009-06-16 15:18:12 +04:00
commit 537bc71069
9 changed files with 113 additions and 61 deletions

View File

@ -281,13 +281,18 @@ edit_load_file_fast (WEdit *edit, const char *filename)
{ {
long buf, buf2; long buf, buf2;
int file = -1; int file = -1;
#ifdef HAVE_CHARSET
const char *cp_id;
#endif
edit->curs2 = edit->last_byte; edit->curs2 = edit->last_byte;
buf2 = edit->curs2 >> S_EDIT_BUF_SIZE; buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
edit->utf8 = 0; edit->utf8 = 0;
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
if ( get_codepage_id( source_codepage ) ) cp_id = get_codepage_id (source_codepage);
edit->utf8 = str_isutf8 (get_codepage_id( source_codepage ));
if (cp_id != NULL)
edit->utf8 = str_isutf8 (cp_id);
#endif #endif
if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) { if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
GString *errmsg = g_string_new(NULL); GString *errmsg = g_string_new(NULL);

View File

@ -2335,9 +2335,16 @@ void
edit_select_codepage_cmd (WEdit *edit) edit_select_codepage_cmd (WEdit *edit)
{ {
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
do_select_codepage (); if (do_select_codepage ()) {
if ( get_codepage_id (source_codepage) ) const char *cp_id;
edit->utf8 = str_isutf8 (get_codepage_id (source_codepage));
cp_id = get_codepage_id (source_codepage >= 0 ?
source_codepage : display_codepage);
if (cp_id != NULL)
edit->utf8 = str_isutf8 (cp_id);
}
edit->force = REDRAW_COMPLETELY; edit->force = REDRAW_COMPLETELY;
edit_refresh_cmd (edit); edit_refresh_cmd (edit);
#endif #endif

View File

@ -126,7 +126,7 @@ static void status_string (WEdit * edit, char *s, int w)
byte_str, byte_str,
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
get_codepage_id ( source_codepage ) source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
#else #else
"" ""
#endif #endif
@ -150,7 +150,7 @@ static void status_string (WEdit * edit, char *s, int w)
byte_str, byte_str,
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
get_codepage_id ( source_codepage ) source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
#else #else
"" ""
#endif #endif

View File

@ -48,7 +48,6 @@
#include "../src/key.h" /* KEY_M_SHIFT */ #include "../src/key.h" /* KEY_M_SHIFT */
#include "../src/tty.h" /* keys */ #include "../src/tty.h" /* keys */
#include "../src/charsets.h" /* convert_from_input_c() */ #include "../src/charsets.h" /* convert_from_input_c() */
#include "../src/selcodepage.h" /* do_select_codepage() */
#include "../src/main.h" /* display_codepage */ #include "../src/main.h" /* display_codepage */
#include "../src/strutil.h" /* str_isutf8 () */ #include "../src/strutil.h" /* str_isutf8 () */

View File

@ -567,19 +567,26 @@ static WCheck *inpcheck;
static int static int
sel_charset_button (int action) sel_charset_button (int action)
{ {
const char *cpname; int new_dcp;
char buf[64];
(void) action; (void) action;
new_display_codepage = select_charset (0, 0, new_display_codepage, 1); new_dcp = select_charset (0, 0, new_display_codepage, TRUE);
cpname = (new_display_codepage < 0)
? _("Other 8 bit") if (new_dcp != SELECT_CHARSET_CANCEL) {
: codepages[new_display_codepage].name; const char *cpname;
if ( cpname ) char buf[BUF_TINY];
utf8_display = str_isutf8 (cpname);
/* avoid strange bug with label repainting */ new_display_codepage = new_dcp;
g_snprintf (buf, sizeof (buf), "%-27s", cpname); cpname = (new_display_codepage == SELECT_CHARSET_OTHER_8BIT) ?
label_set_text (cplabel, buf); _("Other 8 bit") : codepages[new_display_codepage].name;
if (cpname != NULL)
utf8_display = str_isutf8 (cpname);
/* avoid strange bug with label repainting */
g_snprintf (buf, sizeof (buf), "%-27s", cpname);
label_set_text (cplabel, buf);
}
return 0; return 0;
} }

View File

@ -54,7 +54,7 @@
#include "main.h" /* the_menubar */ #include "main.h" /* the_menubar */
#include "unixcompat.h" #include "unixcompat.h"
#include "mountlist.h" /* my_statfs */ #include "mountlist.h" /* my_statfs */
#include "selcodepage.h" /* do_select_codepage () */ #include "selcodepage.h" /* select_charset () */
#include "charsets.h" /* get_codepage_id () */ #include "charsets.h" /* get_codepage_id () */
#include "strutil.h" #include "strutil.h"
@ -2651,13 +2651,13 @@ set_panel_encoding (WPanel *panel)
int r; int r;
int width = (panel->widget.x)? panel->widget.cols : panel->widget.cols * (-1); int width = (panel->widget.x)? panel->widget.cols : panel->widget.cols * (-1);
r = select_charset (width, 0, source_codepage, 0); r = select_charset (width, 0, source_codepage, FALSE);
if ( r == -2) if (r == SELECT_CHARSET_CANCEL)
return; return; /* Cancel */
if (r == SELECT_CHARSET_NO_TRANSLATE) {
if (r == -1){ /* No translation */
errmsg = init_translation_table (display_codepage, display_codepage); errmsg = init_translation_table (display_codepage, display_codepage);
cd_path = remove_encoding_from_path (panel->cwd); cd_path = remove_encoding_from_path (panel->cwd);
do_panel_cd (panel, cd_path, 0); do_panel_cd (panel, cd_path, 0);
@ -2672,13 +2672,13 @@ set_panel_encoding (WPanel *panel)
message (D_ERROR, MSG_ERROR, "%s", errmsg); message (D_ERROR, MSG_ERROR, "%s", errmsg);
return; return;
} }
encoding = g_strdup( get_codepage_id ( source_codepage ) );
encoding = get_codepage_id (source_codepage);
#endif #endif
if (encoding) { if (encoding != NULL) {
cd_path = add_encoding_to_path (panel->cwd, encoding); cd_path = add_encoding_to_path (panel->cwd, encoding);
if (!do_panel_cd (panel, cd_path, 0)) if (!do_panel_cd (panel, cd_path, 0))
message (1, MSG_ERROR, _(" Cannot chdir to %s "), cd_path); message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
g_free (cd_path); g_free (cd_path);
g_free (encoding);
} }
} }

View File

@ -48,16 +48,22 @@ get_hotkey (int n)
return (n <= 9) ? '0' + n : 'a' + n - 10; return (n <= 9) ? '0' + n : 'a' + n - 10;
} }
/* Return value:
* -2 (SELECT_CHARSET_CANCEL) : Cancel
* -1 (SELECT_CHARSET_OTHER_8BIT) : "Other 8 bit" if seldisplay == TRUE
* -1 (SELECT_CHARSET_NO_TRANSLATE) : "No translation" if seldisplay == FALSE
* >= 0 : charset number
*/
int int
select_charset (int delta_x, int delta_y, int current_charset, int seldisplay) select_charset (int delta_x, int delta_y, int current_charset, gboolean seldisplay)
{ {
int i, menu_lines = n_codepages + 1; int i;
char buffer[255]; char buffer[255];
/* Create listbox */ /* Create listbox */
Listbox *listbox = create_listbox_window_delta (delta_x, delta_y, Listbox *listbox = create_listbox_window_delta (delta_x, delta_y,
ENTRY_LEN + 2, menu_lines, ENTRY_LEN + 2, n_codepages + 1,
_(" Choose input codepage "), _("Choose codepage"),
"[Codepages Translation]"); "[Codepages Translation]");
if (!seldisplay) if (!seldisplay)
@ -87,29 +93,40 @@ select_charset (int delta_x, int delta_y, int current_charset, int seldisplay)
i = run_listbox (listbox); i = run_listbox (listbox);
return (seldisplay) ? ((i >= n_codepages) ? -1 : i) if (i < 0) {
: (i - 1); /* Cancel dialog */
return SELECT_CHARSET_CANCEL;
} else {
/* some charset has been selected */
if (seldisplay) {
/* charset list is finished with "Other 8 bit" item */
return (i >= n_codepages) ? SELECT_CHARSET_OTHER_8BIT : i;
} else {
/* charset list is began with "- < No translation >" item */
return (i - 1);
}
}
} }
/* Helper functions for codepages support */ gboolean
int
do_select_codepage (void) do_select_codepage (void)
{ {
const char *errmsg; const char *errmsg = NULL;
int r; int r;
r = select_charset (0, 0, source_codepage, 0); r = select_charset (0, 0, source_codepage, FALSE);
if ( r > 0 ) if (r == SELECT_CHARSET_CANCEL)
source_codepage = r; return FALSE;
errmsg = init_translation_table (source_codepage, display_codepage); source_codepage = r;
if (errmsg) {
errmsg = init_translation_table (r == SELECT_CHARSET_NO_TRANSLATE ?
display_codepage : source_codepage,
display_codepage);
if (errmsg != NULL)
message (D_ERROR, MSG_ERROR, "%s", errmsg); message (D_ERROR, MSG_ERROR, "%s", errmsg);
return -1;
} return (errmsg == NULL);
return 0;
} }
#endif /* HAVE_CHARSET */ #endif /* HAVE_CHARSET */

View File

@ -7,8 +7,24 @@
#define MC_SELCODEPAGE_H #define MC_SELCODEPAGE_H
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
int select_charset (int delta_x, int delta_y, int current_charset, int seldisplay);
int do_select_codepage (void); #include "global.h"
int select_charset (int delta_x, int delta_y, int current_charset, gboolean seldisplay);
gboolean do_select_codepage (void);
/* some results of select_charset() */
#define SELECT_CHARSET_CANCEL -2
/* select_charset() returns this value if dialog has been cenceled */
#define SELECT_CHARSET_OTHER_8BIT -1
/* select_charset() returns this value if seldisplay == TRUE
* and the last item has been selected. Last item is "Other 8 bits" */
#define SELECT_CHARSET_NO_TRANSLATE -1
/* select_charset() returns this value if seldisplay == FALSE
* and the 1st item has been selected. 1st item is "No translation" */
/* In other cases select_charset() returns non-negative value
* which is number of codepage in codepage list */
#endif /* HAVE_CHARSET */ #endif /* HAVE_CHARSET */
#endif #endif /* MC_SELCODEPAGE_H */

View File

@ -3466,15 +3466,17 @@ static void view_cmk_moveto_bottom (void *w, int n) {
static void static void
view_select_encoding (WView *view) view_select_encoding (WView *view)
{ {
char *enc = NULL;
GIConv conv;
struct cache_line *line;
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
do_select_codepage (); char *enc = NULL;
enc = g_strdup( get_codepage_id ( source_codepage ) );
#endif if (!do_select_codepage ())
if ( enc ) { return;
enc = get_codepage_id (source_codepage);
if (enc != NULL) {
GIConv conv;
struct cache_line *line;
conv = str_crt_conv_from (enc); conv = str_crt_conv_from (enc);
if (conv != INVALID_CONV) { if (conv != INVALID_CONV) {
if (view->converter != str_cnv_from_term) if (view->converter != str_cnv_from_term)
@ -3484,9 +3486,8 @@ view_select_encoding (WView *view)
line = view_offset_to_line (view, view->dpy_start); line = view_offset_to_line (view, view->dpy_start);
view_set_first_showed (view, line); view_set_first_showed (view, line);
} }
g_free(enc);
} }
#endif
} }