emacs-devel
[Top][All Lists]
Advanced

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

Patch: goto-address -vs- font-lock-mode


From: Tom Tromey
Subject: Patch: goto-address -vs- font-lock-mode
Date: Tue, 03 Jul 2007 12:03:42 -0600

This patch adds a new minor mode that acts like `goto-address'.
Unlike goto-address, it uses font-lock to add properties to text in
the buffer.  This means that changes to the buffer which add URLs or
email address will automatically be detected.

This patch is not quite ready for checkin.  I wanted some advice first.
This code works by adding multiple properties via font-lock-keywords.
It then adds these new properties to font-lock-extra-managed-props.

This seems a bit error-prone in the case where multiple callers want
to do this.  For instance, I have a separate minor mode that adds
similar functionality for bugzilla references -- it is nearly the same
code and wants to set the same properties.  In this situation there
would be a conflict if the minor mode tries to clean up after itself
by removing items from font-lock-extra-managed-props.

I suppose one idea would be to generalize this code.  It could take a
list of entries (each one a regular expression, a couple of faces, and
a function).  This would work by mandating that only one minor mode at
a time be allowed to do this in a buffer.  (I'm not super happy with
this idea, just throwing it out there...)


Also, I initially wrote this for use in change-log-mode.  However
while writing it, it occurred to me that this would be very useful in
just about any source code buffer, provided the new font-lock keywords
were restricted to comments.  I didn't see an easy way to do that,
though.


Let me know what you think.  Thanks,
Tom

lisp/ChangeLog:
2007-07-03  Tom Tromey  <address@hidden>

        * net/goto-addr.el (goto-address-font-lock-mode): New minor mode.

Index: lisp/net/goto-addr.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/net/goto-addr.el,v
retrieving revision 1.29
diff -u -r1.29 goto-addr.el
--- lisp/net/goto-addr.el       21 Jan 2007 03:02:10 -0000      1.29
+++ lisp/net/goto-addr.el       3 Jul 2007 18:13:31 -0000
@@ -252,6 +252,41 @@
       (goto-address-fontify)))
 ;;;###autoload(put 'goto-address 'safe-local-eval-function t)
 
+(define-minor-mode goto-address-font-lock-mode
+  "A minor mode that adds `goto-address' functionality via `font-lock-mode'.
+Allows user to use mouse/keyboard command to click to go to a URL
+or to send e-mail.
+Enables `font-lock-mode' in the current buffer if not already enabled."
+  nil
+  ""
+  nil
+  (let ((keywords
+        `((,goto-address-mail-regexp
+           . (0 '(face ,goto-address-mail-face
+                       mouse-face ,goto-address-mail-mouse-face
+                       help-echo "mouse-2, C-c RET: mail this address"
+                       keymap ,goto-address-highlight-keymap
+                       follow-link t
+                       goto-address t)))
+          (,goto-address-url-regexp
+           . (0 '(face ,goto-address-url-face
+                       mouse-face ,goto-address-mail-mouse-face
+                       help-echo "mouse-2, C-c RET: follow URL"
+                       keymap ,goto-address-highlight-keymap
+                       follow-link t
+                       goto-address t))))))
+    (if goto-address-font-lock-mode
+       (progn
+         (font-lock-mode 1)
+         (font-lock-add-keywords nil keywords)
+         ;; FIXME: what if other modes want to do this?
+         (setq font-lock-extra-managed-props '(mouse-face help-echo
+                                                          keymap
+                                                          follow-link
+                                                          goto-address)))
+      (font-lock-remove-keywords nil keywords))
+    (font-lock-fontify-buffer)))
+
 (provide 'goto-addr)
 
 ;; arch-tag: ca47c505-5661-425d-a471-62bc6e75cf0a




reply via email to

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