0

I would like to use the hexl-mode but without the ASCII characters printed on the right side as it is mostly gibberish and not a very helpful feature for me. Is there a way to achieve that?

For example:

00000000: 4672 6564 7269 636b 0000 0000 0000 0000  Fredrick........

Needs to be simply:

00000000: 4672 6564 7269 636b 0000 0000 0000 0000
3
  • Anything to do with hexl-follow-ascii?
    – Nishant
    Feb 21, 2018 at 10:20
  • 1
    No hexl-follow-ascii just controls the highlighting of the character corresponding to the current point-position. I had a look at hexl. There is no option for that workhorse that avoids the ascii string. My first simple approach would be to set the foreground equal to the background in hexl-ascii-region.
    – Tobias
    Feb 21, 2018 at 10:46
  • @Tobias, You can move this an answer?
    – Nishant
    Feb 21, 2018 at 11:04

2 Answers 2

1

The following elisp code defines a new minor mode hexl-bin-mode that replaces the ascii-region with spaces. That should avoid side-effects caused by faces as they may appear in my first answer.

You can switch on the binary hexl mode by M-x hexl-bin-mode or bind the command hexl-bin-mode to any key you want with local-set-key in hexl-mode-hook.

(require 'hexl)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Make hexlify-buffer more versatile by adding a hook.
(defvar hexlify-hook nil
  "Hook run after `hexlify-buffer'.")

(defun hexlify-hook (&rest _args) ;; For the case that the argument list of `hexlify-buffer' changes in future.
  "Run functions registered at `hexlify-hook'."
  (run-hooks 'hexlify-hook))

(advice-add 'hexlify-buffer :after #'hexlify-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Define minor-mode for supporting `hexlify-remove-ascii-region'.

(defun hexlify-remove-ascii-region (&rest _args)
  "Remove ASCII region generated by `hexlify-buffer'."
  ;; We cannot use `delete-rectangle' because there may be incomplete lines.
  (save-excursion
    (goto-char (point-min))
    (let ((start (1- (hexl-ascii-start-column))))
      (while
          (progn
            (forward-char start) 
            (replace-region (point) (line-end-position)
                            (make-string (- (line-end-position) (point)) ?\s))
            (and (eq (forward-line) 0)
                 (null (eobp))))))))

(defun hexl-refresh ()
  "Refresh hexl buffer after manipulation of ascii-region"
  (let ((pt (point))
        buffer-undo-list) ; prevents query of `dehexlify-buffer'
    (dehexlify-buffer)
    (hexlify-buffer)
    (goto-char pt)))

(define-minor-mode hexl-bin-mode
  "Don't show ascii region."
  nil " B" nil
  (if hexl-bin-mode
      (progn
        (cl-assert (derived-mode-p 'hexl-mode) nil
                   "hexl-bin-mode only available for hexl-mode")
        (add-hook 'hexlify-hook #'hexlify-remove-ascii-region nil t)
        (hexl-refresh))
    (remove-hook 'hexlify-hook #'hexlify-remove-ascii-region t)
    (when (derived-mode-p 'hexl-mode)
      (hexl-refresh))))

(defun hexl-insert-char-ignore (oldfun ch num)
  "Ignore `hexl-insert-char' in `hexl-bin-mode'."
  (if hexl-bin-mode
      (let ((address (hexl-current-address t)))
        (while (> num 0)
          (let ((hex-position (hexl-address-to-marker address))
                (ascii-position
                 (+ (* (/ address 16) (hexl-line-displen))
                    (hexl-ascii-start-column)
                    (point-min)
                    (% address 16)))
                at-ascii-position)
            (if (= (point) ascii-position)
                (setq at-ascii-position t))
            (goto-char hex-position)
            (delete-char 2)
            (insert (format "%02x" ch))
            (setq num (1- num)))))
    (apply oldfun args)))

(advice-add 'hexl-insert-char :around #'hexl-insert-char-ignore)
1
  • Thanks @Tobias, I will check it out tomorrow and update you. I am not in front of my Emacs now.
    – Nishant
    Feb 22, 2018 at 16:42
2

One simple way to hide the ascii-part of the hexl buffer is customizing the foreground of the face hexl-ascii-region to the background value (e.g., white).

For using this method make sure that font-lock-mode is active in the hexl-mode. It may be that you need

(add-hook 'hexl-mode-hook #'font-lock-mode)

in your init-file.

2
  • That way the ASCII-region will still be marked, if multiple lines are selected, though
    – kai-dj
    Feb 21, 2018 at 13:12
  • @kai-dj Yes. I know. Now you can decide whether this is a bug or a feature;-).
    – Tobias
    Feb 21, 2018 at 13:13

Your Answer

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

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