In emacs, is there a way to Hex edit/view a buffer without losing the undo info

binaryemacshex

When I use hexl-mode in emacs, it discards the buffer's undo info.
Is there another way to hex-edit the buffer and yet retain the undo info?

If there is no alternative hex-editor, is there a hex-viewer which can view the buffer? I have tried hexview-mode, but it doesn't view the actual buffer; it views the buffer's file (from disk), so I don't see the current edit.

Best Answer

Here is a function which copies the contents of the current buffer into a new buffer and runs hexl-mode on that:

(defun hexify-buffer-copy()
    "Edit current buffer in hexl mode by copying it"
    (let ((orig-buffer (current-buffer)))
         (switch-to-buffer (create-file-buffer
             (buffer-file-name orig-buffer)))
         (insert-buffer orig-buffer)
         (setq buffer-undo-list nil)
         (hexl-mode)))

Run with M-: (hexify-buffer-copy)