+ (if (< num 0)
+ (error "Decimal number out of range")
+ (hexl-insert-multibyte-char num arg))))
+
+(defun hexl-follow-ascii (&optional arg)
+ "Toggle following ASCII in Hexl buffers.
+With prefix ARG, turn on following if and only if ARG is positive.
+When following is enabled, the ASCII character corresponding to the
+element under the point is highlighted.
+Customize the variable `hexl-follow-ascii' to disable this feature."
+ (interactive "P")
+ (let ((on-p (if arg
+ (> (prefix-numeric-value arg) 0)
+ (not hexl-ascii-overlay))))
+
+ (if on-p
+ ;; turn it on
+ (if (not hexl-ascii-overlay)
+ (progn
+ (setq hexl-ascii-overlay (make-overlay 1 1)
+ hexl-follow-ascii t)
+ (overlay-put hexl-ascii-overlay 'face 'highlight)
+ (add-hook 'post-command-hook 'hexl-follow-ascii-find nil t)))
+ ;; turn it off
+ (if hexl-ascii-overlay
+ (progn
+ (delete-overlay hexl-ascii-overlay)
+ (setq hexl-ascii-overlay nil
+ hexl-follow-ascii nil)
+ (remove-hook 'post-command-hook 'hexl-follow-ascii-find t)
+ )))))
+
+(defun hexl-activate-ruler ()
+ "Activate `ruler-mode'"
+ (require 'ruler-mode)
+ (set (make-local-variable 'ruler-mode-ruler-function)
+ 'hexl-mode-ruler)
+ (ruler-mode 1))
+
+(defun hexl-follow-line ()
+ "Activate `hl-line-mode'"
+ (require 'frame)
+ (require 'hl-line)
+ (set (make-local-variable 'hl-line-range-function)
+ 'hexl-highlight-line-range)
+ (set (make-local-variable 'hl-line-face)
+ 'highlight)
+ (hl-line-mode 1))
+
+(defun hexl-highlight-line-range ()
+ "Return the range of address area for the point.
+This function is assumed to be used as call back function for `hl-line-mode'."
+ (cons
+ (line-beginning-position)
+ ;; 9 stands for (length "87654321:")
+ (+ (line-beginning-position) 9)))
+
+(defun hexl-follow-ascii-find ()
+ "Find and highlight the ASCII element corresponding to current point."
+ (let ((pos (+ 51
+ (- (point) (current-column))
+ (mod (hexl-current-address) 16))))
+ (move-overlay hexl-ascii-overlay pos (1+ pos))
+ ))
+
+(defun hexl-mode-ruler ()
+ "Return a string ruler for hexl mode."
+ (let* ((highlight (mod (hexl-current-address) 16))
+ (s " 87654321 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789abcdef")
+ (pos 0))
+ (set-text-properties 0 (length s) nil s)
+ ;; Turn spaces in the header into stretch specs so they work
+ ;; regardless of the header-line face.
+ (while (string-match "[ \t]+" s pos)
+ (setq pos (match-end 0))
+ (put-text-property (match-beginning 0) pos 'display
+ ;; Assume fixed-size chars
+ `(space :align-to ,(1- pos))
+ s))
+ ;; Highlight the current column.
+ (put-text-property (+ 11 (/ (* 5 highlight) 2))
+ (+ 13 (/ (* 5 highlight) 2))
+ 'face 'highlight s)
+ ;; Highlight the current ascii column
+ (put-text-property (+ 13 39 highlight) (+ 13 40 highlight)
+ 'face 'highlight s)
+ s))