hexl.el 41 KB

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