]> code.delx.au - gnu-emacs/blob - lisp/hexl.el
(time-stamp-old-format-warn): Fix a tag string.
[gnu-emacs] / lisp / hexl.el
1 ;;; hexl.el --- edit a file in a hex dump format using the hexl filter.
2
3 ;; Copyright (C) 1989, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Keith Gabryelski <ag@wheaties.ai.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: data
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package implements a major mode for editing binary files. It uses
29 ;; a program called hexl, supplied with the GNU Emacs distribution, that
30 ;; can filter a binary into an editable format or from the format back into
31 ;; binary. For full instructions, invoke `hexl-mode' on an empty buffer and
32 ;; do `M-x describe-mode'.
33 ;;
34 ;; This may be useful in your .emacs:
35 ;;
36 ;; (autoload 'hexl-find-file "hexl"
37 ;; "Edit file FILENAME in hexl-mode." t)
38 ;;
39 ;; (define-key global-map "\C-c\C-h" 'hexl-find-file)
40 ;;
41 ;; NOTE: Remember to change HEXL-PROGRAM or HEXL-OPTIONS if needed.
42 ;;
43 ;; Currently hexl only supports big endian hex output with 16 bit
44 ;; grouping.
45 ;;
46 ;; -iso in `hexl-options' will allow iso characters to display in the
47 ;; ASCII region of the screen (if your emacs supports this) instead of
48 ;; changing them to dots.
49
50 ;;; Code:
51
52 ;;
53 ;; vars here
54 ;;
55
56 (defgroup hexl nil
57 "Edit a file in a hex dump format using the hexl filter."
58 :group 'data)
59
60
61 (defcustom hexl-program "hexl"
62 "The program that will hexlify and dehexlify its stdin.
63 `hexl-program' will always be concatenated with `hexl-options'
64 and \"-de\" when dehexlifying a buffer."
65 :type 'string
66 :group 'hexl)
67
68 (defcustom hexl-iso ""
69 "If your emacs can handle ISO characters, this should be set to
70 \"-iso\" otherwise it should be \"\"."
71 :type 'string
72 :group 'hexl)
73
74 (defcustom hexl-options (format "-hex %s" hexl-iso)
75 "Options to hexl-program that suit your needs."
76 :type 'string
77 :group 'hexl)
78
79 (defcustom hexlify-command
80 (format "%s%s %s" exec-directory hexl-program hexl-options)
81 "The command to use to hexlify a buffer."
82 :type 'string
83 :group 'hexl)
84
85 (defcustom dehexlify-command
86 (format "%s%s -de %s" exec-directory hexl-program hexl-options)
87 "The command to use to unhexlify a buffer."
88 :type 'string
89 :group 'hexl)
90
91 (defvar hexl-max-address 0
92 "Maximum offset into hexl buffer.")
93
94 (defvar hexl-mode-map nil)
95
96 (defvar hexl-mode-old-local-map)
97 (defvar hexl-mode-old-mode-name)
98 (defvar hexl-mode-old-major-mode)
99 (defvar hexl-mode-old-write-contents-hooks)
100 (defvar hexl-mode-old-require-final-newline)
101 (defvar hexl-mode-old-syntax-table)
102
103 ;; routines
104
105 ;;;###autoload
106 (defun hexl-mode (&optional arg)
107 "\\<hexl-mode-map>
108 A major mode for editing binary files in hex dump format.
109
110 This function automatically converts a buffer into the hexl format
111 using the function `hexlify-buffer'.
112
113 Each line in the buffer has an \"address\" (displayed in hexadecimal)
114 representing the offset into the file that the characters on this line
115 are at and 16 characters from the file (displayed as hexadecimal
116 values grouped every 16 bits) and as their ASCII values.
117
118 If any of the characters (displayed as ASCII characters) are
119 unprintable (control or meta characters) they will be replaced as
120 periods.
121
122 If `hexl-mode' is invoked with an argument the buffer is assumed to be
123 in hexl format.
124
125 A sample format:
126
127 HEX ADDR: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ASCII-TEXT
128 -------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------
129 00000000: 5468 6973 2069 7320 6865 786c 2d6d 6f64 This is hexl-mod
130 00000010: 652e 2020 4561 6368 206c 696e 6520 7265 e. Each line re
131 00000020: 7072 6573 656e 7473 2031 3620 6279 7465 presents 16 byte
132 00000030: 7320 6173 2068 6578 6164 6563 696d 616c s as hexadecimal
133 00000040: 2041 5343 4949 0a61 6e64 2070 7269 6e74 ASCII.and print
134 00000050: 6162 6c65 2041 5343 4949 2063 6861 7261 able ASCII chara
135 00000060: 6374 6572 732e 2020 416e 7920 636f 6e74 cters. Any cont
136 00000070: 726f 6c20 6f72 206e 6f6e 2d41 5343 4949 rol or non-ASCII
137 00000080: 2063 6861 7261 6374 6572 730a 6172 6520 characters.are
138 00000090: 6469 7370 6c61 7965 6420 6173 2070 6572 displayed as per
139 000000a0: 696f 6473 2069 6e20 7468 6520 7072 696e iods in the prin
140 000000b0: 7461 626c 6520 6368 6172 6163 7465 7220 table character
141 000000c0: 7265 6769 6f6e 2e0a region..
142
143 Movement is as simple as movement in a normal emacs text buffer. Most
144 cursor movement bindings are the same (ie. Use \\[hexl-backward-char], \\[hexl-forward-char], \\[hexl-next-line], and \\[hexl-previous-line]
145 to move the cursor left, right, down, and up).
146
147 Advanced cursor movement commands (ala \\[hexl-beginning-of-line], \\[hexl-end-of-line], \\[hexl-beginning-of-buffer], and \\[hexl-end-of-buffer]) are
148 also supported.
149
150 There are several ways to change text in hexl mode:
151
152 ASCII characters (character between space (0x20) and tilde (0x7E)) are
153 bound to self-insert so you can simply type the character and it will
154 insert itself (actually overstrike) into the buffer.
155
156 \\[hexl-quoted-insert] followed by another keystroke allows you to insert the key even if
157 it isn't bound to self-insert. An octal number can be supplied in place
158 of another key to insert the octal number's ASCII representation.
159
160 \\[hexl-insert-hex-char] will insert a given hexadecimal value (if it is between 0 and 0xFF)
161 into the buffer at the current point.
162
163 \\[hexl-insert-octal-char] will insert a given octal value (if it is between 0 and 0377)
164 into the buffer at the current point.
165
166 \\[hexl-insert-decimal-char] will insert a given decimal value (if it is between 0 and 255)
167 into the buffer at the current point.
168
169 \\[hexl-mode-exit] will exit hexl-mode.
170
171 Note: saving the file with any of the usual Emacs commands
172 will actually convert it back to binary format while saving.
173
174 You can use \\[hexl-find-file] to visit a file in hexl-mode.
175
176 \\[describe-bindings] for advanced commands."
177 (interactive "p")
178 (if (eq major-mode 'hexl-mode)
179 (error "You are already in hexl mode")
180
181 (let ((modified (buffer-modified-p))
182 (inhibit-read-only t)
183 (original-point (1- (point)))
184 max-address)
185 (and (eobp) (not (bobp))
186 (setq original-point (1- original-point)))
187 (if (not (or (eq arg 1) (not arg)))
188 ;; if no argument then we guess at hexl-max-address
189 (setq max-address (+ (* (/ (1- (buffer-size)) 68) 16) 15))
190 (setq max-address (1- (buffer-size)))
191 (hexlify-buffer)
192 (set-buffer-modified-p modified))
193 (make-local-variable 'hexl-max-address)
194 (setq hexl-max-address max-address)
195 (hexl-goto-address original-point))
196
197 ;; We do not turn off the old major mode; instead we just
198 ;; override most of it. That way, we can restore it perfectly.
199 (make-local-variable 'hexl-mode-old-local-map)
200 (setq hexl-mode-old-local-map (current-local-map))
201 (use-local-map hexl-mode-map)
202
203 (make-local-variable 'hexl-mode-old-mode-name)
204 (setq hexl-mode-old-mode-name mode-name)
205 (setq mode-name "Hexl")
206
207 (make-local-variable 'hexl-mode-old-major-mode)
208 (setq hexl-mode-old-major-mode major-mode)
209 (setq major-mode 'hexl-mode)
210
211 (make-local-variable 'hexl-mode-old-syntax-table)
212 (setq hexl-mode-old-syntax-table (syntax-table))
213 (set-syntax-table (standard-syntax-table))
214
215 (make-local-variable 'hexl-mode-old-write-contents-hooks)
216 (setq hexl-mode-old-write-contents-hooks write-contents-hooks)
217 (make-local-variable 'write-contents-hooks)
218 (add-hook 'write-contents-hooks 'hexl-save-buffer)
219
220 (make-local-variable 'hexl-mode-old-require-final-newline)
221 (setq hexl-mode-old-require-final-newline require-final-newline)
222 (make-local-variable 'require-final-newline)
223 (setq require-final-newline nil)
224
225 ;; Add hooks to rehexlify or dehexlify on various events.
226 (make-local-hook 'after-revert-hook)
227 (add-hook 'after-revert-hook 'hexl-after-revert-hook nil t)
228
229 (make-local-hook 'change-major-mode-hook)
230 (add-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer nil t))
231 (run-hooks 'hexl-mode-hook))
232
233 (defun hexl-after-revert-hook ()
234 (hexlify-buffer)
235 (set-buffer-modified-p nil))
236
237 (defvar hexl-in-save-buffer nil)
238
239 (defun hexl-save-buffer ()
240 "Save a hexl format buffer as binary in visited file if modified."
241 (interactive)
242 (if hexl-in-save-buffer nil
243 (set-buffer-modified-p (if (buffer-modified-p)
244 (save-excursion
245 (let ((buf (generate-new-buffer " hexl"))
246 (name (buffer-name))
247 (file-name (buffer-file-name))
248 (start (point-min))
249 (end (point-max))
250 modified)
251 (set-buffer buf)
252 (insert-buffer-substring name start end)
253 (set-buffer name)
254 (dehexlify-buffer)
255 ;; Prevent infinite recursion.
256 (let ((hexl-in-save-buffer t)
257 (buffer-file-type t)) ; for ms-dos
258 (save-buffer))
259 (setq modified (buffer-modified-p))
260 (delete-region (point-min) (point-max))
261 (insert-buffer-substring buf start end)
262 (kill-buffer buf)
263 modified))
264 (message "(No changes need to be saved)")
265 nil))
266 ;; Return t to indicate we have saved t
267 t))
268
269 ;;;###autoload
270 (defun hexl-find-file (filename)
271 "Edit file FILENAME in hexl-mode.
272 Switch to a buffer visiting file FILENAME, creating one in none exists."
273 (interactive "fFilename: ")
274 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
275 (find-file-binary filename)
276 (find-file filename))
277 (if (not (eq major-mode 'hexl-mode))
278 (hexl-mode)))
279
280 (defun hexl-mode-exit (&optional arg)
281 "Exit Hexl mode, returning to previous mode.
282 With arg, don't unhexlify buffer."
283 (interactive "p")
284 (if (or (eq arg 1) (not arg))
285 (let ((modified (buffer-modified-p))
286 (inhibit-read-only t)
287 (original-point (1+ (hexl-current-address))))
288 (dehexlify-buffer)
289 (remove-hook 'write-contents-hooks 'hexl-save-buffer)
290 (set-buffer-modified-p modified)
291 (goto-char original-point)))
292
293 (remove-hook 'after-revert-hook 'hexl-after-revert-hook t)
294 (remove-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer t)
295
296 (setq write-contents-hooks hexl-mode-old-write-contents-hooks)
297 (setq require-final-newline hexl-mode-old-require-final-newline)
298 (setq mode-name hexl-mode-old-mode-name)
299 (use-local-map hexl-mode-old-local-map)
300 (set-syntax-table hexl-mode-old-syntax-table)
301 (setq major-mode hexl-mode-old-major-mode)
302 (force-mode-line-update))
303
304 (defun hexl-maybe-dehexlify-buffer ()
305 "Convert a hexl format buffer to binary.
306 Ask the user for confirmation."
307 (if (y-or-n-p "Convert contents back to binary format? ")
308 (let ((modified (buffer-modified-p))
309 (inhibit-read-only t)
310 (original-point (1+ (hexl-current-address))))
311 (dehexlify-buffer)
312 (remove-hook 'write-contents-hooks 'hexl-save-buffer)
313 (set-buffer-modified-p modified)
314 (goto-char original-point))))
315
316 (defun hexl-current-address (&optional validate)
317 "Return current hexl-address."
318 (interactive)
319 (let ((current-column (- (% (point) 68) 11))
320 (hexl-address 0))
321 (if (< current-column 0)
322 (if validate
323 (error "Point is not on a character in the file")
324 (setq current-column 0)))
325 (setq hexl-address
326 (+ (* (/ (point) 68) 16)
327 (if (>= current-column 41)
328 (- current-column 41)
329 (/ (- current-column (/ current-column 5)) 2))))
330 hexl-address))
331
332 (defun hexl-address-to-marker (address)
333 "Return marker for ADDRESS."
334 (interactive "nAddress: ")
335 (+ (* (/ address 16) 68) 11 (/ (* (% address 16) 5) 2)))
336
337 (defun hexl-goto-address (address)
338 "Goto hexl-mode (decimal) address ADDRESS.
339 Signal error if ADDRESS out of range."
340 (interactive "nAddress: ")
341 (if (or (< address 0) (> address hexl-max-address))
342 (error "Out of hexl region."))
343 (goto-char (hexl-address-to-marker address)))
344
345 (defun hexl-goto-hex-address (hex-address)
346 "Go to hexl-mode address (hex string) HEX-ADDRESS.
347 Signal error if HEX-ADDRESS is out of range."
348 (interactive "sHex Address: ")
349 (hexl-goto-address (hexl-hex-string-to-integer hex-address)))
350
351 (defun hexl-hex-string-to-integer (hex-string)
352 "Return decimal integer for HEX-STRING."
353 (interactive "sHex number: ")
354 (let ((hex-num 0))
355 (while (not (equal hex-string ""))
356 (setq hex-num (+ (* hex-num 16)
357 (hexl-hex-char-to-integer (string-to-char hex-string))))
358 (setq hex-string (substring hex-string 1)))
359 hex-num))
360
361 (defun hexl-octal-string-to-integer (octal-string)
362 "Return decimal integer for OCTAL-STRING."
363 (interactive "sOctal number: ")
364 (let ((oct-num 0))
365 (while (not (equal octal-string ""))
366 (setq oct-num (+ (* oct-num 8)
367 (hexl-oct-char-to-integer
368 (string-to-char octal-string))))
369 (setq octal-string (substring octal-string 1)))
370 oct-num))
371
372 ;; move point functions
373
374 (defun hexl-backward-char (arg)
375 "Move to left ARG bytes (right if ARG negative) in hexl-mode."
376 (interactive "p")
377 (hexl-goto-address (- (hexl-current-address) arg)))
378
379 (defun hexl-forward-char (arg)
380 "Move right ARG bytes (left if ARG negative) in hexl-mode."
381 (interactive "p")
382 (hexl-goto-address (+ (hexl-current-address) arg)))
383
384 (defun hexl-backward-short (arg)
385 "Move to left ARG shorts (right if ARG negative) in hexl-mode."
386 (interactive "p")
387 (hexl-goto-address (let ((address (hexl-current-address)))
388 (if (< arg 0)
389 (progn
390 (setq arg (- arg))
391 (while (> arg 0)
392 (if (not (equal address (logior address 3)))
393 (if (> address hexl-max-address)
394 (progn
395 (message "End of buffer.")
396 (setq address hexl-max-address))
397 (setq address (logior address 3)))
398 (if (> address hexl-max-address)
399 (progn
400 (message "End of buffer.")
401 (setq address hexl-max-address))
402 (setq address (+ address 4))))
403 (setq arg (1- arg)))
404 (if (> address hexl-max-address)
405 (progn
406 (message "End of buffer.")
407 (setq address hexl-max-address))
408 (setq address (logior address 3))))
409 (while (> arg 0)
410 (if (not (equal address (logand address -4)))
411 (setq address (logand address -4))
412 (if (not (equal address 0))
413 (setq address (- address 4))
414 (message "Beginning of buffer.")))
415 (setq arg (1- arg))))
416 address)))
417
418 (defun hexl-forward-short (arg)
419 "Move right ARG shorts (left if ARG negative) in hexl-mode."
420 (interactive "p")
421 (hexl-backward-short (- arg)))
422
423 (defun hexl-backward-word (arg)
424 "Move to left ARG words (right if ARG negative) in hexl-mode."
425 (interactive "p")
426 (hexl-goto-address (let ((address (hexl-current-address)))
427 (if (< arg 0)
428 (progn
429 (setq arg (- arg))
430 (while (> arg 0)
431 (if (not (equal address (logior address 7)))
432 (if (> address hexl-max-address)
433 (progn
434 (message "End of buffer.")
435 (setq address hexl-max-address))
436 (setq address (logior address 7)))
437 (if (> address hexl-max-address)
438 (progn
439 (message "End of buffer.")
440 (setq address hexl-max-address))
441 (setq address (+ address 8))))
442 (setq arg (1- arg)))
443 (if (> address hexl-max-address)
444 (progn
445 (message "End of buffer.")
446 (setq address hexl-max-address))
447 (setq address (logior address 7))))
448 (while (> arg 0)
449 (if (not (equal address (logand address -8)))
450 (setq address (logand address -8))
451 (if (not (equal address 0))
452 (setq address (- address 8))
453 (message "Beginning of buffer.")))
454 (setq arg (1- arg))))
455 address)))
456
457 (defun hexl-forward-word (arg)
458 "Move right ARG words (left if ARG negative) in hexl-mode."
459 (interactive "p")
460 (hexl-backward-word (- arg)))
461
462 (defun hexl-previous-line (arg)
463 "Move vertically up ARG lines [16 bytes] (down if ARG negative) in hexl-mode.
464 If there is byte at the target address move to the last byte in that line."
465 (interactive "p")
466 (hexl-next-line (- arg)))
467
468 (defun hexl-next-line (arg)
469 "Move vertically down ARG lines [16 bytes] (up if ARG negative) in hexl-mode.
470 If there is no byte at the target address move to the last byte in that line."
471 (interactive "p")
472 (hexl-goto-address (let ((address (+ (hexl-current-address) (* arg 16))))
473 (if (and (< arg 0) (< address 0))
474 (progn (message "Out of hexl region.")
475 (setq address
476 (% (hexl-current-address) 16)))
477 (if (and (> address hexl-max-address)
478 (< (% hexl-max-address 16) (% address 16)))
479 (setq address hexl-max-address)
480 (if (> address hexl-max-address)
481 (progn (message "Out of hexl region.")
482 (setq
483 address
484 (+ (logand hexl-max-address -16)
485 (% (hexl-current-address) 16)))))))
486 address)))
487
488 (defun hexl-beginning-of-buffer (arg)
489 "Move to the beginning of the hexl buffer.
490 Leaves `hexl-mark' at previous position.
491 With prefix arg N, puts point N bytes of the way from the true beginning."
492 (interactive "p")
493 (push-mark (point))
494 (hexl-goto-address (+ 0 (1- arg))))
495
496 (defun hexl-end-of-buffer (arg)
497 "Go to `hexl-max-address' minus ARG."
498 (interactive "p")
499 (push-mark (point))
500 (hexl-goto-address (- hexl-max-address (1- arg))))
501
502 (defun hexl-beginning-of-line ()
503 "Goto beginning of line in hexl mode."
504 (interactive)
505 (goto-char (+ (* (/ (point) 68) 68) 11)))
506
507 (defun hexl-end-of-line ()
508 "Goto end of line in hexl mode."
509 (interactive)
510 (hexl-goto-address (let ((address (logior (hexl-current-address) 15)))
511 (if (> address hexl-max-address)
512 (setq address hexl-max-address))
513 address)))
514
515 (defun hexl-scroll-down (arg)
516 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
517 (interactive "P")
518 (if (null arg)
519 (setq arg (1- (window-height)))
520 (setq arg (prefix-numeric-value arg)))
521 (hexl-scroll-up (- arg)))
522
523 (defun hexl-scroll-up (arg)
524 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
525 (interactive "P")
526 (if (null arg)
527 (setq arg (1- (window-height)))
528 (setq arg (prefix-numeric-value arg)))
529 (let ((movement (* arg 16))
530 (address (hexl-current-address)))
531 (if (or (> (+ address movement) hexl-max-address)
532 (< (+ address movement) 0))
533 (message "Out of hexl region.")
534 (hexl-goto-address (+ address movement))
535 (recenter 0))))
536
537 (defun hexl-beginning-of-1k-page ()
538 "Go to beginning of 1k boundary."
539 (interactive)
540 (hexl-goto-address (logand (hexl-current-address) -1024)))
541
542 (defun hexl-end-of-1k-page ()
543 "Go to end of 1k boundary."
544 (interactive)
545 (hexl-goto-address (let ((address (logior (hexl-current-address) 1023)))
546 (if (> address hexl-max-address)
547 (setq address hexl-max-address))
548 address)))
549
550 (defun hexl-beginning-of-512b-page ()
551 "Go to beginning of 512 byte boundary."
552 (interactive)
553 (hexl-goto-address (logand (hexl-current-address) -512)))
554
555 (defun hexl-end-of-512b-page ()
556 "Go to end of 512 byte boundary."
557 (interactive)
558 (hexl-goto-address (let ((address (logior (hexl-current-address) 511)))
559 (if (> address hexl-max-address)
560 (setq address hexl-max-address))
561 address)))
562
563 (defun hexl-quoted-insert (arg)
564 "Read next input character and insert it.
565 Useful for inserting control characters.
566 You may also type up to 3 octal digits, to insert a character with that code"
567 (interactive "p")
568 (hexl-insert-char (read-quoted-char) arg))
569
570 ;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789ABCDEF
571
572 ;;;###autoload
573 (defun hexlify-buffer ()
574 "Convert a binary buffer to hexl format.
575 This discards the buffer's undo information."
576 (interactive)
577 (and buffer-undo-list
578 (or (y-or-n-p "Converting to hexl format discards undo info; ok? ")
579 (error "Aborted")))
580 (setq buffer-undo-list nil)
581 (let ((binary-process-output nil) ; for Ms-Dos
582 (binary-process-input t)
583 (buffer-undo-list t))
584 (shell-command-on-region (point-min) (point-max) hexlify-command t)))
585
586 (defun dehexlify-buffer ()
587 "Convert a hexl format buffer to binary.
588 This discards the buffer's undo information."
589 (interactive)
590 (and buffer-undo-list
591 (or (y-or-n-p "Converting from hexl format discards undo info; ok? ")
592 (error "Aborted")))
593 (setq buffer-undo-list nil)
594 (let ((binary-process-output t) ; for Ms-Dos
595 (binary-process-input nil)
596 (buffer-undo-list t))
597 (shell-command-on-region (point-min) (point-max) dehexlify-command t)))
598
599 (defun hexl-char-after-point ()
600 "Return char for ASCII hex digits at point."
601 (hexl-htoi (char-after (point))
602 (char-after (1+ (point)))))
603
604 (defun hexl-htoi (lh rh)
605 "Hex (char) LH (char) RH to integer."
606 (+ (* (hexl-hex-char-to-integer lh) 16)
607 (hexl-hex-char-to-integer rh)))
608
609 (defun hexl-hex-char-to-integer (character)
610 "Take a char and return its value as if it was a hex digit."
611 (if (and (>= character ?0) (<= character ?9))
612 (- character ?0)
613 (let ((ch (logior character 32)))
614 (if (and (>= ch ?a) (<= ch ?f))
615 (- ch (- ?a 10))
616 (error "Invalid hex digit `%c'." ch)))))
617
618 (defun hexl-oct-char-to-integer (character)
619 "Take a char and return its value as if it was a octal digit."
620 (if (and (>= character ?0) (<= character ?7))
621 (- character ?0)
622 (error "Invalid octal digit `%c'." character)))
623
624 (defun hexl-printable-character (ch)
625 "Return a displayable string for character CH."
626 (format "%c" (if hexl-iso
627 (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
628 46
629 ch)
630 (if (or (< ch 32) (>= ch 127))
631 46
632 ch))))
633
634 (defun hexl-self-insert-command (arg)
635 "Insert this character."
636 (interactive "p")
637 (hexl-insert-char last-command-char arg))
638
639 (defun hexl-insert-char (ch num)
640 "Insert a character in a hexl buffer."
641 (let ((address (hexl-current-address t)))
642 (while (> num 0)
643 (let ((hex-position
644 (+ (* (/ address 16) 68)
645 11
646 (* 2 (% address 16))
647 (/ (% address 16) 2)))
648 (ascii-position
649 (+ (* (/ address 16) 68) 52 (% address 16)))
650 at-ascii-position)
651 (if (= (point) ascii-position)
652 (setq at-ascii-position t))
653 (goto-char hex-position)
654 (delete-char 2)
655 (insert (format "%02x" ch))
656 (goto-char ascii-position)
657 (delete-char 1)
658 (insert (hexl-printable-character ch))
659 (or (eq address hexl-max-address)
660 (setq address (1+ address)))
661 (hexl-goto-address address)
662 (if at-ascii-position
663 (progn
664 (beginning-of-line)
665 (forward-char 51)
666 (forward-char (% address 16)))))
667 (setq num (1- num)))))
668
669 ;; hex conversion
670
671 (defun hexl-insert-hex-char (arg)
672 "Insert a ASCII char ARG times at point for a given hexadecimal number."
673 (interactive "p")
674 (let ((num (hexl-hex-string-to-integer (read-string "Hex number: "))))
675 (if (or (> num 255) (< num 0))
676 (error "Hex number out of range.")
677 (hexl-insert-char num arg))))
678
679 (defun hexl-insert-decimal-char (arg)
680 "Insert a ASCII char ARG times at point for a given decimal number."
681 (interactive "p")
682 (let ((num (string-to-int (read-string "Decimal Number: "))))
683 (if (or (> num 255) (< num 0))
684 (error "Decimal number out of range.")
685 (hexl-insert-char num arg))))
686
687 (defun hexl-insert-octal-char (arg)
688 "Insert a ASCII char ARG times at point for a given octal number."
689 (interactive "p")
690 (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: "))))
691 (if (or (> num 255) (< num 0))
692 (error "Decimal number out of range.")
693 (hexl-insert-char num arg))))
694
695 ;; startup stuff.
696
697 (if hexl-mode-map
698 nil
699 (setq hexl-mode-map (make-sparse-keymap))
700
701 (define-key hexl-mode-map [left] 'hexl-backward-char)
702 (define-key hexl-mode-map [right] 'hexl-forward-char)
703 (define-key hexl-mode-map [up] 'hexl-previous-line)
704 (define-key hexl-mode-map [down] 'hexl-next-line)
705 (define-key hexl-mode-map [M-left] 'hexl-backward-short)
706 (define-key hexl-mode-map [M-right] 'hexl-forward-short)
707 (define-key hexl-mode-map [next] 'hexl-scroll-up)
708 (define-key hexl-mode-map [prior] 'hexl-scroll-down)
709 (define-key hexl-mode-map [home] 'hexl-beginning-of-buffer)
710 (define-key hexl-mode-map [deletechar] 'undefined)
711 (define-key hexl-mode-map [deleteline] 'undefined)
712 (define-key hexl-mode-map [insertline] 'undefined)
713 (define-key hexl-mode-map [S-delete] 'undefined)
714 (define-key hexl-mode-map "\177" 'undefined)
715
716 (define-key hexl-mode-map "\C-a" 'hexl-beginning-of-line)
717 (define-key hexl-mode-map "\C-b" 'hexl-backward-char)
718 (define-key hexl-mode-map "\C-d" 'undefined)
719 (define-key hexl-mode-map "\C-e" 'hexl-end-of-line)
720 (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
721
722 (if (not (eq (key-binding (char-to-string help-char)) 'help-command))
723 (define-key hexl-mode-map (char-to-string help-char) 'undefined))
724
725 (define-key hexl-mode-map "\C-i" 'hexl-self-insert-command)
726 (define-key hexl-mode-map "\C-j" 'hexl-self-insert-command)
727 (define-key hexl-mode-map "\C-k" 'undefined)
728 (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
729 (define-key hexl-mode-map "\C-n" 'hexl-next-line)
730 (define-key hexl-mode-map "\C-o" 'undefined)
731 (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
732 (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
733 (define-key hexl-mode-map "\C-t" 'undefined)
734 (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
735 (define-key hexl-mode-map "\C-w" 'undefined)
736 (define-key hexl-mode-map "\C-y" 'undefined)
737
738 (let ((ch 32))
739 (while (< ch 127)
740 (define-key hexl-mode-map (format "%c" ch) 'hexl-self-insert-command)
741 (setq ch (1+ ch))))
742
743 (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
744 (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
745 (define-key hexl-mode-map "\e\C-c" 'undefined)
746 (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
747 (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
748 (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
749 (define-key hexl-mode-map "\e\C-g" 'undefined)
750 (define-key hexl-mode-map "\e\C-h" 'undefined)
751 (define-key hexl-mode-map "\e\C-i" 'undefined)
752 (define-key hexl-mode-map "\e\C-j" 'undefined)
753 (define-key hexl-mode-map "\e\C-k" 'undefined)
754 (define-key hexl-mode-map "\e\C-l" 'undefined)
755 (define-key hexl-mode-map "\e\C-m" 'undefined)
756 (define-key hexl-mode-map "\e\C-n" 'undefined)
757 (define-key hexl-mode-map "\e\C-o" 'hexl-insert-octal-char)
758 (define-key hexl-mode-map "\e\C-p" 'undefined)
759 (define-key hexl-mode-map "\e\C-q" 'undefined)
760 (define-key hexl-mode-map "\e\C-r" 'undefined)
761 (define-key hexl-mode-map "\e\C-s" 'undefined)
762 (define-key hexl-mode-map "\e\C-t" 'undefined)
763 (define-key hexl-mode-map "\e\C-u" 'undefined)
764
765 (define-key hexl-mode-map "\e\C-w" 'undefined)
766 (define-key hexl-mode-map "\e\C-x" 'hexl-insert-hex-char)
767 (define-key hexl-mode-map "\e\C-y" 'undefined)
768
769 (define-key hexl-mode-map "\ea" 'undefined)
770 (define-key hexl-mode-map "\eb" 'hexl-backward-word)
771 (define-key hexl-mode-map "\ec" 'undefined)
772 (define-key hexl-mode-map "\ed" 'undefined)
773 (define-key hexl-mode-map "\ee" 'undefined)
774 (define-key hexl-mode-map "\ef" 'hexl-forward-word)
775 (define-key hexl-mode-map "\eg" 'hexl-goto-hex-address)
776 (define-key hexl-mode-map "\eh" 'undefined)
777 (define-key hexl-mode-map "\ei" 'undefined)
778 (define-key hexl-mode-map "\ej" 'hexl-goto-address)
779 (define-key hexl-mode-map "\ek" 'undefined)
780 (define-key hexl-mode-map "\el" 'undefined)
781 (define-key hexl-mode-map "\em" 'undefined)
782 (define-key hexl-mode-map "\en" 'undefined)
783 (define-key hexl-mode-map "\eo" 'undefined)
784 (define-key hexl-mode-map "\ep" 'undefined)
785 (define-key hexl-mode-map "\eq" 'undefined)
786 (define-key hexl-mode-map "\er" 'undefined)
787 (define-key hexl-mode-map "\es" 'undefined)
788 (define-key hexl-mode-map "\et" 'undefined)
789 (define-key hexl-mode-map "\eu" 'undefined)
790 (define-key hexl-mode-map "\ev" 'hexl-scroll-down)
791 (define-key hexl-mode-map "\ey" 'undefined)
792 (define-key hexl-mode-map "\ez" 'undefined)
793 (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
794 (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
795
796 (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
797
798 (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
799 (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
800 (define-key hexl-mode-map "\C-x\C-p" 'undefined)
801 (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
802 (define-key hexl-mode-map "\C-x\C-t" 'undefined))
803
804 ;;; hexl.el ends here