unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tom Tromey <tromey@redhat.com>
To: Emacs Hackers <emacs-devel@gnu.org>
Subject: Patch: goto-address -vs- font-lock-mode
Date: Tue, 03 Jul 2007 12:03:42 -0600	[thread overview]
Message-ID: <m3zm2d2wld.fsf@fleche.redhat.com> (raw)

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  <tromey@redhat.com>

	* 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	other threads:[~2007-07-03 18:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-03 18:03 Tom Tromey [this message]
2007-07-05  1:29 ` Patch: goto-address -vs- font-lock-mode Richard Stallman
2007-07-05  1:37   ` Tom Tromey
2007-07-05 14:20   ` Stefan Monnier
2007-07-05 14:29 ` Stefan Monnier

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=m3zm2d2wld.fsf@fleche.redhat.com \
    --to=tromey@redhat.com \
    --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).