hex-mode: some improvements

- added probe function for binary mode so it gets selected when cycling
  modes with next-mode (bound to M-m)
- renamed w->disp_width as dump_width and make it a window variable
- compute default dump-width more accurately for dired-mode
- dump-width can be controlled with C-left and C-right in
  hex, unihex and binary modes
This commit is contained in:
Charlie Gordon 2015-08-13 23:26:59 +00:00
commit 6c2bef2533
4 changed files with 40 additions and 30 deletions

43
hex.c
View file

@ -41,7 +41,7 @@ static int to_disp(int c)
static int hex_backward_offset(EditState *s, int offset)
{
return align(offset, s->disp_width);
return align(offset, s->dump_width);
}
static int hex_display(EditState *s, DisplayState *ds, int offset)
@ -57,14 +57,14 @@ static int hex_display(EditState *s, DisplayState *ds, int offset)
ateof = 0;
len = s->b->total_size - offset;
if (len > s->disp_width)
len = s->disp_width;
if (len > s->dump_width)
len = s->dump_width;
if (s->mode == &hex_mode) {
ds->style = HEX_STYLE_DUMP;
for (j = 0; j < s->disp_width; j++) {
for (j = 0; j < s->dump_width; j++) {
display_char(ds, -1, -1, ' ');
offset1 = offset + j;
offset2 = offset1 + 1;
@ -91,7 +91,7 @@ static int hex_display(EditState *s, DisplayState *ds, int offset)
display_char(ds, -1, -1, ' ');
ateof = 0;
for (j = 0; j < s->disp_width; j++) {
for (j = 0; j < s->dump_width; j++) {
offset1 = offset + j;
offset2 = offset1 + 1;
if (j < len) {
@ -108,7 +108,7 @@ static int hex_display(EditState *s, DisplayState *ds, int offset)
}
display_eol(ds, -1, -1);
if (len >= s->disp_width)
if (len >= s->dump_width)
return offset + len;
else
return -1;
@ -117,7 +117,7 @@ static int hex_display(EditState *s, DisplayState *ds, int offset)
static void do_set_width(EditState *s, int w)
{
if (w >= 1) {
s->disp_width = w;
s->dump_width = w;
s->offset_top = s->mode->text_backward_offset(s, s->offset_top);
}
}
@ -125,7 +125,7 @@ static void do_set_width(EditState *s, int w)
static void do_incr_width(EditState *s, int incr)
{
int w;
w = s->disp_width + incr;
w = s->dump_width + incr;
if (w >= 1)
do_set_width(s, w);
}
@ -166,11 +166,15 @@ static int binary_mode_init(EditState *s, EditBuffer *b, int flags)
num_width = glyph_width(s->screen, font, '0');
release_font(s->screen, font);
s->disp_width = (s->screen->width / num_width) - 10;
s->dump_width = s->screen->width / num_width;
if (s->b->flags & BF_PREVIEW)
s->dump_width = s->dump_width * 4 / 5;
s->dump_width -= 10;
/* align on 16 byte boundary */
s->disp_width &= ~15;
if (s->disp_width < 16)
s->disp_width = 16;
s->dump_width &= ~15;
if (s->dump_width < 16)
s->dump_width = 16;
s->insert = 0;
s->hex_mode = 0;
s->wrap = WRAP_TRUNCATE;
@ -181,7 +185,7 @@ static int binary_mode_init(EditState *s, EditBuffer *b, int flags)
static int hex_mode_init(EditState *s, EditBuffer *b, int flags)
{
if (s) {
s->disp_width = 16;
s->dump_width = 16;
s->hex_mode = 1;
s->unihex_mode = 0;
s->hex_nibble = 0;
@ -216,12 +220,12 @@ static int hex_mode_probe(ModeDef *mode, ModeProbeData *p)
static void hex_move_bol(EditState *s)
{
s->offset = align(s->offset, s->disp_width);
s->offset = align(s->offset, s->dump_width);
}
static void hex_move_eol(EditState *s)
{
s->offset = align(s->offset, s->disp_width) + s->disp_width - 1;
s->offset = align(s->offset, s->dump_width) + s->dump_width - 1;
if (s->offset > s->b->total_size)
s->offset = s->b->total_size;
}
@ -238,7 +242,7 @@ static void hex_move_left_right(EditState *s, int dir)
static void hex_move_up_down(EditState *s, int dir)
{
s->offset += dir * s->disp_width;
s->offset += dir * s->dump_width;
if (s->offset < 0)
s->offset = 0;
else
@ -318,9 +322,14 @@ static void hex_mode_line(EditState *s, buf_t *out)
buf_printf(out, "--%d%%", compute_percent(s->offset, s->b->total_size));
}
static int binary_mode_probe(ModeDef *mode, ModeProbeData *p)
{
return 5;
}
static ModeDef binary_mode = {
.name = "binary",
.mode_probe = NULL,
.mode_probe = binary_mode_probe,
.mode_init = binary_mode_init,
.text_display = hex_display,
.text_backward_offset = hex_backward_offset,

2
qe.h
View file

@ -1129,7 +1129,7 @@ struct EditState {
int y_disp; /* virtual position of the displayed text */
int x_disp[2]; /* position for LTR and RTL text resp. */
int minibuf; /* true if single line editing */
int disp_width; /* width in binary, hex and unihex modes */
int dump_width; /* width in binary, hex and unihex modes */
int hex_mode; /* true if we are currently editing hexa */
int unihex_mode; /* true if unihex editing (width of hex char dump) */
int hex_nibble; /* current hexa nibble */

View file

@ -44,7 +44,7 @@ static int unihex_mode_init(EditState *s, EditBuffer *b, int flags)
s->unihex_mode = snprintf(NULL, 0, "%x", maxc);
s->disp_width = 32 / s->unihex_mode;
s->dump_width = 32 / s->unihex_mode;
s->hex_mode = 1;
s->hex_nibble = 0;
s->insert = 0;
@ -69,13 +69,13 @@ static int unihex_backward_offset(EditState *s, int offset)
/* CG: beware: offset may fall inside a character */
pos = eb_get_char_offset(s->b, offset);
pos = align(pos, s->disp_width);
pos = align(pos, s->dump_width);
return eb_goto_char(s->b, pos);
}
static int unihex_display(EditState *s, DisplayState *ds, int offset)
{
int j, len, ateof, disp_width;
int j, len, ateof, dump_width;
int offset1, offset2;
unsigned int b;
/* CG: array size is incorrect, should be smaller */
@ -90,10 +90,10 @@ static int unihex_display(EditState *s, DisplayState *ds, int offset)
//display_printf(ds, -1, -1, "%08x ", charpos);
//display_printf(ds, -1, -1, "%08x %08x ", charpos, offset);
disp_width = min(LINE_MAX_SIZE - 1, s->disp_width);
dump_width = min(LINE_MAX_SIZE - 1, s->dump_width);
ateof = 0;
len = 0;
for (j = 0; j < disp_width && offset < s->b->total_size; j++) {
for (j = 0; j < dump_width && offset < s->b->total_size; j++) {
pos[len] = offset;
buf[len] = eb_nextc(s->b, offset, &offset);
len++;
@ -102,7 +102,7 @@ static int unihex_display(EditState *s, DisplayState *ds, int offset)
ds->style = UNIHEX_STYLE_DUMP;
for (j = 0; j < disp_width; j++) {
for (j = 0; j < dump_width; j++) {
display_char(ds, -1, -1, ' ');
offset1 = pos[j];
offset2 = pos[j + 1];
@ -132,7 +132,7 @@ static int unihex_display(EditState *s, DisplayState *ds, int offset)
display_char(ds, -1, -1, ' ');
ateof = 0;
for (j = 0; j < disp_width; j++) {
for (j = 0; j < dump_width; j++) {
offset1 = pos[j];
offset2 = pos[j + 1];
if (j < len) {
@ -156,7 +156,7 @@ static int unihex_display(EditState *s, DisplayState *ds, int offset)
}
display_eol(ds, -1, -1);
if (len >= disp_width)
if (len >= dump_width)
return offset;
else
return -1;
@ -167,7 +167,7 @@ static void unihex_move_bol(EditState *s)
int pos;
pos = eb_get_char_offset(s->b, s->offset);
pos = align(pos, s->disp_width);
pos = align(pos, s->dump_width);
s->offset = eb_goto_char(s->b, pos);
}
@ -178,7 +178,7 @@ static void unihex_move_eol(EditState *s)
pos = eb_get_char_offset(s->b, s->offset);
/* CG: should include the last character! */
pos = align(pos, s->disp_width) + s->disp_width - 1;
pos = align(pos, s->dump_width) + s->dump_width - 1;
s->offset = eb_goto_char(s->b, pos);
}
@ -198,7 +198,7 @@ static void unihex_move_up_down(EditState *s, int dir)
pos = eb_get_char_offset(s->b, s->offset);
pos += dir * s->disp_width;
pos += dir * s->dump_width;
s->offset = eb_goto_char(s->b, pos);
}

View file

@ -70,12 +70,13 @@ static VarDef var_table[] = {
W_VAR( "window-left", xleft, VAR_NUMBER, VAR_RW )
W_VAR( "window-top", ytop, VAR_NUMBER, VAR_RW )
W_VAR( "window-prompt", prompt, VAR_STRING, VAR_RW )
W_VAR( "dump-width", dump_width, VAR_NUMBER, VAR_RW )
M_VAR( "mode-name", name, VAR_STRING, VAR_RO )
M_VAR( "auto-indent", auto_indent, VAR_NUMBER, VAR_RW )
/* more buffer fields: modified, readonly, binary, charset */
/* more window fields: mode_line, disp_width, color, input_method...
/* more window fields: mode_line, color, input_method...
*/
//G_VAR( "text-mode-line", text_mode.mode_line, VAR_STRING, VAR_RW )