[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using overlay in register

From: Masatake YAMATO
Subject: Using overlay in register
Date: Thu, 18 Mar 2004 19:33:46 +0900 (JST)

How do you think using overlay instead of marker to represent a point in 

The advantage is strong visual feed back. During editing emacs,
sometime I forget the position of a register. With overlay, we can put
a face on the position. The faces let me keep the memory about the
position of registers. We can also put help-echo on the overlay.

BTW, (overlay-start overlay) < (overlay-end overlay) is guaranteed?

Masatake YAMATO

2004-03-18  Masatake YAMATO  <address@hidden>

        * register.el (register): New customize group.
        (register-point): New face.
        (register-alist): Doc fix.
        (set-register): Delete overlay before
        setting new value to the register.
        (register-put-overlay-at-point): New function.
        (register-get-overlay-position): New function.
        (point-to-register): Use overlay instead of marker.
        (jump-to-register): Ditto.
        (describe-register-1): Ditto.
        (insert-register): Ditto.
        (register-swap-out): Ditto. Doc fix.

cvs diff: warning: unrecognized response `access control disabled, clients can 
connect from any host' from cvs server
Index: lisp/register.el
RCS file: /cvsroot/emacs/emacs/lisp/register.el,v
retrieving revision 1.48
diff -u -r1.48 register.el
--- lisp/register.el    16 Mar 2004 09:52:36 -0000      1.48
+++ lisp/register.el    18 Mar 2004 10:20:36 -0000
@@ -31,9 +31,20 @@
 ;;; Code:
+(defgroup register ()
+  "register commands for Emacs."
+  :group 'editing
+  :prefix "register-")
+(defface register-point
+  '((((class color)) (:box "blue"))
+    (t (:underline t)))
+  "Face for the overlay associated with register."
+  :group 'register)
 (defvar register-alist nil
   "Alist of elements (NAME . CONTENTS), one for each Emacs register.
-NAME is a character (a number).  CONTENTS is a string, number, marker or list.
+NAME is a character (a number).  CONTENTS is a string, number, overlay or list.
 A list of strings represents a rectangle.
 A list of the form (file . NAME) represents the file named NAME.
 A list of the form (file-query NAME POSITION) represents position POSITION
@@ -50,23 +61,46 @@
 (defun set-register (register value)
   "Set contents of Emacs register named REGISTER to VALUE.  Returns VALUE.
 See the documentation of the variable `register-alist' for possible VALUE."
+  ;; If the old value is overlay, delete it first.
+  (let ((overlay (get-register register)))
+    (if (overlayp overlay)
+       (delete-overlay overlay)))
   (let ((aelt (assq register register-alist)))
     (if aelt
        (setcdr aelt value)
       (push (cons register value) register-alist))
+(defun register-put-overlay-at-point (register)
+  "Put a overlay associated with REGISTER at the point."
+  (let ((overlay (make-overlay (point) 
+                              (1+ (point))
+                              (current-buffer)
+                              t))
+       (help (format "Register: %s\nType \"%s %s\" to jump here." 
+                     (single-key-description register)
+                     (substitute-command-keys "\\[jump-to-register]")
+                     (single-key-description register))))
+    (overlay-put overlay 'face 'register-point)
+    (overlay-put overlay 'help-echo help)
+    (overlay-put overlay 'register register)
+    overlay))
+(defun register-get-overlay-position (overlay)
+  "Get the position for OVERLAY."
+  (min (overlay-start overlay) (overlay-end overlay)))
 (defun point-to-register (register &optional arg)
   "Store current location of point in register REGISTER.
 With prefix argument, store current frame configuration.
 Use \\[jump-to-register] to go to that location or restore that configuration.
 Argument is a character, naming the register."
   (interactive "cPoint to register: \nP")
-  ;; Turn the marker into a file-ref if the buffer is killed.
+  ;; Turn the overlay into a file-ref if the buffer is killed.
   (add-hook 'kill-buffer-hook 'register-swap-out nil t)
   (set-register register
                (if arg (list (current-frame-configuration) (point-marker))
-                 (point-marker))))
+                 (register-put-overlay-at-point register))))
 (defun window-configuration-to-register (register &optional arg)
   "Store the window configuration of the selected frame in register REGISTER.
@@ -106,11 +140,11 @@
      ((and (consp val) (window-configuration-p (car val)))
       (set-window-configuration (car val))
       (goto-char (cadr val)))
-     ((markerp val)
-      (or (marker-buffer val)
+     ((overlayp val)
+      (or (overlay-buffer val)
          (error "That register's buffer no longer exists"))
-      (switch-to-buffer (marker-buffer val))
-      (goto-char val))
+      (switch-to-buffer (overlay-buffer val))
+      (goto-char (register-get-overlay-position val)))
      ((and (consp val) (eq (car val) 'file))
       (find-file (cdr val)))
      ((and (consp val) (eq (car val) 'file-query))
@@ -123,15 +157,15 @@
       (error "Register doesn't contain a buffer position or configuration")))))
 (defun register-swap-out ()
-  "Turn markers into file-query references when a buffer is killed."
+  "Turn overlays into file-query references when a buffer is killed."
   (and buffer-file-name
        (dolist (elem register-alist)
-        (and (markerp (cdr elem))
-             (eq (marker-buffer (cdr elem)) (current-buffer))
+        (and (overlayp (cdr elem))
+             (eq (overlay-buffer (cdr elem)) (current-buffer))
              (setcdr elem
                      (list 'file-query
-                           (marker-position (cdr elem))))))))
+                           (overlay-start (cdr elem))))))))
 (defun number-to-register (number register)
   "Store a number in a register.
@@ -187,14 +221,14 @@
      ((numberp val)
       (princ val))
-     ((markerp val)
-      (let ((buf (marker-buffer val)))
+     ((overlayp val)
+      (let ((buf (overlay-buffer val)))
        (if (null buf)
-           (princ "a marker in no buffer")
+           (princ "a overlay in no buffer")
          (princ "a buffer position:\n    buffer ")
          (princ (buffer-name buf))
          (princ ", position ")
-         (princ (marker-position val)))))
+         (princ (register-get-overlay-position val)))))
      ((and (consp val) (window-configuration-p (car val)))
       (princ "a window configuration."))
@@ -264,8 +298,8 @@
       (insert-for-yank val))
      ((numberp val)
       (princ val (current-buffer)))
-     ((and (markerp val) (marker-position val))
-      (princ (marker-position val) (current-buffer)))
+     ((and (overlayp val) (overlay-start val))
+      (princ (register-get-overlay-position val) (current-buffer)))
       (error "Register does not contain text"))))
   (if (not arg) (exchange-point-and-mark)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]