all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Masatake YAMATO <jet@gyve.org>
Cc: emacs-devel@gnu.org
Subject: Re: ruler support in hexl mode
Date: Sun, 14 Mar 2004 03:13:12 +0900 (JST)	[thread overview]
Message-ID: <20040314.031312.21594984.jet@gyve.org> (raw)
In-Reply-To: <jwvekrx7owy.fsf-monnier+emacs@asado.iro.umontreal.ca>

> > +(defcustom hexl-use-ruler t
> > +  "If non-nil then show the ruler for hexl mode."
> > +  :type 'boolean
> > +  :group 'hexl)
> 
> I'd call it hexl-use-header-line, but maybe that's just me.

I will use hexl-use-ruler. After thinking I have defined
`ruler-mode-ruler-function' in ruler-mode.el. With the variable, you
can define a mode specific ruler. I have used `ruler-mode-ruler-function'
in hexl-mode.el. As the result I dont have to create a backup of
header format; ruler-mode does it.

> > +(defface hexl-ascii-overlay
> > +  ;; Definition borrowed from vcursor.el.
> > +  '((((class color)) (:foreground "blue" :background "cyan" :underline t))
> > +    (t (:inverse-video t :underline t)))
> > +  "Face for the overlay in ascii area of hexl mode buffer."
> > +  :group 'hexl)
> 
> I'd call it `hexl-ascii-cursor' since the user might not know it's an
> overlay (and it could actually be implemented as a text-property tomorrow).
> Also I'd stick to just `:inverse-video' as much as possible or more
> specifically I'd try to make it look just like the normal cursor (since
> there's conceptually no difference between the two).

You are right. I have changed the code as you wrote.

> You could define this with `define-minor-mode'.
> But I'd recommend to go even further and replace the above with:
> 
> (defcustom hexl-mode-hook ()
>   "Blabla"
>   :type 'hook
>   :options '(hexl-follow-line))
> 
> (defun hexl-follow-line ()
>   (hl-line-mode 1))

Smart. I use this technique both in follow-line and ruler.
I've turned hl-line and ruler on in default.

Summary of changes since last review:
- scroll-bar-columns is moved from frame.el to scroll-bar.el, and
- ruler-mode-current-column-face is used in hexl's ruler, and
- ruler-mode-ruler-function is introduced in ruler-mode.el.

Regards,
Masatake YAMATO

Index: lisp/scroll-bar.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/scroll-bar.el,v
retrieving revision 1.48
diff -u -r1.48 scroll-bar.el
--- lisp/scroll-bar.el	20 Sep 2003 23:33:37 -0000	1.48
+++ lisp/scroll-bar.el	13 Mar 2004 17:58:41 -0000
@@ -54,6 +54,23 @@
   ;; with a large scroll bar portion can easily overflow a lisp int.
   (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
 
+(defun scroll-bar-columns (side)
+  "Return the width, measured in columns, of the vertical scrollbar on SIDE.
+SIDE must be the symbol `left' or `right'."
+  (let* ((wsb   (window-scroll-bars))
+         (vtype (nth 2 wsb))
+         (cols  (nth 1 wsb)))
+    (cond
+     ((not (memq side '(left right)))
+      (error "`left' or `right' expected instead of %S" side))
+     ((and (eq vtype side) cols))
+     ((eq (frame-parameter nil 'vertical-scroll-bars) side)
+      ;; nil means it's a non-toolkit scroll bar, and its width in
+      ;; columns is 14 pixels rounded up.
+      (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
+               (frame-char-width)))
+     (0))))
+
 \f
 ;;;; Helpful functions for enabling and disabling scroll bars.
 
Index: lisp/ruler-mode.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/ruler-mode.el,v
retrieving revision 1.17
diff -u -r1.17 ruler-mode.el
--- lisp/ruler-mode.el	20 Oct 2003 23:27:52 -0000	1.17
+++ lisp/ruler-mode.el	13 Mar 2004 17:58:42 -0000
@@ -94,6 +94,9 @@
 ;; WARNING: To keep ruler graduations aligned on text columns it is
 ;; important to use the same font family and size for ruler and text
 ;; areas.
+;;
+;; You can override the ruler format by defining an appropriate 
+;; function as the buffer-local value of `ruler-mode-ruler-function'.
 
 ;; Installation
 ;;
@@ -108,6 +111,8 @@
 ;;; Code:
 (eval-when-compile
   (require 'wid-edit))
+(require 'scroll-bar)
+(require 'fringe)
 
 (defgroup ruler-mode nil
   "Display a ruler in the header line."
@@ -298,42 +303,21 @@
   "Return the width, measured in columns, of the left fringe area.
 If optional argument REAL is non-nil, return a real floating point
 number instead of a rounded integer value."
-  (funcall (if real '/ 'ceiling)
-           (or (car (window-fringes)) 0)
-           (float (frame-char-width))))
+  (fringe-columns 'left real))
 
 (defsubst ruler-mode-right-fringe-cols (&optional real)
   "Return the width, measured in columns, of the right fringe area.
 If optional argument REAL is non-nil, return a real floating point
 number instead of a rounded integer value."
-  (funcall (if real '/ 'ceiling)
-            (or (nth 1 (window-fringes)) 0)
-            (float (frame-char-width))))
-
-(defun ruler-mode-scroll-bar-cols (side)
-  "Return the width, measured in columns, of the vertical scrollbar on SIDE.
-SIDE must be the symbol `left' or `right'."
-  (let* ((wsb   (window-scroll-bars))
-         (vtype (nth 2 wsb))
-         (cols  (nth 1 wsb)))
-    (cond
-     ((not (memq side '(left right)))
-      (error "`left' or `right' expected instead of %S" side))
-     ((and (eq vtype side) cols))
-     ((eq (frame-parameter nil 'vertical-scroll-bars) side)
-      ;; nil means it's a non-toolkit scroll bar, and its width in
-      ;; columns is 14 pixels rounded up.
-      (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
-               (frame-char-width)))
-     (0))))
+  (fringe-columns 'right real))
 
 (defmacro ruler-mode-right-scroll-bar-cols ()
   "Return the width, measured in columns, of the right vertical scrollbar."
-  '(ruler-mode-scroll-bar-cols 'right))
+  '(scroll-bar-columns 'right))
 
 (defmacro ruler-mode-left-scroll-bar-cols ()
   "Return the width, measured in columns, of the left vertical scrollbar."
-  '(ruler-mode-scroll-bar-cols 'left))
+  '(scroll-bar-columns 'left))
 
 (defsubst ruler-mode-full-window-width ()
   "Return the full width of the selected window."
@@ -568,9 +552,17 @@
   "Hold previous value of `header-line-format'.")
 (make-variable-buffer-local 'ruler-mode-header-line-format-old)
 
+(defvar ruler-mode-ruler-function nil
+  "If non-nil, function to call to return ruler string.
+This variable is expected to be made buffer-local by modes.")
+
 (defconst ruler-mode-header-line-format
-  '(:eval (ruler-mode-ruler))
-  "`header-line-format' used in ruler mode.")
+  '(:eval (funcall (if ruler-mode-ruler-function
+		       ruler-mode-ruler-function
+		     'ruler-mode-ruler)))
+  "`header-line-format' used in ruler mode.
+If the non-nil value for ruler-mode-ruler-function is given, use it.
+Else use `ruler-mode-ruler' is used as default value.")
 
 ;;;###autoload
 (define-minor-mode ruler-mode
Index: lisp/hexl.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/hexl.el,v
retrieving revision 1.85
diff -u -r1.85 hexl.el
--- lisp/hexl.el	9 Mar 2004 01:25:27 -0000	1.85
+++ lisp/hexl.el	13 Mar 2004 17:58:42 -0000
@@ -43,6 +43,11 @@
 ;;; Code:
 
 (require 'eldoc)
+(require 'ruler-mode)
+(require 'frame)
+(require 'fringe)
+(eval-when-compile
+  (require 'hl-line))
 
 ;;
 ;; vars here
@@ -78,6 +83,27 @@
   :group 'hexl
   :version "20.3")
 
+(defcustom hexl-mode-hook '(hexl-follow-line hexl-activate-ruler)
+  "Normal hook run when entering Hexl mode."
+  :type 'hook
+  :options '(hexl-follow-line hexl-activate-ruler))
+
+(defface hexl-address-area
+  '((t (:inherit header-line)))
+  "Face used in address are of hexl-mode buffer."
+  :group 'hexl)
+
+(defface hexl-ascii-area
+  '((t (:inherit header-line)))
+  "Face used in ascii are of hexl-mode buffer."
+  :group 'hexl)
+
+(defface hexl-ascii-cursor
+  '((((class color)) (:foreground "blue" :background "cyan" :underline t))
+    (t (:inverse-video t)))
+  "Face for the cursor in ascii area of hexl mode buffer."
+  :group 'hexl)
+
 (defvar hexl-max-address 0
   "Maximum offset into hexl buffer.")
 
@@ -245,6 +271,10 @@
     (eldoc-remove-command "hexl-save-buffer" 
 			  "hexl-current-address")
 
+    ;; Set a callback function for ruler. 
+    (set (make-local-variable 'ruler-mode-ruler-function) 
+	 'hexl-mode-ruler)
+
     (if hexl-follow-ascii (hexl-follow-ascii 1)))
   (run-hooks 'hexl-mode-hook))
 
@@ -648,6 +678,15 @@
     (apply 'call-process-region (point-min) (point-max)
 	   (expand-file-name hexl-program exec-directory)
 	   t t nil (split-string hexl-options))
+    (save-excursion
+      (goto-char (point-min))
+      (while (re-search-forward "^[0-9a-f]+:" nil t)
+	(put-text-property (match-beginning 0) (match-end 0)
+			   'font-lock-face 'hexl-address-area))
+      (goto-char (point-min))
+      (while (re-search-forward "  \\(.+$\\)" nil t)
+	(put-text-property (match-beginning 1) (match-end 1) 
+			   'font-lock-face 'hexl-ascii-area)))
     (if (> (point) (hexl-address-to-marker hexl-max-address))
 	(hexl-goto-address hexl-max-address))))
 
@@ -865,6 +904,14 @@
 	    (remove-hook 'post-command-hook 'hexl-follow-ascii-find t)
 	    )))))
 
+(defun hexl-activate-ruler ()
+  "Activate `ruler-mode'"
+  (ruler-mode 1))
+
+(defun hexl-follow-line ()
+  "Activate `hl-line-mode'"
+  (hl-line-mode 1))
+
 (defun hexl-follow-ascii-find ()
   "Find and highlight the ASCII element corresponding to current point."
   (let ((pos (+ 51
@@ -872,6 +919,37 @@
 		(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)
+	 (spaces (+ (scroll-bar-columns 'left) 
+		    (fringe-columns 'left)
+		    (or (car (window-margins)) 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 (+ (scroll-bar . left)
+					      left-fringe left-margin
+					      ,pos))
+			 s))
+    ;; Highlight the current column.
+    (put-text-property (+ 10 (/ (* 5 highlight) 2))
+		       (+ 12 (/ (* 5 highlight) 2))
+		       'face 'ruler-mode-current-column-face s)
+    ;; Highlight the current ascii column
+    (put-text-property (+ 12 39 highlight) (+ 12 40 highlight)
+		       'face 'ruler-mode-current-column-face s)
+    ;; Add the leading space.
+    (concat (propertize (make-string (floor spaces) ? )
+			'display `(space :width ,spaces))
+	    s)))
 
 ;; startup stuff.
 
Index: lisp/fringe.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/fringe.el,v
retrieving revision 1.10
diff -u -r1.10 fringe.el
--- lisp/fringe.el	8 Feb 2004 23:33:16 -0000	1.10
+++ lisp/fringe.el	13 Mar 2004 17:58:42 -0000
@@ -218,6 +218,17 @@
    (list (cons 'left-fringe (if (consp mode) (car mode) mode))
 	 (cons 'right-fringe (if (consp mode) (cdr mode) mode)))))
 
+(defsubst fringe-columns (side &optional real)
+  "Return the width, measured in columns, of the fringe area on SIDE.
+If optional argument REAL is non-nil, return a real floating point
+number instead of a rounded integer value.
+SIDE must be the symbol `left' or `right'."
+  (funcall (if real '/ 'ceiling)
+	   (or (funcall (if (eq side 'left) 'car 'cadr)
+			(window-fringes))
+	       0)
+           (float (frame-char-width))))
+  
 (provide 'fringe)
 
 ;;; arch-tag: 6611ef60-0869-47ed-8b93-587ee7d3ff5d

  reply	other threads:[~2004-03-13 18:13 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
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 [this message]
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

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

  git send-email \
    --in-reply-to=20040314.031312.21594984.jet@gyve.org \
    --to=jet@gyve.org \
    --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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.