unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: ruler support in hexl mode
Date: 08 Mar 2004 15:05:50 -0500	[thread overview]
Message-ID: <jwv65dfxi5i.fsf-monnier+emacs@asado.iro.umontreal.ca> (raw)
In-Reply-To: <20040305.142915.63122255.jet@gyve.org>

> +(defcustom hexl-follow-line t
> +  "If non-nil then highlight the line address corresponding to point."
> +  :type 'boolean
> +  :group 'hexl)

Any reason why you do not simply use hl-line-mode ?
Or, better yet, let the user select hl-line-mode if he wants it?

> +(defcustom hexl-use-face-on-address-area t
> +  "If non-nil then put `hexl-address-area-face' on adderss area of hexl-mode buffer."
> +  :type 'face
> +  :group 'hexl)
> +(defcustom hexl-use-face-on-ascii-area t
> +  "If non-nil then put `hexl-ascii-area-face' on ascii area of hexl-mode buffer."
> +  :type 'face
> +  :group 'hexl)

You can probably just remove these variables and place the faces on the
`font-lock-face' property, so the user can use M-x global-font-lock-mode
to turn highlighting on/off.

> @@ -89,11 +118,20 @@
>  (defvar hexl-mode-old-isearch-search-fun-function)
>  (defvar hexl-mode-old-require-final-newline)
>  (defvar hexl-mode-old-syntax-table)
> +(defvar hexl-mode-old-header-line-format)
 
>  (defvar hexl-ascii-overlay nil
>    "Overlay used to highlight ASCII element corresponding to current point.")
>  (make-variable-buffer-local 'hexl-ascii-overlay)
 
> +(defvar hexl-line-overlay nil
> +  "Overlay used to highlight the address of line corresponding to current point.")
> +(make-variable-buffer-local 'hexl-line-overlay)
> +
> +(defconst hexl-mode-header-line-format
> +  '(:eval (hexl-mode-ruler))
> +  "`header-line-format' used in hexl mode.")
> +
>  ;; routines
 
>  (put 'hexl-mode 'mode-class 'special)
> @@ -245,8 +283,13 @@
>      (eldoc-remove-command "hexl-save-buffer" 
>  			  "hexl-current-address")
 
> -    (if hexl-follow-ascii (hexl-follow-ascii 1)))
> -  (run-hooks 'hexl-mode-hook))
> +    (make-variable-buffer-local 'hexl-mode-old-header-line-format)
> +    (setq hexl-mode-old-header-line-format header-line-format)

That should be
   (set (make-local-variable 'hexl-mode-old-header-line-format) header-line-format)
`make-variable-buffer-local' should almost never be called from inside
a function.

> +	(while (re-search-forward "^[0-9a-f]\\{8\\}:" nil t)

I'd use `+' rather than `\\{8\\}' because it's shorter, easier to read,
more efficient, and most importantly future-proof (for very large files).

> +	  (put-text-property (match-beginning 0) (match-end 0)
> +			     'face 'hexl-address-area-face)))
> +      (goto-char (point-min))
> +      (when hexl-use-face-on-ascii-area
> +	(while (re-search-forward " \\( .+$\\)" nil t)
> +	  (put-text-property (match-beginning 1) (match-end 1)
> +			     'face 'hexl-ascii-area-face))))

Why do you put the face property on the space before the actual "ascii" data?

> +;; This function is derived from `ruler-mode-ruler' in ruler-mode.el.
> +(defun hexl-mode-ruler ()
> +  "Return a string ruler for hexl mode."
> +  (when hexl-use-ruler
> +    (let* ((fullw (ruler-mode-full-window-width))
> +	   (w     (window-width))
> +	   (m     (window-margins))
> +	   (lsb   (ruler-mode-left-scroll-bar-cols))
> +	   (lf    (ruler-mode-left-fringe-cols))
> +	   (lm    (or (car m) 0))
> +	   (ruler (make-string fullw ?\ ))

We really need to move this out of ruler-mode into frame.el or some other
"global" file.  And to give it a clean interface so its implementation can
be improved later.

> +	   (o     (+ lsb lf lm))
> +	   (x o)
> +	   (highlight (mod (hexl-current-address) 16)))
> +      ;; "87654321"
> +      (do ((i 8 (1- i)))
> +	  ((= i 0))
> +	(aset ruler x (aref (number-to-string i) 0))
> +	(setq x (1+ x)))
> +      ;; "87654321  "
> +      (setq x (+ x 2))			; ": "
> +      ;; "87654321  0011 2233 4455 6677 8899 aabb ccdd eeff"
> +      (do* ((i 0 (1+ i))
> +	    (c (format "%x" i) (format "%x" i)))
> +	  ((= i 16))
> +	(aset ruler x (aref c 0))
> +	(setq x (1+ x))
> +	(aset ruler x (aref c 0))
> +	(setq x (1+ x))
> +	(if (= highlight i)
> +	    (put-text-property (- x 2) x 
> +			       'face 'highlight
> +			       ruler))
> +	(when (= (mod i 2) 1) 
> +	  (aset ruler x ?\ )
> +	  (setq x (1+ x))))
> +      ;; "87654321  0011 2233 4455 6677 8899 aabb ccdd eeff "
> +      (setq x (1+ x))			; " "
> +      ;; "87654321  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789abcdef"
> +      (do* ((i 0 (1+ i))
> +	    (c (format "%x" i) (format "%x" i)))
> +	  ((= i 16))
> +	(aset ruler x (aref c 0))
> +	(setq x (1+ x))
> +	(if (= highlight i)
> +	    (put-text-property (1- x) x 
> +			       'face 'highlight
> +			       ruler)))
> +      ruler)))

Isn't this always building the exact same string, except for the size
(which really does not need to depend on the window's width), the leading
space (to align it), and the highlighting of the current column?

Couldn't we just do (100% untested code, inspired from buff-menu.el):

  (let ((spaces (+ lsb lf lm))
        (s "87654321  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789abcdef")
        (pos 0))
    ;; 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 ,(+ spaces pos))
                         s))
    ;; Highlight the current column.
    (put-text-property (+ 10 (/ (* 5 highlight) 2))
                       (+ 12 (/ (* 5 highlight) 2))
                       'face 'highlight s)
    ;; Add the leading space.
    (concat (propertize (make-string (floor spaces) ? )
                        'display `(space :width ,spaces))
            s))

Note that the use of the display property also allows us to deal with
fractional sizes, although lsb and lf don't take advantage of it yet.
 
Also I wish we could put some more useful info up there, but admittedly,
I can't think of any.


        Stefan

  reply	other threads:[~2004-03-08 20:05 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-05  5:29 ruler support in hexl mode Masatake YAMATO
2004-03-08 20:05 ` Stefan Monnier [this message]
2004-03-09 12:11   ` Masatake YAMATO
2004-03-11  6:59   ` Masatake YAMATO
2004-03-11 16:27   ` Kim F. Storm
2004-03-11 17:43     ` Stefan Monnier
2004-03-11 23:56       ` Kim F. Storm
2004-03-12  6:05         ` Masatake YAMATO
2004-03-12 21:24           ` Stefan Monnier
2004-03-13 18:13             ` Masatake YAMATO
2004-03-15  7:37               ` Masatake YAMATO
2004-03-15  4:55       ` Richard Stallman
2004-03-15 11:00         ` Kim F. Storm
2004-03-16 19:02           ` Richard Stallman
2004-03-17  0:08             ` Kim F. Storm
2004-03-17  0:42               ` Stefan Monnier
2004-03-17  2:23               ` Kim F. Storm
2004-03-19  5:01               ` Richard Stallman
2004-03-19 10:06                 ` Kim F. Storm
2004-03-19 13:33                   ` Kim F. Storm
2004-03-08 21:00 ` Miles Bader
2004-03-11 14:41   ` Juanma Barranquero
2004-03-17  3:59     ` Miles Bader
2004-03-18  0:53       ` Juanma Barranquero
2004-03-20  4:48         ` Richard Stallman
2004-03-22 11:52           ` Juanma Barranquero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv65dfxi5i.fsf-monnier+emacs@asado.iro.umontreal.ca \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).