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