evil-qemacs/hex.c

371 lines
9.6 KiB
C
Raw Normal View History

2004-05-29 10:17:37 +00:00
/*
* Hexadecimal modes for QEmacs.
2007-12-03 16:10:38 +00:00
*
* Copyright (c) 2000-2001 Fabrice Bellard.
* Copyright (c) 2002-2014 Charlie Gordon.
2004-05-29 10:17:37 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2007-12-03 16:10:38 +00:00
2004-05-29 10:17:37 +00:00
#include "qe.h"
static int to_disp(int c)
{
#if 1
/* Allow characters in range 160-255 to show as graphics */
if ((c & 127) < ' ' || c == 127)
c = '.';
#else
2004-05-29 10:17:37 +00:00
if (c < ' ' || c >= 127)
c = '.';
#endif
2004-05-29 10:17:37 +00:00
return c;
}
static int hex_backward_offset(EditState *s, int offset)
{
return align(offset, s->disp_width);
}
static int hex_display(EditState *s, DisplayState *ds, int offset)
{
2008-04-05 18:16:30 +00:00
int j, len, ateof;
int offset1, offset2;
2004-05-29 10:17:37 +00:00
unsigned char b;
display_bol(ds);
ds->style = QE_STYLE_COMMENT;
2004-05-29 10:17:37 +00:00
display_printf(ds, -1, -1, "%08x ", offset);
2008-04-05 18:16:30 +00:00
ateof = 0;
2004-05-29 10:17:37 +00:00
len = s->b->total_size - offset;
if (len > s->disp_width)
len = s->disp_width;
2004-05-29 10:17:37 +00:00
if (s->mode == &hex_mode) {
ds->style = QE_STYLE_FUNCTION;
2005-05-11 15:12:14 +00:00
for (j = 0; j < s->disp_width; j++) {
2004-05-29 10:17:37 +00:00
display_char(ds, -1, -1, ' ');
offset1 = offset + j;
offset2 = offset1 + 1;
2004-05-29 10:17:37 +00:00
if (j < len) {
eb_read(s->b, offset1, &b, 1);
display_printhex(ds, offset1, offset2, b, 2);
2004-05-29 10:17:37 +00:00
} else {
2008-04-05 18:16:30 +00:00
if (!ateof) {
ateof = 1;
2004-05-29 10:17:37 +00:00
} else {
offset1 = offset2 = -1;
2004-05-29 10:17:37 +00:00
}
ds->cur_hex_mode = s->hex_mode;
display_printf(ds, offset1, offset2, " ");
ds->cur_hex_mode = 0;
2004-05-29 10:17:37 +00:00
}
if ((j & 7) == 7)
2004-05-29 10:17:37 +00:00
display_char(ds, -1, -1, ' ');
}
display_char(ds, -1, -1, ' ');
}
ds->style = 0;
display_char(ds, -1, -1, ' ');
2008-04-05 18:16:30 +00:00
ateof = 0;
2005-05-11 15:12:14 +00:00
for (j = 0; j < s->disp_width; j++) {
2004-05-29 10:17:37 +00:00
offset1 = offset + j;
offset2 = offset1 + 1;
2004-05-29 10:17:37 +00:00
if (j < len) {
eb_read(s->b, offset1, &b, 1);
2004-05-29 10:17:37 +00:00
} else {
b = ' ';
2008-04-05 18:16:30 +00:00
if (!ateof) {
ateof = 1;
2004-05-29 10:17:37 +00:00
} else {
offset1 = offset2 = -1;
2004-05-29 10:17:37 +00:00
}
}
display_char(ds, offset1, offset2, to_disp(b));
2004-05-29 10:17:37 +00:00
}
display_eol(ds, -1, -1);
if (len >= s->disp_width)
return offset + len;
2004-05-29 10:17:37 +00:00
else
return -1;
}
static void do_set_width(EditState *s, int w)
2004-05-29 10:17:37 +00:00
{
if (w >= 1) {
s->disp_width = w;
s->offset_top = s->mode->text_backward_offset(s, s->offset_top);
}
}
static void do_incr_width(EditState *s, int incr)
2004-05-29 10:17:37 +00:00
{
int w;
w = s->disp_width + incr;
if (w >= 1)
do_set_width(s, w);
}
static void do_toggle_hex(EditState *s)
2004-05-29 10:17:37 +00:00
{
s->hex_mode = !s->hex_mode;
}
/* specific hex commands */
static CmdDef hex_commands[] = {
CMD1( KEY_CTRL_LEFT, KEY_NONE,
"decrease-width", do_incr_width, -1)
CMD1( KEY_CTRL_RIGHT, KEY_NONE,
"increase-width", do_incr_width, 1)
CMD2( KEY_NONE, KEY_NONE,
"set-width", do_set_width, ESi,
"ui{Width: }")
CMD3( KEY_META('g'), KEY_NONE,
"goto-byte", do_goto, ESsi, 'b',
"us{Goto byte: }"
"v")
CMD0( KEY_NONE, KEY_NONE,
"toggle-hex", do_toggle_hex)
2004-05-29 10:17:37 +00:00
CMD_DEF_END,
};
static int binary_mode_init(EditState *s, ModeSavedData *saved_data)
2004-05-29 10:17:37 +00:00
{
QEFont *font;
QEStyleDef style;
int num_width;
text_mode_init(s, saved_data);
2004-05-29 10:17:37 +00:00
/* get typical number width */
get_style(s, &style, s->default_style);
font = select_font(s->screen, style.font_style, style.font_size);
num_width = glyph_width(s->screen, font, '0');
release_font(s->screen, font);
2004-05-29 10:17:37 +00:00
s->disp_width = (s->screen->width / num_width) - 10;
/* align on 16 byte boundary */
s->disp_width &= ~15;
if (s->disp_width < 16)
s->disp_width = 16;
2008-04-22 09:15:00 +00:00
s->insert = 0;
2004-05-29 10:17:37 +00:00
s->hex_mode = 0;
s->wrap = WRAP_TRUNCATE;
return 0;
}
static int hex_mode_init(EditState *s, ModeSavedData *saved_data)
{
text_mode_init(s, saved_data);
2004-05-29 10:17:37 +00:00
s->disp_width = 16;
s->hex_mode = 1;
s->unihex_mode = 0;
s->hex_nibble = 0;
s->insert = 0;
2004-05-29 10:17:37 +00:00
s->wrap = WRAP_TRUNCATE;
return 0;
}
static int detect_binary(const unsigned char *buf, int size)
2004-05-29 10:17:37 +00:00
{
static const uint32_t magic = (1 << '\b') | (1 << '\t') | (1 << '\f') |
(1 << '\n') | (1 << '\r') | (1 << '\033') |
(1 << 0x0e) | (1 << 0x0f) | (1 << 0x1f);
2004-05-29 10:17:37 +00:00
int i, c;
for (i = 0; i < size; i++) {
c = buf[i];
if (c < 32 && !(magic & (1 << c)))
2005-05-11 15:12:14 +00:00
return 1;
2004-05-29 10:17:37 +00:00
}
return 0;
}
static int hex_mode_probe(ModeDef *mode, ModeProbeData *p)
2004-05-29 10:17:37 +00:00
{
if (detect_binary(p->buf, p->buf_size))
return 50;
else
return 10;
2004-05-29 10:17:37 +00:00
}
static void hex_move_bol(EditState *s)
2004-05-29 10:17:37 +00:00
{
s->offset = align(s->offset, s->disp_width);
}
static void hex_move_eol(EditState *s)
2004-05-29 10:17:37 +00:00
{
s->offset = align(s->offset, s->disp_width) + s->disp_width - 1;
if (s->offset > s->b->total_size)
2004-05-29 10:17:37 +00:00
s->offset = s->b->total_size;
}
static void hex_move_left_right(EditState *s, int dir)
2004-05-29 10:17:37 +00:00
{
s->offset += dir;
if (s->offset < 0)
s->offset = 0;
else
if (s->offset > s->b->total_size)
2004-05-29 10:17:37 +00:00
s->offset = s->b->total_size;
}
static void hex_move_up_down(EditState *s, int dir)
2004-05-29 10:17:37 +00:00
{
s->offset += dir * s->disp_width;
if (s->offset < 0)
s->offset = 0;
else
if (s->offset > s->b->total_size)
2004-05-29 10:17:37 +00:00
s->offset = s->b->total_size;
}
void hex_write_char(EditState *s, int key)
{
unsigned int cur_ch, ch;
int hsize, shift, cur_len, len, h;
2014-01-31 14:50:56 +00:00
int offset = s->offset;
2004-05-29 10:17:37 +00:00
char buf[10];
2004-05-29 10:17:37 +00:00
if (s->hex_mode) {
if (s->unihex_mode)
hsize = s->unihex_mode;
2004-05-29 10:17:37 +00:00
else
hsize = 2;
h = to_hex(key);
if (h < 0)
return;
2014-01-31 14:50:56 +00:00
if ((s->insert || offset >= s->b->total_size) && s->hex_nibble == 0) {
2004-05-29 10:17:37 +00:00
ch = h << ((hsize - 1) * 4);
if (s->unihex_mode || s->b->charset->char_size > 1) {
2004-05-29 10:17:37 +00:00
len = unicode_to_charset(buf, ch, s->b->charset);
} else {
len = 1;
buf[0] = ch;
}
2014-01-31 14:50:56 +00:00
eb_insert(s->b, offset, buf, len);
2004-05-29 10:17:37 +00:00
} else {
if (s->unihex_mode) {
2014-01-31 14:50:56 +00:00
cur_ch = eb_nextc(s->b, offset, &cur_len);
cur_len -= offset;
2004-05-29 10:17:37 +00:00
} else {
2014-01-31 14:50:56 +00:00
eb_read(s->b, offset, buf, 1);
2004-05-29 10:17:37 +00:00
cur_ch = buf[0];
cur_len = 1;
}
shift = (hsize - s->hex_nibble - 1) * 4;
ch = (cur_ch & ~(0xf << shift)) | (h << shift);
if (s->unihex_mode) {
len = unicode_to_charset(buf, ch, s->b->charset);
} else {
len = 1;
buf[0] = ch;
}
2007-12-26 11:09:34 +00:00
#if 1
2014-01-31 14:50:56 +00:00
eb_replace(s->b, offset, cur_len, buf, len);
2007-12-26 11:09:34 +00:00
#else
2004-05-29 10:17:37 +00:00
if (cur_len == len) {
2014-01-31 14:50:56 +00:00
eb_write(s->b, offset, buf, len);
2004-05-29 10:17:37 +00:00
} else {
2014-01-31 14:50:56 +00:00
eb_delete(s->b, offset, cur_len);
eb_insert(s->b, offset, buf, len);
2004-05-29 10:17:37 +00:00
}
2007-12-26 11:09:34 +00:00
#endif
2004-05-29 10:17:37 +00:00
}
2014-01-31 14:50:56 +00:00
s->offset = offset;
2004-05-29 10:17:37 +00:00
if (++s->hex_nibble == hsize) {
s->hex_nibble = 0;
2014-01-31 14:50:56 +00:00
if (offset < s->b->total_size)
2004-05-29 10:17:37 +00:00
s->offset += len;
}
} else {
text_write_char(s, key);
}
}
static void hex_mode_line(EditState *s, buf_t *out)
2004-05-29 10:17:37 +00:00
{
basic_mode_line(s, out, '-');
buf_printf(out, "0x%x--0x%x", s->offset, s->b->total_size);
buf_printf(out, "--%d%%", compute_percent(s->offset, s->b->total_size));
2004-05-29 10:17:37 +00:00
}
static ModeDef binary_mode = {
.name = "binary",
.instance_size = 0,
.mode_probe = NULL,
.mode_init = binary_mode_init,
.mode_close = text_mode_close,
.text_display = hex_display,
.text_backward_offset = hex_backward_offset,
2007-12-20 20:39:18 +00:00
.move_up_down = hex_move_up_down,
.move_left_right = hex_move_left_right,
.move_bol = hex_move_bol,
.move_eol = hex_move_eol,
.scroll_up_down = text_scroll_up_down,
.write_char = text_write_char,
.mouse_goto = text_mouse_goto,
.get_mode_line = hex_mode_line,
2004-05-29 10:17:37 +00:00
};
ModeDef hex_mode = {
.name = "hex",
.instance_size = 0,
.mode_probe = hex_mode_probe,
.mode_init = hex_mode_init,
.mode_close = text_mode_close,
.text_display = hex_display,
.text_backward_offset = hex_backward_offset,
2007-12-20 20:39:18 +00:00
.move_up_down = hex_move_up_down,
.move_left_right = hex_move_left_right,
.move_bol = hex_move_bol,
.move_eol = hex_move_eol,
.scroll_up_down = text_scroll_up_down,
.write_char = hex_write_char,
.mouse_goto = text_mouse_goto,
.get_mode_line = hex_mode_line,
2004-05-29 10:17:37 +00:00
};
static int hex_init(void)
{
/* first register mode(s) */
qe_register_mode(&binary_mode);
2004-05-29 10:17:37 +00:00
qe_register_mode(&hex_mode);
/* commands and default keys */
qe_register_cmd_table(hex_commands, &hex_mode);
qe_register_cmd_table(hex_commands, &binary_mode);
2004-05-29 10:17:37 +00:00
2006-12-09 19:02:07 +00:00
/* additional mode specific keys */
qe_register_binding(KEY_TAB, "toggle-hex", &hex_mode);
qe_register_binding(KEY_SHIFT_TAB, "toggle-hex", &hex_mode);
2004-05-29 10:17:37 +00:00
return 0;
}
qe_module_init(hex_init);