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