1

I sometimes want to look at the hexl-mode view of a text file buffer in one window and in another window I want to keep the original text view.

What's an easy procedure to do so? -- Due to the fact that I cannot easily clone a file buffer, it means I have to create a new buffer, copy the content and then hexl-mode this new buffer. Is there a simpler way?

6
  • It's not hard/long to do, is it? How often do you need to do it? In any case, you could certainly write a simple command to do that. Or even use a keyboard macro.
    – Drew
    Feb 9 at 2:36
  • Although clone-buffer objects when the buffer is visiting a file, I think you could detach the buffer from the file name (with set-visited-file-name), clone it, hexl-mode the clone, go back to the original buffer and reset the file name - maybe... The devil is in the details.
    – NickD
    Feb 9 at 5:08
  • @NickD Your idea is not quite riight: Evem after set-visited-file-name cloning is not allowed. But: I still can use this: 1. Set a new filename, hxl-mode that buffer. Reload the original into its normal buffer. Voila. Slightly easier than what I did before...
    – halloleo
    Feb 9 at 6:51
  • You have to (set-visited-file-name nil) to be able to clone it.
    – NickD
    Feb 9 at 15:07
  • IOW, do M-: (set-visited-file-name nil) ; do not do M-x set-visited-file-name RET nil RET.
    – NickD
    Feb 9 at 16:17

1 Answer 1

2

Is there a simpler way?

No, I think you have that correctly. Something like this, then:

(defun hexl-me-this ()
  "Open a copy of the current buffer in `hexl-mode'."
  (interactive)
  (display-buffer
   (let* ((buf (current-buffer))
          (mode major-mode)
          (coding buffer-file-coding-system)
          (file (or buffer-file-name
                    dired-directory
                    (expand-file-name (buffer-name) default-directory)))
          (tmp (concat temporary-file-directory "hexl" file))
          (hexlbuf (or (get-file-buffer tmp) (create-file-buffer tmp))))
     (with-current-buffer hexlbuf
       (setq buffer-file-name tmp)
       (buffer-disable-undo)
       (erase-buffer)
       (remove-hook 'change-major-mode-hook #'hexl-maybe-dehexlify-buffer t)
       (funcall mode)
       (set-buffer-file-coding-system coding)
       (insert-buffer-substring buf)
       (hexl-mode)
       (set-buffer-modified-p nil))
     hexlbuf)))
3
  • Does make-indirect-buffer not work?
    – rpluim
    Feb 9 at 10:34
  • 1
    It does not. Indirect buffers share their buffer text, and hexl-mode clobbers that text.
    – phils
    Feb 9 at 14:54
  • D'oh. -1 for my reading comprehension today
    – rpluim
    Feb 9 at 15:31

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.