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
|
|
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24
|
|
25 ;;; Synched up with: FSF 19.30.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This package implements a major mode for editing binary files. It uses
|
|
30 ;; a program called hexl, supplied with the GNU Emacs distribution, that
|
|
31 ;; can filter a binary into an editable format or from the format back into
|
|
32 ;; binary. For full instructions, invoke `hexl-mode' on an empty buffer and
|
|
33 ;; do `M-x describe-mode'.
|
|
34 ;;
|
|
35 ;; This may be useful in your .emacs:
|
|
36 ;;
|
|
37 ;; (autoload 'hexl-find-file "hexl"
|
|
38 ;; "Edit file FILENAME in hexl-mode." t)
|
|
39 ;;
|
|
40 ;; (define-key global-map "\C-c\C-h" 'hexl-find-file)
|
|
41 ;;
|
|
42 ;; NOTE: Remember to change HEXL-PROGRAM or HEXL-OPTIONS if needed.
|
|
43 ;;
|
|
44 ;; Currently hexl only supports big endian hex output with 16 bit
|
|
45 ;; grouping.
|
|
46 ;;
|
|
47 ;; -iso in `hexl-options' will allow iso characters to display in the
|
|
48 ;; ASCII region of the screen (if your emacs supports this) instead of
|
|
49 ;; changing them to dots.
|
|
50
|
|
51 ;;; Code:
|
|
52
|
|
53 ;;
|
|
54 ;; vars here
|
|
55 ;;
|
|
56
|
|
57 (defvar hexl-program "hexl"
|
|
58 "The program that will hexlify and de-hexlify its stdin.
|
|
59 `hexl-program' will always be concatenated with `hexl-options'
|
|
60 and \"-de\" when dehexlfying a buffer.")
|
|
61
|
|
62 (defvar hexl-iso ""
|
|
63 "If your emacs can handle ISO characters, this should be set to
|
|
64 \"-iso\" otherwise it should be \"\".")
|
|
65
|
|
66 (defvar hexl-options (format "-hex %s" hexl-iso)
|
|
67 "Options to hexl-program that suit your needs.")
|
|
68
|
|
69 (defvar hexlify-command
|
|
70 (format "%s%s %s" exec-directory hexl-program hexl-options)
|
|
71 "The command to use to hexlify a buffer.")
|
|
72
|
|
73 (defvar dehexlify-command
|
|
74 (format "%s%s -de %s" exec-directory hexl-program hexl-options)
|
|
75 "The command to use to unhexlify a buffer.")
|
|
76
|
|
77 (defvar hexl-max-address 0
|
|
78 "Maximum offset into hexl buffer.")
|
|
79
|
|
80 (defvar hexl-mode-map nil)
|
|
81
|
|
82 (defvar hexl-mode-old-local-map)
|
|
83 (defvar hexl-mode-old-mode-name)
|
|
84 (defvar hexl-mode-old-major-mode)
|
|
85
|
|
86 ;; routines
|
|
87
|
|
88 (defvar hexl-mode-old-local-map)
|
|
89 (defvar hexl-mode-old-mode-name)
|
|
90 (defvar hexl-mode-old-major-mode)
|
|
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)
|
|
166 (error "You are already in hexl mode.")
|
|
167 (kill-all-local-variables)
|
|
168 (make-local-variable 'hexl-mode-old-local-map)
|
|
169 (setq hexl-mode-old-local-map (current-local-map))
|
|
170 (use-local-map hexl-mode-map)
|
|
171
|
|
172 (make-local-variable 'hexl-mode-old-mode-name)
|
|
173 (setq hexl-mode-old-mode-name mode-name)
|
|
174 (setq mode-name "Hexl")
|
|
175
|
|
176 (make-local-variable 'hexl-mode-old-major-mode)
|
|
177 (setq hexl-mode-old-major-mode major-mode)
|
|
178 (setq major-mode 'hexl-mode)
|
|
179
|
|
180 (make-local-variable 'write-contents-hooks)
|
|
181 (add-hook 'write-contents-hooks 'hexl-save-buffer)
|
|
182
|
|
183 (make-local-hook 'after-revert-hook)
|
|
184 (add-hook 'after-revert-hook 'hexl-after-revert-hook nil t)
|
|
185
|
|
186 (make-local-variable 'hexl-max-address)
|
|
187
|
|
188 (make-local-variable 'change-major-mode-hook)
|
|
189 (add-hook 'change-major-mode-hook 'hexl-maybe-dehexlify-buffer)
|
|
190
|
|
191 (make-local-variable 'require-final-newline)
|
|
192 (setq require-final-newline nil)
|
|
193
|
|
194 (let ((modified (buffer-modified-p))
|
|
195 (inhibit-read-only t)
|
|
196 (original-point (1- (point))))
|
|
197 (and (eobp) (not (bobp))
|
|
198 (setq original-point (1- original-point)))
|
|
199 (if (not (or (eq arg 1) (not arg)))
|
|
200 ;; if no argument then we guess at hexl-max-address
|
|
201 (setq hexl-max-address (+ (* (/ (1- (buffer-size)) 68) 16) 15))
|
|
202 (setq hexl-max-address (1- (buffer-size)))
|
|
203 (hexlify-buffer)
|
|
204 (set-buffer-modified-p modified)
|
|
205 (hexl-goto-address original-point)))))
|
|
206
|
|
207 (defun hexl-after-revert-hook ()
|
|
208 (hexlify-buffer)
|
|
209 (set-buffer-modified-p nil))
|
|
210
|
|
211 (defvar hexl-in-save-buffer nil)
|
|
212
|
|
213 (defun hexl-save-buffer ()
|
|
214 "Save a hexl format buffer as binary in visited file if modified."
|
|
215 (interactive)
|
|
216 (if hexl-in-save-buffer nil
|
|
217 (set-buffer-modified-p (if (buffer-modified-p)
|
|
218 (save-excursion
|
|
219 (let ((buf (generate-new-buffer " hexl"))
|
|
220 (name (buffer-name))
|
|
221 (file-name (buffer-file-name))
|
|
222 (start (point-min))
|
|
223 (end (point-max))
|
|
224 modified)
|
|
225 (set-buffer buf)
|
|
226 (insert-buffer-substring name start end)
|
|
227 (set-buffer name)
|
|
228 (dehexlify-buffer)
|
|
229 ;; Prevent infinite recursion.
|
|
230 (let ((hexl-in-save-buffer t)
|
|
231 (buffer-file-type t)) ; for ms-dos
|
|
232 (save-buffer))
|
|
233 (setq modified (buffer-modified-p))
|
|
234 (delete-region (point-min) (point-max))
|
|
235 (insert-buffer-substring buf start end)
|
|
236 (kill-buffer buf)
|
|
237 modified))
|
|
238 (message "(No changes need to be saved)")
|
|
239 nil))
|
|
240 ;; Return t to indicate we have saved t
|
|
241 t))
|
|
242
|
|
243 ;;;###autoload
|
|
244 (defun hexl-find-file (filename)
|
|
245 "Edit file FILENAME in hexl-mode.
|
|
246 Switch to a buffer visiting file FILENAME, creating one in none exists."
|
|
247 (interactive "fFilename: ")
|
|
248 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
|
|
249 (find-file-binary filename)
|
|
250 (find-file filename))
|
|
251 (if (not (eq major-mode 'hexl-mode))
|
|
252 (hexl-mode)))
|
|
253
|
|
254 (defun hexl-mode-exit (&optional arg)
|
|
255 "Exit Hexl mode, returning to previous mode.
|
|
256 With arg, don't unhexlify buffer."
|
|
257 (interactive "p")
|
|
258 (if (or (eq arg 1) (not arg))
|
|
259 (let ((modified (buffer-modified-p))
|
|
260 (inhibit-read-only t)
|
|
261 (original-point (1+ (hexl-current-address))))
|
|
262 (dehexlify-buffer)
|
|
263 (remove-hook 'write-contents-hooks 'hexl-save-buffer)
|
|
264 (set-buffer-modified-p modified)
|
|
265 (goto-char original-point)))
|
|
266 (setq mode-name hexl-mode-old-mode-name)
|
|
267 (use-local-map hexl-mode-old-local-map)
|
|
268 (setq major-mode hexl-mode-old-major-mode)
|
|
269 (force-mode-line-update))
|
|
270
|
|
271 (defun hexl-maybe-dehexlify-buffer ()
|
|
272 "Convert a hexl format buffer to binary.
|
|
273 Ask the user for confirmation."
|
|
274 (if (y-or-n-p "Convert contents back to binary format? ")
|
|
275 (let ((modified (buffer-modified-p))
|
|
276 (inhibit-read-only t)
|
|
277 (original-point (1+ (hexl-current-address))))
|
|
278 (dehexlify-buffer)
|
|
279 (remove-hook 'write-contents-hooks 'hexl-save-buffer)
|
|
280 (set-buffer-modified-p modified)
|
|
281 (goto-char original-point))))
|
|
282
|
|
283 (defun hexl-current-address (&optional validate)
|
|
284 "Return current hexl-address."
|
|
285 (interactive)
|
|
286 (let ((current-column (- (% (point) 68) 11))
|
|
287 (hexl-address 0))
|
|
288 (if (< current-column 0)
|
|
289 (if validate
|
|
290 (error "Point is not on a character in the file")
|
|
291 (setq current-column 0)))
|
|
292 (setq hexl-address
|
|
293 (+ (* (/ (point) 68) 16)
|
|
294 (if (>= current-column 41)
|
|
295 (- current-column 41)
|
|
296 (/ (- current-column (/ current-column 5)) 2))))
|
|
297 hexl-address))
|
|
298
|
|
299 (defun hexl-address-to-marker (address)
|
|
300 "Return marker for ADDRESS."
|
|
301 (interactive "nAddress: ")
|
|
302 (+ (* (/ address 16) 68) 11 (/ (* (% address 16) 5) 2)))
|
|
303
|
|
304 (defun hexl-goto-address (address)
|
|
305 "Goto hexl-mode (decimal) address ADDRESS.
|
|
306 Signal error if ADDRESS out of range."
|
|
307 (interactive "nAddress: ")
|
|
308 (if (or (< address 0) (> address hexl-max-address))
|
|
309 (error "Out of hexl region."))
|
|
310 (goto-char (hexl-address-to-marker address)))
|
|
311
|
|
312 (defun hexl-goto-hex-address (hex-address)
|
|
313 "Go to hexl-mode address (hex string) HEX-ADDRESS.
|
|
314 Signal error if HEX-ADDRESS is out of range."
|
|
315 (interactive "sHex Address: ")
|
|
316 (hexl-goto-address (hexl-hex-string-to-integer hex-address)))
|
|
317
|
|
318 (defun hexl-hex-string-to-integer (hex-string)
|
|
319 "Return decimal integer for HEX-STRING."
|
|
320 (interactive "sHex number: ")
|
|
321 (let ((hex-num 0))
|
|
322 (while (not (equal hex-string ""))
|
|
323 (setq hex-num (+ (* hex-num 16)
|
|
324 (hexl-hex-char-to-integer (string-to-char hex-string))))
|
|
325 (setq hex-string (substring hex-string 1)))
|
|
326 hex-num))
|
|
327
|
|
328 (defun hexl-octal-string-to-integer (octal-string)
|
|
329 "Return decimal integer for OCTAL-STRING."
|
|
330 (interactive "sOctal number: ")
|
|
331 (let ((oct-num 0))
|
|
332 (while (not (equal octal-string ""))
|
|
333 (setq oct-num (+ (* oct-num 8)
|
|
334 (hexl-oct-char-to-integer
|
|
335 (string-to-char octal-string))))
|
|
336 (setq octal-string (substring octal-string 1)))
|
|
337 oct-num))
|
|
338
|
|
339 ;; move point functions
|
|
340
|
|
341 (defun hexl-backward-char (arg)
|
|
342 "Move to left ARG bytes (right if ARG negative) in hexl-mode."
|
|
343 (interactive "p")
|
|
344 (hexl-goto-address (- (hexl-current-address) arg)))
|
|
345
|
|
346 (defun hexl-forward-char (arg)
|
|
347 "Move right ARG bytes (left if ARG negative) in hexl-mode."
|
|
348 (interactive "p")
|
|
349 (hexl-goto-address (+ (hexl-current-address) arg)))
|
|
350
|
|
351 (defun hexl-backward-short (arg)
|
|
352 "Move to left ARG shorts (right if ARG negative) in hexl-mode."
|
|
353 (interactive "p")
|
|
354 (hexl-goto-address (let ((address (hexl-current-address)))
|
|
355 (if (< arg 0)
|
|
356 (progn
|
|
357 (setq arg (- arg))
|
|
358 (while (> arg 0)
|
|
359 (if (not (equal address (logior address 3)))
|
|
360 (if (> address hexl-max-address)
|
|
361 (progn
|
|
362 (message "End of buffer.")
|
|
363 (setq address hexl-max-address))
|
|
364 (setq address (logior address 3)))
|
|
365 (if (> address hexl-max-address)
|
|
366 (progn
|
|
367 (message "End of buffer.")
|
|
368 (setq address hexl-max-address))
|
|
369 (setq address (+ address 4))))
|
|
370 (setq arg (1- arg)))
|
|
371 (if (> address hexl-max-address)
|
|
372 (progn
|
|
373 (message "End of buffer.")
|
|
374 (setq address hexl-max-address))
|
|
375 (setq address (logior address 3))))
|
|
376 (while (> arg 0)
|
|
377 (if (not (equal address (logand address -4)))
|
|
378 (setq address (logand address -4))
|
|
379 (if (not (equal address 0))
|
|
380 (setq address (- address 4))
|
|
381 (message "Beginning of buffer.")))
|
|
382 (setq arg (1- arg))))
|
|
383 address)))
|
|
384
|
|
385 (defun hexl-forward-short (arg)
|
|
386 "Move right ARG shorts (left if ARG negative) in hexl-mode."
|
|
387 (interactive "p")
|
|
388 (hexl-backward-short (- arg)))
|
|
389
|
|
390 (defun hexl-backward-word (arg)
|
|
391 "Move to left ARG words (right if ARG negative) in hexl-mode."
|
|
392 (interactive "p")
|
|
393 (hexl-goto-address (let ((address (hexl-current-address)))
|
|
394 (if (< arg 0)
|
|
395 (progn
|
|
396 (setq arg (- arg))
|
|
397 (while (> arg 0)
|
|
398 (if (not (equal address (logior address 7)))
|
|
399 (if (> address hexl-max-address)
|
|
400 (progn
|
|
401 (message "End of buffer.")
|
|
402 (setq address hexl-max-address))
|
|
403 (setq address (logior address 7)))
|
|
404 (if (> address hexl-max-address)
|
|
405 (progn
|
|
406 (message "End of buffer.")
|
|
407 (setq address hexl-max-address))
|
|
408 (setq address (+ address 8))))
|
|
409 (setq arg (1- arg)))
|
|
410 (if (> address hexl-max-address)
|
|
411 (progn
|
|
412 (message "End of buffer.")
|
|
413 (setq address hexl-max-address))
|
|
414 (setq address (logior address 7))))
|
|
415 (while (> arg 0)
|
|
416 (if (not (equal address (logand address -8)))
|
|
417 (setq address (logand address -8))
|
|
418 (if (not (equal address 0))
|
|
419 (setq address (- address 8))
|
|
420 (message "Beginning of buffer.")))
|
|
421 (setq arg (1- arg))))
|
|
422 address)))
|
|
423
|
|
424 (defun hexl-forward-word (arg)
|
|
425 "Move right ARG words (left if ARG negative) in hexl-mode."
|
|
426 (interactive "p")
|
|
427 (hexl-backward-word (- arg)))
|
|
428
|
|
429 (defun hexl-previous-line (arg)
|
|
430 "Move vertically up ARG lines [16 bytes] (down if ARG negative) in hexl-mode.
|
|
431 If there is byte at the target address move to the last byte in that line."
|
|
432 (interactive "p")
|
|
433 (hexl-next-line (- arg)))
|
|
434
|
|
435 (defun hexl-next-line (arg)
|
|
436 "Move vertically down ARG lines [16 bytes] (up if ARG negative) in hexl-mode.
|
|
437 If there is no byte at the target address move to the last byte in that line."
|
|
438 (interactive "p")
|
|
439 (hexl-goto-address (let ((address (+ (hexl-current-address) (* arg 16))))
|
|
440 (if (and (< arg 0) (< address 0))
|
|
441 (progn (message "Out of hexl region.")
|
|
442 (setq address
|
|
443 (% (hexl-current-address) 16)))
|
|
444 (if (and (> address hexl-max-address)
|
|
445 (< (% hexl-max-address 16) (% address 16)))
|
|
446 (setq address hexl-max-address)
|
|
447 (if (> address hexl-max-address)
|
|
448 (progn (message "Out of hexl region.")
|
|
449 (setq
|
|
450 address
|
|
451 (+ (logand hexl-max-address -16)
|
|
452 (% (hexl-current-address) 16)))))))
|
|
453 address)))
|
|
454
|
|
455 (defun hexl-beginning-of-buffer (arg)
|
|
456 "Move to the beginning of the hexl buffer.
|
|
457 Leaves `hexl-mark' at previous position.
|
|
458 With prefix arg N, puts point N bytes of the way from the true beginning."
|
|
459 (interactive "p")
|
|
460 (push-mark (point))
|
|
461 (hexl-goto-address (+ 0 (1- arg))))
|
|
462
|
|
463 (defun hexl-end-of-buffer (arg)
|
|
464 "Go to `hexl-max-address' minus ARG."
|
|
465 (interactive "p")
|
|
466 (push-mark (point))
|
|
467 (hexl-goto-address (- hexl-max-address (1- arg))))
|
|
468
|
|
469 (defun hexl-beginning-of-line ()
|
|
470 "Goto beginning of line in hexl mode."
|
|
471 (interactive)
|
|
472 (goto-char (+ (* (/ (point) 68) 68) 11)))
|
|
473
|
|
474 (defun hexl-end-of-line ()
|
|
475 "Goto end of line in hexl mode."
|
|
476 (interactive)
|
|
477 (hexl-goto-address (let ((address (logior (hexl-current-address) 15)))
|
|
478 (if (> address hexl-max-address)
|
|
479 (setq address hexl-max-address))
|
|
480 address)))
|
|
481
|
|
482 (defun hexl-scroll-down (arg)
|
|
483 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
|
|
484 (interactive "P")
|
|
485 (if (null arg)
|
|
486 (setq arg (1- (window-height)))
|
|
487 (setq arg (prefix-numeric-value arg)))
|
|
488 (hexl-scroll-up (- arg)))
|
|
489
|
|
490 (defun hexl-scroll-up (arg)
|
|
491 "Scroll hexl buffer window upward ARG lines; or near full window if no ARG."
|
|
492 (interactive "P")
|
|
493 (if (null arg)
|
|
494 (setq arg (1- (window-height)))
|
|
495 (setq arg (prefix-numeric-value arg)))
|
|
496 (let ((movement (* arg 16))
|
|
497 (address (hexl-current-address)))
|
|
498 (if (or (> (+ address movement) hexl-max-address)
|
|
499 (< (+ address movement) 0))
|
|
500 (message "Out of hexl region.")
|
|
501 (hexl-goto-address (+ address movement))
|
|
502 (recenter 0))))
|
|
503
|
|
504 (defun hexl-beginning-of-1k-page ()
|
|
505 "Goto to beginning of 1k boundry."
|
|
506 (interactive)
|
|
507 (hexl-goto-address (logand (hexl-current-address) -1024)))
|
|
508
|
|
509 (defun hexl-end-of-1k-page ()
|
|
510 "Goto to end of 1k boundry."
|
|
511 (interactive)
|
|
512 (hexl-goto-address (let ((address (logior (hexl-current-address) 1023)))
|
|
513 (if (> address hexl-max-address)
|
|
514 (setq address hexl-max-address))
|
|
515 address)))
|
|
516
|
|
517 (defun hexl-beginning-of-512b-page ()
|
|
518 "Goto to beginning of 512 byte boundry."
|
|
519 (interactive)
|
|
520 (hexl-goto-address (logand (hexl-current-address) -512)))
|
|
521
|
|
522 (defun hexl-end-of-512b-page ()
|
|
523 "Goto to end of 512 byte boundry."
|
|
524 (interactive)
|
|
525 (hexl-goto-address (let ((address (logior (hexl-current-address) 511)))
|
|
526 (if (> address hexl-max-address)
|
|
527 (setq address hexl-max-address))
|
|
528 address)))
|
|
529
|
|
530 (defun hexl-quoted-insert (arg)
|
|
531 "Read next input character and insert it.
|
|
532 Useful for inserting control characters.
|
|
533 You may also type up to 3 octal digits, to insert a character with that code"
|
|
534 (interactive "p")
|
|
535 (hexl-insert-char (read-quoted-char) arg))
|
|
536
|
|
537 ;00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789ABCDEF
|
|
538
|
|
539 (defun hexlify-buffer ()
|
|
540 "Convert a binary buffer to hexl format."
|
|
541 (interactive)
|
|
542 (let ((binary-process-output nil) ; for Ms-Dos
|
|
543 (binary-process-input t))
|
|
544 (shell-command-on-region (point-min) (point-max) hexlify-command t)))
|
|
545
|
|
546 (defun dehexlify-buffer ()
|
|
547 "Convert a hexl format buffer to binary."
|
|
548 (interactive)
|
|
549 (let ((binary-process-output t) ; for Ms-Dos
|
|
550 (binary-process-input nil))
|
|
551 (shell-command-on-region (point-min) (point-max) dehexlify-command t)))
|
|
552
|
|
553 (defun hexl-char-after-point ()
|
|
554 "Return char for ASCII hex digits at point."
|
|
555 (hexl-htoi (char-after (point))
|
|
556 (char-after (1+ (point)))))
|
|
557
|
|
558 (defun hexl-htoi (lh rh)
|
|
559 "Hex (char) LH (char) RH to integer."
|
|
560 (+ (* (hexl-hex-char-to-integer lh) 16)
|
|
561 (hexl-hex-char-to-integer rh)))
|
|
562
|
|
563 (defun hexl-hex-char-to-integer (character)
|
|
564 "Take a char and return its value as if it was a hex digit."
|
|
565 (if (and (>= character ?0) (<= character ?9))
|
|
566 (- character ?0)
|
|
567 (let ((ch (logior character 32)))
|
|
568 (if (and (>= ch ?a) (<= ch ?f))
|
|
569 (- ch (- ?a 10))
|
|
570 (error (format "Invalid hex digit `%c'." ch))))))
|
|
571
|
|
572 (defun hexl-oct-char-to-integer (character)
|
|
573 "Take a char and return its value as if it was a octal digit."
|
|
574 (if (and (>= character ?0) (<= character ?7))
|
|
575 (- character ?0)
|
|
576 (error (format "Invalid octal digit `%c'." character))))
|
|
577
|
|
578 (defun hexl-printable-character (ch)
|
|
579 "Return a displayable string for character CH."
|
|
580 (format "%c" (if hexl-iso
|
|
581 (if (or (< ch 32) (and (>= ch 127) (< ch 160)))
|
|
582 46
|
|
583 ch)
|
|
584 (if (or (< ch 32) (>= ch 127))
|
|
585 46
|
|
586 ch))))
|
|
587
|
|
588 (defun hexl-self-insert-command (arg)
|
|
589 "Insert this character."
|
|
590 (interactive "p")
|
|
591 (hexl-insert-char last-command-char arg))
|
|
592
|
|
593 (defun hexl-insert-char (ch num)
|
|
594 "Insert a character in a hexl buffer."
|
|
595 (let ((address (hexl-current-address t)))
|
|
596 (while (> num 0)
|
|
597 (let ((hex-position
|
|
598 (+ (* (/ address 16) 68)
|
|
599 11
|
|
600 (* 2 (% address 16))
|
|
601 (/ (% address 16) 2)))
|
|
602 (ascii-position
|
|
603 (+ (* (/ address 16) 68) 52 (% address 16)))
|
|
604 at-ascii-position)
|
|
605 (if (= (point) ascii-position)
|
|
606 (setq at-ascii-position t))
|
|
607 (goto-char hex-position)
|
|
608 (delete-char 2)
|
|
609 (insert (format "%02x" ch))
|
|
610 (goto-char ascii-position)
|
|
611 (delete-char 1)
|
|
612 (insert (hexl-printable-character ch))
|
|
613 (or (eq address hexl-max-address)
|
|
614 (setq address (1+ address)))
|
|
615 (hexl-goto-address address)
|
|
616 (if at-ascii-position
|
|
617 (progn
|
|
618 (beginning-of-line)
|
|
619 (forward-char 51)
|
|
620 (forward-char (% address 16)))))
|
|
621 (setq num (1- num)))))
|
|
622
|
|
623 ;; hex conversion
|
|
624
|
|
625 (defun hexl-insert-hex-char (arg)
|
|
626 "Insert a ASCII char ARG times at point for a given hexadecimal number."
|
|
627 (interactive "p")
|
|
628 (let ((num (hexl-hex-string-to-integer (read-string "Hex number: "))))
|
|
629 (if (or (> num 255) (< num 0))
|
|
630 (error "Hex number out of range.")
|
|
631 (hexl-insert-char num arg))))
|
|
632
|
|
633 (defun hexl-insert-decimal-char (arg)
|
|
634 "Insert a ASCII char ARG times at point for a given decimal number."
|
|
635 (interactive "p")
|
|
636 (let ((num (string-to-int (read-string "Decimal Number: "))))
|
|
637 (if (or (> num 255) (< num 0))
|
|
638 (error "Decimal number out of range.")
|
|
639 (hexl-insert-char num arg))))
|
|
640
|
|
641 (defun hexl-insert-octal-char (arg)
|
|
642 "Insert a ASCII char ARG times at point for a given octal number."
|
|
643 (interactive "p")
|
|
644 (let ((num (hexl-octal-string-to-integer (read-string "Octal Number: "))))
|
|
645 (if (or (> num 255) (< num 0))
|
|
646 (error "Decimal number out of range.")
|
|
647 (hexl-insert-char num arg))))
|
|
648
|
|
649 ;; startup stuff.
|
|
650
|
|
651 (if hexl-mode-map
|
|
652 nil
|
|
653 (setq hexl-mode-map (make-sparse-keymap))
|
|
654 (set-keymap-name hexl-mode-map 'hexl-mode-map)
|
|
655
|
|
656 (define-key hexl-mode-map 'left 'hexl-backward-char)
|
|
657 (define-key hexl-mode-map 'right 'hexl-forward-char)
|
|
658 (define-key hexl-mode-map 'up 'hexl-previous-line)
|
|
659 (define-key hexl-mode-map 'down 'hexl-next-line)
|
|
660 (define-key hexl-mode-map '(meta left) 'hexl-backward-short)
|
|
661 (define-key hexl-mode-map '(meta right) 'hexl-forward-short)
|
|
662 (define-key hexl-mode-map 'next 'hexl-scroll-up)
|
|
663 (define-key hexl-mode-map 'prior 'hexl-scroll-down)
|
|
664
|
|
665 (define-key hexl-mode-map "\C-a" 'hexl-beginning-of-line)
|
|
666 (define-key hexl-mode-map "\C-b" 'hexl-backward-char)
|
|
667 (define-key hexl-mode-map "\C-d" 'undefined)
|
|
668 (define-key hexl-mode-map "\C-e" 'hexl-end-of-line)
|
|
669 (define-key hexl-mode-map "\C-f" 'hexl-forward-char)
|
|
670
|
|
671 (if (not (eq (key-binding (char-to-string help-char)) 'help-command))
|
|
672 (define-key hexl-mode-map (char-to-string help-char) 'undefined))
|
|
673
|
|
674 (define-key hexl-mode-map "\C-i" 'hexl-self-insert-command)
|
|
675 (define-key hexl-mode-map "\C-j" 'hexl-self-insert-command)
|
|
676 (define-key hexl-mode-map "\C-k" 'undefined)
|
|
677 (define-key hexl-mode-map "\C-m" 'hexl-self-insert-command)
|
|
678 (define-key hexl-mode-map "\C-n" 'hexl-next-line)
|
|
679 (define-key hexl-mode-map "\C-o" 'undefined)
|
|
680 (define-key hexl-mode-map "\C-p" 'hexl-previous-line)
|
|
681 (define-key hexl-mode-map "\C-q" 'hexl-quoted-insert)
|
|
682 (define-key hexl-mode-map "\C-t" 'undefined)
|
|
683 (define-key hexl-mode-map "\C-v" 'hexl-scroll-up)
|
|
684 (define-key hexl-mode-map "\C-w" 'undefined)
|
|
685 (define-key hexl-mode-map "\C-y" 'undefined)
|
|
686
|
|
687 (let ((ch 32))
|
|
688 (while (< ch 127)
|
|
689 (define-key hexl-mode-map (char-to-string ch) 'hexl-self-insert-command)
|
|
690 (setq ch (1+ ch))))
|
|
691
|
|
692 (define-key hexl-mode-map "\e\C-a" 'hexl-beginning-of-512b-page)
|
|
693 (define-key hexl-mode-map "\e\C-b" 'hexl-backward-short)
|
|
694 (define-key hexl-mode-map "\e\C-c" 'undefined)
|
|
695 (define-key hexl-mode-map "\e\C-d" 'hexl-insert-decimal-char)
|
|
696 (define-key hexl-mode-map "\e\C-e" 'hexl-end-of-512b-page)
|
|
697 (define-key hexl-mode-map "\e\C-f" 'hexl-forward-short)
|
|
698 (define-key hexl-mode-map "\e\C-g" 'undefined)
|
|
699 (define-key hexl-mode-map "\e\C-h" 'undefined)
|
|
700 (define-key hexl-mode-map "\e\C-i" 'undefined)
|
|
701 (define-key hexl-mode-map "\e\C-j" 'undefined)
|
|
702 (define-key hexl-mode-map "\e\C-k" 'undefined)
|
|
703 (define-key hexl-mode-map "\e\C-l" 'undefined)
|
|
704 (define-key hexl-mode-map "\e\C-m" 'undefined)
|
|
705 (define-key hexl-mode-map "\e\C-n" 'undefined)
|
|
706 (define-key hexl-mode-map "\e\C-o" 'hexl-insert-octal-char)
|
|
707 (define-key hexl-mode-map "\e\C-p" 'undefined)
|
|
708 (define-key hexl-mode-map "\e\C-q" 'undefined)
|
|
709 (define-key hexl-mode-map "\e\C-r" 'undefined)
|
|
710 (define-key hexl-mode-map "\e\C-s" 'undefined)
|
|
711 (define-key hexl-mode-map "\e\C-t" 'undefined)
|
|
712 (define-key hexl-mode-map "\e\C-u" 'undefined)
|
|
713
|
|
714 (define-key hexl-mode-map "\e\C-w" 'undefined)
|
|
715 (define-key hexl-mode-map "\e\C-x" 'hexl-insert-hex-char)
|
|
716 (define-key hexl-mode-map "\e\C-y" 'undefined)
|
|
717
|
|
718 (define-key hexl-mode-map "\ea" 'undefined) ;hexl-beginning-of-1k-page
|
|
719 (define-key hexl-mode-map "\eb" 'hexl-backward-word)
|
|
720 (define-key hexl-mode-map "\ec" 'undefined)
|
|
721 (define-key hexl-mode-map "\ed" 'undefined)
|
|
722 (define-key hexl-mode-map "\ee" 'undefined) ;hexl-end-of-1k-page
|
|
723 (define-key hexl-mode-map "\ef" 'hexl-forward-word)
|
|
724 (define-key hexl-mode-map "\eg" 'hexl-goto-hex-address)
|
|
725 (define-key hexl-mode-map "\eh" 'undefined)
|
|
726 (define-key hexl-mode-map "\ei" 'undefined)
|
|
727 (define-key hexl-mode-map "\ej" 'hexl-goto-address)
|
|
728 (define-key hexl-mode-map "\ek" 'undefined)
|
|
729 (define-key hexl-mode-map "\el" 'undefined)
|
|
730 (define-key hexl-mode-map "\em" 'undefined)
|
|
731 (define-key hexl-mode-map "\en" 'undefined)
|
|
732 (define-key hexl-mode-map "\eo" 'undefined)
|
|
733 (define-key hexl-mode-map "\ep" 'undefined)
|
|
734 (define-key hexl-mode-map "\eq" 'undefined)
|
|
735 (define-key hexl-mode-map "\er" 'undefined)
|
|
736 (define-key hexl-mode-map "\es" 'undefined)
|
|
737 (define-key hexl-mode-map "\et" 'undefined)
|
|
738 (define-key hexl-mode-map "\eu" 'undefined)
|
|
739 (define-key hexl-mode-map "\ev" 'hexl-scroll-down)
|
|
740 (define-key hexl-mode-map "\ey" 'undefined)
|
|
741 (define-key hexl-mode-map "\ez" 'undefined)
|
|
742 (define-key hexl-mode-map "\e<" 'hexl-beginning-of-buffer)
|
|
743 (define-key hexl-mode-map "\e>" 'hexl-end-of-buffer)
|
|
744
|
|
745 (define-key hexl-mode-map "\C-c\C-c" 'hexl-mode-exit)
|
|
746
|
|
747 (define-key hexl-mode-map "\C-x[" 'hexl-beginning-of-1k-page)
|
|
748 (define-key hexl-mode-map "\C-x]" 'hexl-end-of-1k-page)
|
|
749 (define-key hexl-mode-map "\C-x\C-p" 'undefined)
|
|
750 (define-key hexl-mode-map "\C-x\C-s" 'hexl-save-buffer)
|
|
751 (define-key hexl-mode-map "\C-x\C-t" 'undefined))
|
|
752
|
|
753 ;;; hexl.el ends here
|