4

In hexl-mode how can I make Emacs enter a state where it takes everything I enter as hex input until I tell it to stop? Eg. I want to tap "A", "B", "1", "0", etc., and have this entered as $AB, $10, etc. in the binary file, be able to cursor around, enter more hex values, and so forth, until I'm satisfied.

By default I have to press C-M-x for every hex value I want to enter, which is very counterproductive at least for the type of binary file I'm working with at the moment.

2 Answers 2

2

hexl-insert-hex-string can enter a hex string in a hexl-mode buffer, but the string length cannot be greater than the file size.

1

You could try something like (guaranteed 100% untested):

(defvar hexl-hex-insert-mode-map
  (let ((map (make-sparse-keymap)))
    (mapc (lambda (c)
            (define-key map (vector c) 'hexl-hex-self-insert))
          "0123456789abcdef")
    map))

(defvar hexl-hex--last-char nil)

(defun hexl-hex-self-insert ()
  (interactive)
  "Self-insert \"nibble\"."
  (if (not (and hexl-hex--last-char
                (eq this-command last-command)))
      (setq hexl-hex--last-char last-command-event)
    (hexl-insert-multibyte-char
     (hexl-hex-string-to-integer
      (string hexl-hex--last-char last-command-event))
     1)
    (setq hexl-hex--last-char nil)))

(define-minor-mode hexl-hex-insert-mode
  "Hex insertion mode."
  :lighter " HI")
7
  • Assuming that works I think this, along with an octal variant, would make fine enhancements to hexl.el
    – phils
    Commented Apr 11, 2018 at 22:30
  • Seems to work! It feels a bit awkward that nothing prints until you've entered both nibbles. Can this be helped? Example: Enter first nibble: Set and printed; Cursor highlights second nibble. A) User enters second nibble: Set and printed; Cursor moves to next byte. B) If user moves cursor instead of entering second nibble: Cursor simply moves to first nibble of cursor destination. PS! One could also discuss whether general cursor navigation always should move in nibbles. I'm used to this from low level environments, but the current behaviour (moving in bytes) isn't a major nuisance.
    – forthrin
    Commented Apr 12, 2018 at 6:59
  • @forthrin: There's clearly room for improvement, indeed. I think that changing the code to move by nibbles and update nibbles directly would make it much better.
    – Stefan
    Commented Apr 12, 2018 at 12:02
  • @Stefan: Glad to hear you agree. It could be a setting (hexl-mode-nibble-navigation). At least now, I can manipulate hex data like a breeze. Would be great if it made it into the core too!
    – forthrin
    Commented Apr 12, 2018 at 12:18
  • @forthrin: Alright, alright, I just added a nhexl-nibble-edit-mode to my nhexl-mode package (available from GNU ELPA, the new version should appear there soon).
    – Stefan
    Commented Apr 12, 2018 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.