hexl.el 43 KB

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