1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* Hexadecimal modes for QEmacs.
* Copyright (c) 2000, 2001 Fabrice Bellard.
*
* 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
*/
#include "qe.h"
extern ModeDef hex_mode;
static int to_disp(int c)
{
if (c < ' ' || c >= 127)
c = '.';
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)
{
int j, len, eof;
int offset1;
unsigned char b;
display_bol(ds);
display_printf(ds, -1, -1, "%08x ", offset);
eof = 0;
len = s->b->total_size - offset;
if (len > s->disp_width)
len = s->disp_width;
if (s->mode == &hex_mode) {
for(j=0;j<s->disp_width;j++) {
display_char(ds, -1, -1, ' ');
offset1 = offset + j;
if (j < len) {
eb_read(s->b, offset + j, &b, 1);
display_printhex(ds, offset1, offset1 + 1, b, 2);
} else {
if (!eof) {
eof = 1;
} else {
offset1 = -2;
}
display_printf(ds, offset1, offset1 + 1, " ");
}
if ((j & 7)== 7)
display_char(ds, -1, -1, ' ');
}
display_char(ds, -1, -1, ' ');
display_char(ds, -1, -1, ' ');
}
eof = 0;
for(j=0;j<s->disp_width;j++) {
offset1 = offset + j;
if (j < len) {
eb_read(s->b, offset + j, &b, 1);
} else {
b = ' ';
if (!eof) {
eof = 1;
} else {
offset1 = -2;
}
}
display_char(ds, offset1, offset1 + 1, to_disp(b));
}
offset += len;
display_eol(ds, -1, -1);
if (len >= s->disp_width)
return offset;
else
return -1;
}
void do_goto_byte(EditState *s, int offset)
{
if (offset < 0 || offset >= s->b->total_size)
return;
s->offset = offset;
}
void do_set_width(EditState *s, int w)
{
if (w >= 1) {
s->disp_width = w;
s->offset_top = s->mode->text_backward_offset(s, s->offset_top);
}
}
void do_incr_width(EditState *s, int incr)
{
int w;
w = s->disp_width + incr;
if (w >= 1)
do_set_width(s, w);
}
void do_toggle_hex(EditState *s)
{
s->hex_mode = !s->hex_mode;
}
/* specific hex commands */
static CmdDef hex_commands[] = {
CMD1( KEY_NONE, KEY_NONE, "decrease-width", do_incr_width, -1)
CMD1( KEY_NONE, KEY_NONE, "increase-width", do_incr_width, 1)
CMD( KEY_NONE, KEY_NONE, "set-width\0i{Width: }", do_set_width)
CMD( KEY_NONE, KEY_NONE, "goto-byte\0i{Goto byte: }", do_goto_byte)
CMD0( KEY_NONE, KEY_NONE, "toggle-hex", do_toggle_hex)
CMD_DEF_END,
};
static int ascii_mode_init(EditState *s, ModeSavedData *saved_data)
{
QEFont *font;
QEStyleDef style;
int num_width;
int ret;
ret = text_mode_init(s, saved_data);
if (ret)
return ret;
/* 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');
s->disp_width = (s->screen->width / num_width) - 10;
s->hex_mode = 0;
s->wrap = WRAP_TRUNCATE;
return 0;
}
static int hex_mode_init(EditState *s, ModeSavedData *saved_data)
{
int ret;
ret = text_mode_init(s, saved_data);
if (ret)
return ret;
s->disp_width = 16;
s->hex_mode = 1;
s->unihex_mode = 0;
s->hex_nibble = 0;
s->wrap = WRAP_TRUNCATE;
return 0;
}
int detect_binary(const unsigned char *buf, int size)
{
int i, c;
for (i = 0; i < size; i++) {
c = buf[i];
if (c < 32 &&
(c != '\r' && c != '\n' && c != '\t' && c != '\e' && c!= '\b'))
return 1;
}
return 0;
}
static int hex_mode_probe(ModeProbeData *p)
{
if (detect_binary(p->buf, p->buf_size))
return 50;
else
return 0;
}
void hex_move_bol(EditState *s)
{
s->offset = align(s->offset, s->disp_width);
}
void hex_move_eol(EditState *s)
{
s->offset = align(s->offset, s->disp_width) + s->disp_width - 1;
if (s->offset >= s->b->total_size)
s->offset = s->b->total_size;
}
void hex_move_left_right(EditState *s, int dir)
{
s->offset += dir;
if (s->offset < 0)
s->offset = 0;
else if (s->offset > s->b->total_size)
s->offset = s->b->total_size;
}
void hex_move_up_down(EditState *s, int dir)
{
s->offset += dir * s->disp_width;
if (s->offset < 0)
s->offset = 0;
else if (s->offset > s->b->total_size)
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;
char buf[10];
if (s->hex_mode) {
if (s->unihex_mode)
hsize = 4;
else
hsize = 2;
h = to_hex(key);
if (h < 0)
return;
if (s->insert && s->hex_nibble == 0) {
ch = h << ((hsize - 1) * 4);
if (s->unihex_mode) {
len = unicode_to_charset(buf, ch, s->b->charset);
} else {
len = 1;
buf[0] = ch;
}
eb_insert(s->b, s->offset, buf, len);
} else {
if (s->unihex_mode) {
cur_ch = eb_nextc(s->b, s->offset, &cur_len);
cur_len -= s->offset;
} else {
eb_read(s->b, s->offset, buf, 1);
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;
}
if (cur_len == len) {
eb_write(s->b, s->offset, buf, len);
} else {
eb_delete(s->b, s->offset, cur_len);
eb_insert(s->b, s->offset, buf, len);
}
}
if (++s->hex_nibble == hsize) {
s->hex_nibble = 0;
if (s->offset < s->b->total_size)
s->offset += len;
}
} else {
text_write_char(s, key);
}
}
void hex_mode_line(EditState *s, char *buf, int buf_size)
{
char *q;
int percent;
basic_mode_line(s, buf, buf_size, '-');
q = buf + strlen(buf);
q += sprintf(q, "0x%x--0x%x",
s->offset, s->b->total_size);
percent = 0;
if (s->b->total_size > 0)
percent = (s->offset * 100) / s->b->total_size;
q += sprintf(q, "--%d%%", percent);
}
ModeDef ascii_mode = {
"ascii",
instance_size: 0,
mode_probe: NULL,
mode_init: ascii_mode_init,
mode_close: text_mode_close,
text_display: hex_display,
text_backward_offset: hex_backward_offset,
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,
mode_line: hex_mode_line,
};
ModeDef hex_mode = {
"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,
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,
mode_line: hex_mode_line,
};
static int hex_init(void)
{
/* first register mode(s) */
qe_register_mode(&ascii_mode);
qe_register_mode(&hex_mode);
/* commands and default keys */
qe_register_cmd_table(hex_commands, NULL);
/* additionnal mode specific keys */
qe_register_binding(KEY_CTRL_LEFT, "decrease-width", "ascii|hex");
qe_register_binding(KEY_CTRL_RIGHT, "increase-width", "ascii|hex");
qe_register_binding(KEY_TAB, "toggle-hex", "hex");
qe_register_binding(KEY_SHIFT_TAB, "toggle-hex", "hex");
return 0;
}
qe_module_init(hex_init);
|