*** empty log message ***
[emacs.git] / lisp / hexl.el
blobf5b83c0afdecd78d2b0977a9a82ee979bbafd8af
1 ;;; hexl.el --- edit a file in a hex dump format using the hexl filter
3 ;; Copyright (C) 1989, 1994, 1998, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 ;; Author: Keith Gabryelski <ag@wheaties.ai.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: data
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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'.
34 ;; NOTE: Remember to change `hexl-program' or `hexl-options' if needed.
36 ;; Currently hexl only supports big endian hex output with 16 bit
37 ;; grouping.
39 ;; -iso in `hexl-options' will allow iso characters to display in the
40 ;; ASCII region of the screen (if your emacs supports this) instead of
41 ;; changing them to dots.
43 ;;; Code:
45 (require 'eldoc)
48 ;; vars here
51 (defgroup hexl nil
52 "Edit a file in a hex dump format using the hexl filter."
53 :group 'data)
56 (defcustom hexl-program "hexl"
57 "The program that will hexlify and dehexlify its stdin.
58 `hexl-program' will always be concatenated with `hexl-options'
59 and \"-de\" when dehexlifying a buffer."
60 :type 'string
61 :group 'hexl)
63 (defcustom hexl-iso ""
64 "If your emacs can handle ISO characters, this should be set to
65 \"-iso\" otherwise it should be \"\"."
66 :type 'string
67 :group 'hexl)
69 (defcustom hexl-options (format "-hex %s" hexl-iso)
70 "Space separated options to `hexl-program' that suit your needs.
71 Quoting cannot be used, so the arguments cannot themselves contain spaces."
72 :type 'string
73 :group 'hexl)
75 (defcustom hexl-follow-ascii t
76 "If non-nil then highlight the ASCII character corresponding to point."
77 :type 'boolean
78 :group 'hexl
79 :version "20.3")
81 (defcustom hexl-mode-hook '(hexl-follow-line hexl-activate-ruler)
82 "Normal hook run when entering Hexl mode."
83 :type 'hook
84 :options '(hexl-follow-line hexl-activate-ruler turn-on-eldoc-mode)
85 :group 'hexl)
87 (defface hexl-address-area
88 '((t (:inherit header-line)))
89 "Face used in address are of hexl-mode buffer."
90 :group 'hexl)
92 (defface hexl-ascii-area
93 '((t (:inherit header-line)))
94 "Face used in ascii are of hexl-mode buffer."
95 :group 'hexl)
97 (defvar hexl-max-address 0
98 "Maximum offset into hexl buffer.")
100 (defvar hexl-mode-map nil)
102 (defvar ruler-mode)
103 (defvar ruler-mode-ruler-function)
104 (defvar hl-line-mode)
106 (defvar hexl-mode-old-hl-line-mode)
107 (defvar hexl-mode-old-local-map)
108 (defvar hexl-mode-old-mode-name)
109 (defvar hexl-mode-old-major-mode)
110 (defvar hexl-mode-old-ruler-mode)
111 (defvar hexl-mode-old-isearch-search-fun-function)
112 (defvar hexl-mode-old-require-final-newline)
113 (defvar hexl-mode-old-syntax-table)
115 (defvar hexl-ascii-overlay nil
116 "Overlay used to highlight ASCII element corresponding to current point.")
117 (make-variable-buffer-local 'hexl-ascii-overlay)
119 ;; routines
121 (put 'hexl-mode 'mode-class 'special)
123 ;;;###autoload
124 (defun hexl-mode (&optional arg)
125 "\\<hexl-mode-map>A mode for editing binary files in hex dump format.
126 This is not an ordinary major mode; it alters some aspects
127 of the current mode's behavior, but not all; also, you can exit
128 Hexl mode and return to the previous mode using `hexl-mode-exit'.
130 This function automatically converts a buffer into the hexl format
131 using the function `hexlify-buffer'.
133 Each line in the buffer has an \"address\" (displayed in hexadecimal)
134 representing the offset into the file that the characters on this line
135 are at and 16 characters from the file (displayed as hexadecimal
136 values grouped every 16 bits) and as their ASCII values.
138 If any of the characters (displayed as ASCII characters) are
139 unprintable (control or meta characters) they will be replaced as
140 periods.
142 If `hexl-mode' is invoked with an argument the buffer is assumed to be
143 in hexl format.
145 A sample format:
147 HEX ADDR: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ASCII-TEXT
148 -------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------
149 00000000: 5468 6973 2069 7320 6865 786c 2d6d 6f64 This is hexl-mod
150 00000010: 652e 2020 4561 6368 206c 696e 6520 7265 e. Each line re
151 00000020: 7072 6573 656e 7473 2031 3620 6279 7465 presents 16 byte
152 00000030: 7320 6173 2068 6578 6164 6563 696d 616c s as hexadecimal
153 00000040: 2041 5343 4949 0a61 6e64 2070 7269 6e74 ASCII.and print
154 00000050: 6162 6c65 2041 5343 4949 2063 6861 7261 able ASCII chara
155 00000060: 6374 6572 732e 2020 416e 7920 636f 6e74 cters. Any cont
156 00000070: 726f 6c20 6f72 206e 6f6e 2d41 5343 4949 rol or non-ASCII
157 00000080: 2063 6861 7261 6374 6572 730a 6172 6520 characters.are
158 00000090: 6469 7370 6c61 7965 6420 6173 2070 6572 displayed as per
159 000000a0: 696f 6473 2069 6e20 7468 6520 7072 696e iods in the prin
160 000000b0: 7461 626c 6520 6368 6172 6163 7465 7220 table character
161 000000c0: 7265 6769 6f6e 2e0a region..
163 Movement is as simple as movement in a normal emacs text buffer. Most
164 cursor movement bindings are the same (ie. Use \\[hexl-backward-char], \\[hexl-forward-char], \\[hexl-next-line], and \\[hexl-previous-line]
165 to move the cursor left, right, down, and up).
167 Advanced cursor movement commands (ala \\[hexl-beginning-of-line], \\[hexl-end-of-line], \\[hexl-beginning-of-buffer], and \\[hexl-end-of-buffer]) are
168 also supported.
170 There are several ways to change text in hexl mode:
172 ASCII characters (character between space (0x20) and tilde (0x7E)) are
173 bound to self-insert so you can simply type the character and it will
174 insert itself (actually overstrike) into the buffer.
176 \\[hexl-quoted-insert] followed by another keystroke allows you to insert the key even if
177 it isn't bound to self-insert. An octal number can be supplied in place
178 of another key to insert the octal number's ASCII representation.
180 \\[hexl-insert-hex-char] will insert a given hexadecimal value (if it is between 0 and 0xFF)
181 into the buffer at the current point.
183 \\[hexl-insert-octal-char] will insert a given octal value (if it is between 0 and 0377)
184 into the buffer at the current point.
186 \\[hexl-insert-decimal-char] will insert a given decimal value (if it is between 0 and 255)
187 into the buffer at the current point.
189 \\[hexl-mode-exit] will exit hexl-mode.
191 Note: saving the file with any of the usual Emacs commands
192 will actually convert it back to binary format while saving.
194 You can use \\[hexl-find-file] to visit a file in Hexl mode.
196 \\[describe-bindings] for advanced commands."
197 (interactive "p")
198 (unless (eq major-mode 'hexl-mode)
199 (let ((modified (buffer-modified-p))
200 (inhibit-read-only t)
201 (original-point (- (point) (point-min)))
202 max-address)
203 (and (eobp) (not (bobp))
204 (setq original-point (1- original-point)))
205 (if (not (or (eq arg 1) (not arg)))
206 ;; if no argument then we guess at hexl-max-address
207 (setq max-address (+ (* (/ (1- (buffer-size)) 68) 16) 15))
208 (setq max-address (1- (buffer-size)))
209 ;; If the buffer's EOL type is -dos, we need to account for
210 ;; extra CR characters added when hexlify-buffer writes the
211 ;; buffer to a file.
212 (when (eq (coding-system-eol-type buffer-file-coding-system) 1)
213 (setq max-address (+ (count-lines (point-min) (point-max))
214 max-address))
215 ;; But if there's no newline at the last line, we are off by
216 ;; one; adjust.
217 (or (eq (char-before (point-max)) ?\n)
218 (setq max-address (1- max-address)))
219 (setq original-point (+ (count-lines (point-min) (point))
220 original-point))
221 (or (bolp) (setq original-point (1- original-point))))
222 (hexlify-buffer)
223 (set-buffer-modified-p modified))
224 (make-local-variable 'hexl-max-address)
225 (setq hexl-max-address max-address)
226 (condition-case nil
227 (hexl-goto-address original-point)
228 (error nil)))
230 ;; We do not turn off the old major mode; instead we just
231 ;; override most of it. That way, we can restore it perfectly.
232 (make-local-variable 'hexl-mode-old-local-map)
233 (setq hexl-mode-old-local-map (current-local-map))
234 (use-local-map hexl-mode-map)
236 (make-local-variable 'hexl-mode-old-mode-name)
237 (setq hexl-mode-old-mode-name mode-name)
238 (setq mode-name "Hexl")
240 (set (make-local-variable 'hexl-mode-old-isearch-search-fun-function)
241 isearch-search-fun-function)
242 (set (make-local-variable 'isearch-search-fun-function)
243 'hexl-isearch-search-function)
245 (make-local-variable 'hexl-mode-old-major-mode)
246 (setq hexl-mode-old-major-mode major-mode)
247 (setq major-mode 'hexl-mode)
249 (make-local-variable 'hexl-mode-old-ruler-mode)
250 (setq hexl-mode-old-ruler-mode
251 (and (boundp 'ruler-mode) ruler-mode))
253 (make-local-variable 'hexl-mode-old-hl-line-mode)
254 (setq hexl-mode-old-hl-line-mode
255 (and (boundp 'hl-line-mode) hl-line-mode))
257 (make-local-variable 'hexl-mode-old-syntax-table)
258 (setq hexl-mode-old-syntax-table (syntax-table))
259 (set-syntax-table (standard-syntax-table))
261 (add-hook 'write-contents-functions 'hexl-save-buffer nil t)
263 (make-local-variable 'hexl-mode-old-require-final-newline)
264 (setq hexl-mode-old-require-final-newline require-final-newline)
265 (make-local-variable 'require-final-newline)
266 (setq require-final-newline nil)
268 ;; Add hooks to rehexlify or dehexlify on various events.
269 (add-hook 'after-revert-hook 'hexl-after-revert-hook nil t)
271 (add-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer nil t)
273 ;; Set a callback function for eldoc.
274 (set (make-local-variable 'eldoc-print-current-symbol-info-function)
275 'hexl-print-current-point-info)
276 (eldoc-add-command-completions "hexl-")
277 (eldoc-remove-command "hexl-save-buffer"
278 "hexl-current-address")
280 (if hexl-follow-ascii (hexl-follow-ascii 1)))
281 (run-hooks 'hexl-mode-hook))
284 (defun hexl-isearch-search-function ()
285 (if (and (not isearch-regexp) (not isearch-word))
286 (lambda (string &optional bound noerror count)
287 (funcall
288 (if isearch-forward 're-search-forward 're-search-backward)
289 (if (> (length string) 80)
290 (regexp-quote string)
291 (mapconcat 'string string "\\(?:\n\\(?:[:a-f0-9]+ \\)+ \\)?"))
292 bound noerror count))
293 (let ((isearch-search-fun-function nil))
294 (isearch-search-fun))))
296 (defun hexl-after-revert-hook ()
297 (setq hexl-max-address (1- (buffer-size)))
298 (hexlify-buffer)
299 (set-buffer-modified-p nil))
301 (defvar hexl-in-save-buffer nil)
303 (defun hexl-save-buffer ()
304 "Save a hexl format buffer as binary in visited file if modified."
305 (interactive)
306 (if hexl-in-save-buffer nil
307 (set-buffer-modified-p (if (buffer-modified-p)
308 (save-excursion
309 (let ((buf (generate-new-buffer " hexl"))
310 (name (buffer-name))
311 (file-name (buffer-file-name))
312 (start (point-min))
313 (end (point-max))
314 modified)
315 (set-buffer buf)
316 (insert-buffer-substring name start end)
317 (set-buffer name)
318 (dehexlify-buffer)
319 ;; Prevent infinite recursion.
320 (let ((hexl-in-save-buffer t))
321 (save-buffer))
322 (setq modified (buffer-modified-p))
323 (delete-region (point-min) (point-max))
324 (insert-buffer-substring buf start end)
325 (kill-buffer buf)
326 modified))
327 (message "(No changes need to be saved)")
328 nil))
329 ;; Return t to indicate we have saved t
332 ;;;###autoload
333 (defun hexl-find-file (filename)
334 "Edit file FILENAME in hexl-mode.
335 Switch to a buffer visiting file FILENAME, creating one in none exists."
336 (interactive
337 (list
338 (let ((completion-ignored-extensions nil))
339 (read-file-name "Filename: " nil nil 'ret-must-match))))
340 ;; Ignore the user's setting of default-major-mode.
341 (let ((default-major-mode 'fundamental-mode))
342 (find-file-literally filename))
343 (if (not (eq major-mode 'hexl-mode))
344 (hexl-mode)))
346 (defun hexl-mode-exit (&optional arg)
347 "Exit Hexl mode, returning to previous mode.
348 With arg, don't unhexlify buffer."
349 (interactive "p")
350 (if (or (eq arg 1) (not arg))
351 (let ((modified (buffer-modified-p))
352 (inhibit-read-only t)
353 (original-point (1+ (hexl-current-address))))
354 (dehexlify-buffer)
355 (remove-hook 'write-contents-functions 'hexl-save-buffer t)
356 (set-buffer-modified-p modified)
357 (goto-char original-point)
358 ;; Maybe adjust point for the removed CR characters.
359 (when (eq (coding-system-eol-type buffer-file-coding-system) 1)
360 (setq original-point (- original-point
361 (count-lines (point-min) (point))))
362 (or (bobp) (setq original-point (1+ original-point))))
363 (goto-char original-point)))
365 (remove-hook 'after-revert-hook 'hexl-after-revert-hook t)
366 (remove-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer t)
367 (remove-hook 'post-command-hook 'hexl-follow-ascii-find t)
368 (setq hexl-ascii-overlay nil)
370 (if (and (boundp 'ruler-mode) ruler-mode (not hexl-mode-old-ruler-mode))
371 (ruler-mode 0))
372 (if (and (boundp 'hl-line-mode) hl-line-mode (not hexl-mode-old-hl-line-mode))
373 (hl-line-mode 0))
374 (setq require-final-newline hexl-mode-old-require-final-newline)
375 (setq mode-name hexl-mode-old-mode-name)
376 (setq isearch-search-fun-function hexl-mode-old-isearch-search-fun-function)
377 (use-local-map hexl-mode-old-local-map)
378 (set-syntax-table hexl-mode-old-syntax-table)
379 (setq major-mode hexl-mode-old-major-mode)
380 (force-mode-line-update))
382 (defun hexl-maybe-dehexlify-buffer ()
383 "Convert a hexl format buffer to binary.
384 Ask the user for confirmation."
385 (if (y-or-n-p "Convert contents back to binary format? ")
386 (let ((modified (buffer-modified-p))
387 (inhibit-read-only t)
388 (original-point (1+ (hexl-current-address))))
389 (dehexlify-buffer)
390 (remove-hook 'write-contents-functions 'hexl-save-buffer t)
391 (set-buffer-modified-p modified)
392 (goto-char original-point))))
394 (defun hexl-current-address (&optional validate)
395 "Return current hexl-address."
396 (interactive)
397 (let ((current-column (- (% (- (point) (point-min) -1) 68) 11))
398 (hexl-address 0))
399 (if (< current-column 0)
400 (if validate
401 (error "Point is not on a character in the file")
402 (setq current-column 0)))
403 (setq hexl-address
404 (+ (* (/ (- (point) (point-min) -1) 68) 16)
405 (if (>= current-column 41)
406 (- current-column 41)
407 (/ (- current-column (/ current-column 5)) 2))))
408 (when (interactive-p)
409 (message "Current address is %d/0x%08x" hexl-address hexl-address))
410 hexl-address))
412 (defun hexl-print-current-point-info ()
413 "Return current hexl-address in string.
414 This function is indented to be used as eldoc callback."
415 (let ((addr (hexl-current-address)))
416 (format "Current address is %d/0x%08x" addr addr)))
418 (defun hexl-address-to-marker (address)
419 "Return buffer position for ADDRESS."
420 (interactive "nAddress: ")
421 (+ (* (/ address 16) 68) 10 (point-min) (/ (* (% address 16) 5) 2)))
423 (defun hexl-goto-address (address)
424 "Goto hexl-mode (decimal) address ADDRESS.
425 Signal error if ADDRESS out of range."
426 (interactive "nAddress: ")
427 (if (or (< address 0) (> address hexl-max-address))
428 (error "Out of hexl region"))
429 (goto-char (hexl-address-to-marker address)))
431 (defun hexl-goto-hex-address (hex-address)
432 "Go to hexl-mode address (hex string) HEX-ADDRESS.
433 Signal error if HEX-ADDRESS is out of range."
434 (interactive "sHex Address: ")
435 (hexl-goto-address (hexl-hex-string-to-integer hex-address)))
437 (defun hexl-hex-string-to-integer (hex-string)
438 "Return decimal integer for HEX-STRING."
439 (interactive "sHex number: ")
440 (let ((hex-num 0))
441 (while (not (equal hex-string ""))
442 (setq hex-num (+ (* hex-num 16)
443 (hexl-hex-char-to-integer (string-to-char hex-string))))
444 (setq hex-string (substring hex-string 1)))
445 hex-num))
447 (defun hexl-octal-string-to-integer (octal-string)
448 "Return decimal integer for OCTAL-STRING."
449 (interactive "sOctal number: ")
450 (let ((oct-num 0))
451 (while (not (equal octal-string ""))
452 (setq oct-num (+ (* oct-num 8)
453 (hexl-oct-char-to-integer
454 (string-to-char octal-string))))
455 (setq octal-string (substring octal-string 1)))
456 oct-num))
458 ;; move point functions
460 (defun hexl-backward-char (arg)
461 "Move to left ARG bytes (right if ARG negative) in hexl-mode."
462 (interactive "p")
463 (hexl-goto-address (- (hexl-current-address) arg)))
465 (defun hexl-forward-char (arg)
466 "Move right ARG bytes (left if ARG negative) in hexl-mode."
467 (interactive "p")
468 (hexl-goto-address (+ (hexl-current-address) arg)))
470 (defun hexl-backward-short (arg)
471 "Move to left ARG shorts (right if ARG negative) in hexl-mode."
472 (interactive "p")
473 (hexl-goto-address (let ((address (hexl-current-address)))
474 (if (< arg 0)
475 (progn
476 (setq arg (- arg))
477 (while (> arg 0)
478 (if (not (equal address (logior address 3)))
479 (if (> address hexl-max-address)
480 (progn
481 (message "End of buffer.")
482 (setq address hexl-max-address))
483 (setq address (logior address 3)))
484 (if (> address hexl-max-address)
485 (progn
486 (message "End of buffer.")
487 (setq address hexl-max-address))
488 (setq address (+ address 4))))
489 (setq arg (1- arg)))
490 (if (> address hexl-max-address)
491 (progn
492 (message "End of buffer.")
493 (setq address hexl-max-address))
494 (setq address (logior address 3))))
495 (while (> arg 0)
496 (if (not (equal address (logand address -4)))
497 (setq address (logand address -4))
498 (if (not (equal address 0))
499 (setq address (- address 4))
500 (message "Beginning of buffer.")))
501 (setq arg (1- arg))))
502 address)))
504 (defun hexl-forward-short (arg)
505 "Move right ARG shorts (left if ARG negative) in hexl-mode."
506 (interactive "p")
507 (hexl-backward-short (- arg)))
509 (defun hexl-backward-word (arg)
510 "Move to left ARG words (right if ARG negative) in hexl-mode."
511 (interactive "p")
512 (hexl-goto-address (let ((address (hexl-current-address)))
513 (if (< arg 0)
514 (progn
515 (setq arg (- arg))
516 (while (> arg 0)
517 (if (not (equal address (logior address 7)))
518 (if (> address hexl-max-address)
519 (progn
520 (message "End of buffer.")
521 (setq address hexl-max-address))
522 (setq address (logior address 7)))
523 (if (> address hexl-max-address)
524 (progn
525 (message "End of buffer.")
526 (setq address hexl-max-address))
527 (setq address (+ address 8))))
528 (setq arg (1- arg)))
529 (if (> address hexl-max-address)
530 (progn
531 (message "End of buffer.")
532 (setq address hexl-max-address))
533 (setq address (logior address 7))))
534 (while (> arg 0)
535 (if (not (equal address (logand address -8)))
536 (setq address (logand address -8))
537 (if (not (equal address 0))
538 (setq address (- address 8))
539 (message "Beginning of buffer.")))
540 (setq arg (1- arg))))
541 address)))
543 (defun hexl-forward-word (arg)
544 "Move right ARG words (left if ARG negative) in hexl-mode."
545 (interactive "p")
546 (hexl-backward-word (- arg)))
548 (defun hexl-previous-line (arg)
549 "Move vertically up ARG lines [16 bytes] (down if ARG negative) in hexl-mode.
550 If there is byte at the target address move to the last byte in that line."
551 (interactive "p")
552 (hexl-next-line (- arg)))
554 (defun hexl-next-line (arg)
555 "Move vertically down ARG lines [16 bytes] (up if ARG negative) in hexl-mode.
556 If there is no byte at the target address move to the last byte in that line."
557 (interactive "p")
558 (hexl-goto-address (let ((address (+ (hexl-current-address) (* arg 16))))
559 (if (and (< arg 0) (< address 0))
560 (progn (message "Out of hexl region.")
561 (setq address
562 (% (hexl-current-address) 16)))
563 (if (and (> address hexl-max-address)
564 (< (% hexl-max-address 16) (% address 16)))
565 (setq address hexl-max-address)
566 (if (> address hexl-max-address)
567 (progn (message "Out of hexl region.")
568 (setq
569 address
570 (+ (logand hexl-max-address -16)
571 (% (hexl-current-address) 16)))))))
572 address)))
574 (defun hexl-beginning-of-buffer (arg)
575 "Move to the beginning of the hexl buffer.
576 Leaves `hexl-mark' at previous position.
577 With prefix arg N, puts point N bytes of the way from the true beginning."
578 (interactive "p")
579 (push-mark (point))
580 (hexl-goto-address (+ 0 (1- arg))))
582 (defun hexl-end-of-buffer (arg)
583 "Go to `hexl-max-address' minus ARG."
584 (interactive "p")
585 (push-mark (point))
586 (hexl-goto-address (- hexl-max-address (1- arg))))
588 (defun hexl-beginning-of-line ()
589 "Goto beginning of line in hexl mode."
590 (interactive)
591 (goto-char (+ (* (/ (point) 68) 68) 11)))
593 (defun hexl-end-of-line ()
594 "Goto end of line in hexl mode."
595 (interactive)
596 (hexl-goto-address (let ((address (logior (hexl-current-address) 15)))
597 (if (> address hexl-max-address)
598 (setq address hexl-max-address))
599 address)))
601 (defun hexl-scroll-down (arg)
602 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
603 (interactive "P")
604 (if (null arg)
605 (setq arg (1- (window-height)))
606 (setq arg (prefix-numeric-value arg)))
607 (hexl-scroll-up (- arg)))
609 (defun hexl-scroll-up (arg)
610 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG.
611 If there's no byte at the target address, move to the first or last line."
612 (interactive "P")
613 (if (null arg)
614 (setq arg (1- (window-height)))
615 (setq arg (prefix-numeric-value arg)))
616 (let* ((movement (* arg 16))
617 (address (hexl-current-address))
618 (dest (+ address movement)))
619 (cond
620 ;; If possible, try to stay at the same offset from the beginning
621 ;; of the 16-byte group, even if we move to the first or last
622 ;; group.
623 ((and (> dest hexl-max-address)
624 (>= (% hexl-max-address 16) (% address 16)))
625 (setq dest (+ (logand hexl-max-address -16) (% address 16))))
626 ((> dest hexl-max-address)
627 (setq dest hexl-max-address))
628 ((< dest 0)
629 (setq dest (% address 16))))
630 (if (/= dest (+ address movement))
631 (message "Out of hexl region."))
632 (hexl-goto-address dest)
633 (recenter 0)))
635 (defun hexl-beginning-of-1k-page ()
636 "Go to beginning of 1k boundary."
637 (interactive)
638 (hexl-goto-address (logand (hexl-current-address) -1024)))
640 (defun hexl-end-of-1k-page ()
641 "Go to end of 1k boundary."
642 (interactive)
643 (hexl-goto-address (let ((address (logior (hexl-current-address) 1023)))
644 (if (> address hexl-max-address)
645 (setq address hexl-max-address))
646 address)))
648 (defun hexl-beginning-of-512b-page ()
649 "Go to beginning of 512 byte boundary."
650 (interactive)
651 (hexl-goto-address (logand (hexl-current-address) -512)))
653 (defun hexl-end-of-512b-page ()
654 "Go to end of 512 byte boundary."
655 (interactive)
656 (hexl-goto-address (let ((address (logior (hexl-current-address) 511)))
657 (if (> address hexl-max-address)
658 (setq address hexl-max-address))
659 address)))
661 (defun hexl-quoted-insert (arg)
662 "Read next input character and insert it.
663 Useful for inserting control characters and non-ASCII characters given their
664 numerical code.
665 You may also type octal digits, to insert a character with that code."
666 (interactive "p")
667 (hexl-insert-multibyte-char (read-quoted-char) arg))
669 ;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789ABCDEF
671 ;;;###autoload
672 (defun hexlify-buffer ()
673 "Convert a binary buffer to hexl format.
674 This discards the buffer's undo information."
675 (interactive)
676 (and buffer-undo-list
677 (or (y-or-n-p "Converting to hexl format discards undo info; ok? ")
678 (error "Aborted")))
679 (setq buffer-undo-list nil)
680 ;; Don't decode text in the ASCII part of `hexl' program output.
681 (let ((coding-system-for-read 'raw-text)
682 (coding-system-for-write buffer-file-coding-system)
683 (buffer-undo-list t))
684 (apply 'call-process-region (point-min) (point-max)
685 (expand-file-name hexl-program exec-directory)
686 t t nil (split-string hexl-options))
687 (save-excursion
688 (goto-char (point-min))
689 (while (re-search-forward "^[0-9a-f]+:" nil t)
690 (put-text-property (match-beginning 0) (match-end 0)
691 'font-lock-face 'hexl-address-area))
692 (goto-char (point-min))
693 (while (re-search-forward " \\(.+$\\)" nil t)
694 (put-text-property (match-beginning 1) (match-end 1)
695 'font-lock-face 'hexl-ascii-area)))
696 (if (> (point) (hexl-address-to-marker hexl-max-address))
697 (hexl-goto-address hexl-max-address))))
699 (defun dehexlify-buffer ()
700 "Convert a hexl format buffer to binary.
701 This discards the buffer's undo information."
702 (interactive)
703 (and buffer-undo-list
704 (or (y-or-n-p "Converting from hexl format discards undo info; ok? ")
705 (error "Aborted")))
706 (setq buffer-undo-list nil)
707 (let ((coding-system-for-write 'raw-text)
708 (coding-system-for-read buffer-file-coding-system)
709 (buffer-undo-list t))
710 (apply 'call-process-region (point-min) (point-max)
711 (expand-file-name hexl-program exec-directory)
712 t t nil "-de" (split-string hexl-options))))
714 (defun hexl-char-after-point ()
715 "Return char for ASCII hex digits at point."
716 (hexl-htoi (char-after (point))
717 (char-after (1+ (point)))))
719 (defun hexl-htoi (lh rh)
720 "Hex (char) LH (char) RH to integer."
721 (+ (* (hexl-hex-char-to-integer lh) 16)
722 (hexl-hex-char-to-integer rh)))
724 (defun hexl-hex-char-to-integer (character)
725 "Take a char and return its value as if it was a hex digit."
726 (if (and (>= character ?0) (<= character ?9))
727 (- character ?0)
728 (let ((ch (logior character 32)))
729 (if (and (>= ch ?a) (<= ch ?f))
730 (- ch (- ?a 10))
731 (error "Invalid hex digit `%c'" ch)))))
733 (defun hexl-oct-char-to-integer (character)
734 "Take a char and return its value as if it was a octal digit."
735 (if (and (>= character ?0) (<= character ?7))
736 (- character ?0)
737 (error "Invalid octal digit `%c'" character)))
739 (defun hexl-printable-character (ch)
740 "Return a displayable string for character CH."
741 (format "%c" (if hexl-iso
742 (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
745 (if (or (< ch 32) (>= ch 127))
747 ch))))
749 (defun hexl-insert-multibyte-char (ch num)
750 "Insert a possibly multibyte character CH NUM times.
752 Non-ASCII characters are first encoded with `buffer-file-coding-system',
753 and their encoded form is inserted byte by byte."
754 (let ((charset (char-charset ch))
755 (coding (if (or (null buffer-file-coding-system)
756 ;; coding-system-type equals t means undecided.
757 (eq (coding-system-type buffer-file-coding-system) t))
758 default-buffer-file-coding-system
759 buffer-file-coding-system)))
760 (cond ((and (> ch 0) (< ch 256))
761 (hexl-insert-char ch num))
762 ((eq charset 'unknown)
763 (error
764 "0x%x -- invalid character code; use \\[hexl-insert-hex-string]"
765 ch))
767 (let ((encoded (encode-coding-char ch coding))
768 (internal (string-as-unibyte (char-to-string ch)))
769 internal-hex)
770 ;; If encode-coding-char returns nil, it means our character
771 ;; cannot be safely encoded with buffer-file-coding-system.
772 ;; In that case, we offer to insert the internal representation
773 ;; of that character, byte by byte.
774 (when (null encoded)
775 (setq internal-hex
776 (mapconcat (function (lambda (c) (format "%x" c)))
777 internal " "))
778 (if (yes-or-no-p
779 (format
780 "Insert char 0x%x's internal representation \"%s\"? "
781 ch internal-hex))
782 (setq encoded internal)
783 (error
784 "Can't encode `0x%x' with this buffer's coding system; try \\[hexl-insert-hex-string]"
785 ch)))
786 (while (> num 0)
787 (mapc
788 (function (lambda (c) (hexl-insert-char c 1))) encoded)
789 (setq num (1- num))))))))
791 (defun hexl-self-insert-command (arg)
792 "Insert this character.
793 Interactively, with a numeric argument, insert this character that many times.
795 Non-ASCII characters are first encoded with `buffer-file-coding-system',
796 and their encoded form is inserted byte by byte."
797 (interactive "p")
798 (hexl-insert-multibyte-char last-command-char arg))
800 (defun hexl-insert-char (ch num)
801 "Insert the character CH NUM times in a hexl buffer.
803 CH must be a unibyte character whose value is between 0 and 255."
804 (if (or (< ch 0) (> ch 255))
805 (error "Invalid character 0x%x -- must be in the range [0..255]" ch))
806 (let ((address (hexl-current-address t)))
807 (while (> num 0)
808 (let ((hex-position
809 (+ (* (/ address 16) 68)
810 10 (point-min)
811 (* 2 (% address 16))
812 (/ (% address 16) 2)))
813 (ascii-position
814 (+ (* (/ address 16) 68) 51 (point-min) (% address 16)))
815 at-ascii-position)
816 (if (= (point) ascii-position)
817 (setq at-ascii-position t))
818 (goto-char hex-position)
819 (delete-char 2)
820 (insert (format "%02x" ch))
821 (goto-char ascii-position)
822 (delete-char 1)
823 (insert (hexl-printable-character ch))
824 (or (eq address hexl-max-address)
825 (setq address (1+ address)))
826 (hexl-goto-address address)
827 (if at-ascii-position
828 (progn
829 (beginning-of-line)
830 (forward-char 51)
831 (forward-char (% address 16)))))
832 (setq num (1- num)))))
834 ;; hex conversion
836 (defun hexl-insert-hex-char (arg)
837 "Insert a character given by its hexadecimal code ARG times at point."
838 (interactive "p")
839 (let ((num (hexl-hex-string-to-integer (read-string "Hex number: "))))
840 (if (< num 0)
841 (error "Hex number out of range")
842 (hexl-insert-multibyte-char num arg))))
844 (defun hexl-insert-hex-string (str arg)
845 "Insert hexadecimal string STR at point ARG times.
846 Embedded whitespace, dashes, and periods in the string are ignored."
847 (interactive "sHex string: \np")
848 (setq str (replace-regexp-in-string "[- \t.]" "" str))
849 (let ((chars '()))
850 (let ((len (length str))
851 (idx 0))
852 (if (eq (logand len 1) 1)
853 (let ((num (hexl-hex-string-to-integer (substring str 0 1))))
854 (setq chars (cons num chars))
855 (setq idx 1)))
856 (while (< idx len)
857 (let* ((nidx (+ idx 2))
858 (num (hexl-hex-string-to-integer (substring str idx nidx))))
859 (setq chars (cons num chars))
860 (setq idx nidx))))
861 (setq chars (nreverse chars))
862 (while (> arg 0)
863 (let ((chars chars))
864 (while chars
865 (hexl-insert-char (car chars) 1)
866 (setq chars (cdr chars))))
867 (setq arg (- arg 1)))))
869 (defun hexl-insert-decimal-char (arg)
870 "Insert a character given by its decimal code ARG times at point."
871 (interactive "p")
872 (let ((num (string-to-int (read-string "Decimal Number: "))))
873 (if (< num 0)
874 (error "Decimal number out of range")
875 (hexl-insert-multibyte-char num arg))))
877 (defun hexl-insert-octal-char (arg)
878 "Insert a character given by its octal code ARG times at point."
879 (interactive "p")
880 (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: "))))
881 (if (< num 0)
882 (error "Decimal number out of range")
883 (hexl-insert-multibyte-char num arg))))
885 (defun hexl-follow-ascii (&optional arg)
886 "Toggle following ASCII in Hexl buffers.
887 With prefix ARG, turn on following if and only if ARG is positive.
888 When following is enabled, the ASCII character corresponding to the
889 element under the point is highlighted.
890 Customize the variable `hexl-follow-ascii' to disable this feature."
891 (interactive "P")
892 (let ((on-p (if arg
893 (> (prefix-numeric-value arg) 0)
894 (not hexl-ascii-overlay))))
896 (if on-p
897 ;; turn it on
898 (if (not hexl-ascii-overlay)
899 (progn
900 (setq hexl-ascii-overlay (make-overlay 1 1)
901 hexl-follow-ascii t)
902 (overlay-put hexl-ascii-overlay 'face 'highlight)
903 (add-hook 'post-command-hook 'hexl-follow-ascii-find nil t)))
904 ;; turn it off
905 (if hexl-ascii-overlay
906 (progn
907 (delete-overlay hexl-ascii-overlay)
908 (setq hexl-ascii-overlay nil
909 hexl-follow-ascii nil)
910 (remove-hook 'post-command-hook 'hexl-follow-ascii-find t)
911 )))))
913 (defun hexl-activate-ruler ()
914 "Activate `ruler-mode'"
915 (require 'ruler-mode)
916 (set (make-local-variable 'ruler-mode-ruler-function)
917 'hexl-mode-ruler)
918 (ruler-mode 1))
920 (defun hexl-follow-line ()
921 "Activate `hl-line-mode'"
922 (require 'frame)
923 (require 'hl-line)
924 (set (make-local-variable 'hl-line-range-function)
925 'hexl-highlight-line-range)
926 (set (make-local-variable 'hl-line-face)
927 'highlight)
928 (hl-line-mode 1))
930 (defun hexl-highlight-line-range ()
931 "Return the range of address area for the point.
932 This function is assumed to be used as call back function for `hl-line-mode'."
933 (cons
934 (line-beginning-position)
935 ;; 9 stands for (length "87654321:")
936 (+ (line-beginning-position) 9)))
938 (defun hexl-follow-ascii-find ()
939 "Find and highlight the ASCII element corresponding to current point."
940 (let ((pos (+ 51
941 (- (point) (current-column))
942 (mod (hexl-current-address) 16))))
943 (move-overlay hexl-ascii-overlay pos (1+ pos))
946 (defun hexl-mode-ruler ()
947 "Return a string ruler for hexl mode."
948 (let* ((highlight (mod (hexl-current-address) 16))
949 (s " 87654321 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789abcdef")
950 (pos 0))
951 (set-text-properties 0 (length s) nil s)
952 ;; Turn spaces in the header into stretch specs so they work
953 ;; regardless of the header-line face.
954 (while (string-match "[ \t]+" s pos)
955 (setq pos (match-end 0))
956 (put-text-property (match-beginning 0) pos 'display
957 ;; Assume fixed-size chars
958 `(space :align-to ,(1- pos))
960 ;; Highlight the current column.
961 (put-text-property (+ 11 (/ (* 5 highlight) 2))
962 (+ 13 (/ (* 5 highlight) 2))
963 'face 'highlight s)
964 ;; Highlight the current ascii column
965 (put-text-property (+ 13 39 highlight) (+ 13 40 highlight)
966 'face 'highlight s)
969 ;; startup stuff.
971 (if hexl-mode-map
973 (setq hexl-mode-map (make-keymap))
974 ;; Make all self-inserting keys go through hexl-self-insert-command,
975 ;; because we need to convert them to unibyte characters before
976 ;; inserting them into the buffer.
977 (define-key hexl-mode-map [remap self-insert-command] 'hexl-self-insert-command)
979 (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
980 (define-key hexl-mode-map [left] 'hexl-backward-char)
981 (define-key hexl-mode-map [right] 'hexl-forward-char)
982 (define-key hexl-mode-map [up] 'hexl-previous-line)
983 (define-key hexl-mode-map [down] 'hexl-next-line)
984 (define-key hexl-mode-map [M-left] 'hexl-backward-short)
985 (define-key hexl-mode-map [M-right] 'hexl-forward-short)
986 (define-key hexl-mode-map [next] 'hexl-scroll-up)
987 (define-key hexl-mode-map [prior] 'hexl-scroll-down)
988 (define-key hexl-mode-map [home] 'hexl-beginning-of-line)
989 (define-key hexl-mode-map [end] 'hexl-end-of-line)
990 (define-key hexl-mode-map [C-home] 'hexl-beginning-of-buffer)
991 (define-key hexl-mode-map [C-end] 'hexl-end-of-buffer)
992 (define-key hexl-mode-map [deletechar] 'undefined)
993 (define-key hexl-mode-map [deleteline] 'undefined)
994 (define-key hexl-mode-map [insertline] 'undefined)
995 (define-key hexl-mode-map [S-delete] 'undefined)
996 (define-key hexl-mode-map "\177" 'undefined)
998 (define-key hexl-mode-map "\C-a" 'hexl-beginning-of-line)
999 (define-key hexl-mode-map "\C-b" 'hexl-backward-char)
1000 (define-key hexl-mode-map "\C-d" 'undefined)
1001 (define-key hexl-mode-map "\C-e" 'hexl-end-of-line)
1002 (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
1004 (if (not (eq (key-binding (char-to-string help-char)) 'help-command))
1005 (define-key hexl-mode-map (char-to-string help-char) 'undefined))
1007 (define-key hexl-mode-map "\C-k" 'undefined)
1008 (define-key hexl-mode-map "\C-n" 'hexl-next-line)
1009 (define-key hexl-mode-map "\C-o" 'undefined)
1010 (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
1011 (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
1012 (define-key hexl-mode-map "\C-t" 'undefined)
1013 (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
1014 (define-key hexl-mode-map "\C-w" 'undefined)
1015 (define-key hexl-mode-map "\C-y" 'undefined)
1017 (fset 'hexl-ESC-prefix (copy-keymap 'ESC-prefix))
1018 (define-key hexl-mode-map "\e" 'hexl-ESC-prefix)
1019 (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
1020 (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
1021 (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
1022 (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
1023 (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
1024 (define-key hexl-mode-map "\e\C-i" 'undefined)
1025 (define-key hexl-mode-map "\e\C-j" 'undefined)
1026 (define-key hexl-mode-map "\e\C-k" 'undefined)
1027 (define-key hexl-mode-map "\e\C-o" 'hexl-insert-octal-char)
1028 (define-key hexl-mode-map "\e\C-q" 'undefined)
1029 (define-key hexl-mode-map "\e\C-t" 'undefined)
1030 (define-key hexl-mode-map "\e\C-x" 'hexl-insert-hex-char)
1031 (define-key hexl-mode-map "\eb" 'hexl-backward-word)
1032 (define-key hexl-mode-map "\ec" 'undefined)
1033 (define-key hexl-mode-map "\ed" 'undefined)
1034 (define-key hexl-mode-map "\ef" 'hexl-forward-word)
1035 (define-key hexl-mode-map "\eg" 'hexl-goto-hex-address)
1036 (define-key hexl-mode-map "\ei" 'undefined)
1037 (define-key hexl-mode-map "\ej" 'hexl-goto-address)
1038 (define-key hexl-mode-map "\ek" 'undefined)
1039 (define-key hexl-mode-map "\el" 'undefined)
1040 (define-key hexl-mode-map "\eq" 'undefined)
1041 (define-key hexl-mode-map "\es" 'undefined)
1042 (define-key hexl-mode-map "\et" 'undefined)
1043 (define-key hexl-mode-map "\eu" 'undefined)
1044 (define-key hexl-mode-map "\ev" 'hexl-scroll-down)
1045 (define-key hexl-mode-map "\ey" 'undefined)
1046 (define-key hexl-mode-map "\ez" 'undefined)
1047 (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
1048 (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
1050 (fset 'hexl-C-c-prefix (copy-keymap mode-specific-map))
1051 (define-key hexl-mode-map "\C-c" 'hexl-C-c-prefix)
1052 (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
1054 (fset 'hexl-C-x-prefix (copy-keymap 'Control-X-prefix))
1055 (define-key hexl-mode-map "\C-x" 'hexl-C-x-prefix)
1056 (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
1057 (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
1058 (define-key hexl-mode-map "\C-x\C-p" 'undefined)
1059 (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
1060 (define-key hexl-mode-map "\C-x\C-t" 'undefined))
1062 (provide 'hexl)
1064 ;;; arch-tag: d5a7aa8a-9bce-480b-bcff-6c4c7ca5ea4a
1065 ;;; hexl.el ends here