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.

forthrin
  • 451
  • 2
  • 10

2 Answers2

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
netawater
  • 245
  • 1
  • 6
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")
Stefan
  • 26,154
  • 3
  • 46
  • 84
  • Assuming that works I think this, along with an octal variant, would make fine enhancements to `hexl.el` – phils Apr 11 '18 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 Apr 12 '18 at 06: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 Apr 12 '18 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 Apr 12 '18 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 Apr 12 '18 at 15:40
  • @Stefan: Wow! The cogwheels really got going here! I'll check it as soon as it's out. – forthrin Apr 12 '18 at 15:57
  • @forthrin: This is not the place to discuss nhexl-mode. Please just send me email directly. – Stefan Apr 16 '18 at 12:26