unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Patch: goto-address -vs- font-lock-mode
@ 2007-07-03 18:03 Tom Tromey
  2007-07-05  1:29 ` Richard Stallman
  2007-07-05 14:29 ` Stefan Monnier
  0 siblings, 2 replies; 5+ messages in thread
From: Tom Tromey @ 2007-07-03 18:03 UTC (permalink / raw)
  To: Emacs Hackers

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Patch: goto-address -vs- font-lock-mode
  2007-07-03 18:03 Patch: goto-address -vs- font-lock-mode Tom Tromey
@ 2007-07-05  1:29 ` Richard Stallman
  2007-07-05  1:37   ` Tom Tromey
  2007-07-05 14:20   ` Stefan Monnier
  2007-07-05 14:29 ` Stefan Monnier
  1 sibling, 2 replies; 5+ messages in thread
From: Richard Stallman @ 2007-07-05  1:29 UTC (permalink / raw)
  To: Tom Tromey; +Cc: emacs-devel

    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.

Would it be more appropriate to use overlays for this?
You wouldn't want to copy those properties ever, and it is better
if adding them does not modify the buffer.

Is it feasible to use the font-lock mechanism to add overlays?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Patch: goto-address -vs- font-lock-mode
  2007-07-05  1:29 ` Richard Stallman
@ 2007-07-05  1:37   ` Tom Tromey
  2007-07-05 14:20   ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2007-07-05  1:37 UTC (permalink / raw)
  To: rms; +Cc: emacs-devel

>>>>> "rms" == Richard Stallman <rms@gnu.org> writes:

>     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.

rms> Would it be more appropriate to use overlays for this?
rms> You wouldn't want to copy those properties ever, and it is better
rms> if adding them does not modify the buffer.

rms> Is it feasible to use the font-lock mechanism to add overlays?

I don't know.  I don't really know that much about font-lock -- I just
used it since it was an easy way to get the effect I wanted.

I saw some code on the Emacs wiki that handles updating overlays in
response to modifications to the buffer.  Maybe this would be a good
candidate for inclusion in Emacs itself.  I can email you the code
and/or the author's contact info if that would help.

Also, Lennart told me off-list that there is some code in nXhtml to
handle some cases related to this.  I haven't looked at this code (or
the overlay updating code for that matter) yet.

Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Patch: goto-address -vs- font-lock-mode
  2007-07-05  1:29 ` Richard Stallman
  2007-07-05  1:37   ` Tom Tromey
@ 2007-07-05 14:20   ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2007-07-05 14:20 UTC (permalink / raw)
  To: rms; +Cc: Tom Tromey, emacs-devel

>     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.

> Would it be more appropriate to use overlays for this?
> You wouldn't want to copy those properties ever, and it is better
> if adding them does not modify the buffer.

Unless there are many URLs in the buffer, overlays do seem better suited.

> Is it feasible to use the font-lock mechanism to add overlays?

Of course, since font-lock provides the hooks to plug any piece of
elisp code.   You could do something like:

(defun g-a-f-l-add-overlay (type)
  (let ((ol (make-overlay (match-beginning 0) (match-end 0))))
    (overlay-put ol 'goto-address t)
    (case type
      (url ...)
      (mail ...))
    ...))
    

(defun g-a-f-l-remove-overlays (b e)
  (remove-overlays b e 'goto-address 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 nil
  (let ((keywords
	 `((,goto-address-mail-regexp . (0 (g-a-f-l-add-overlay 'mail)))
	   (,goto-address-url-regexp  . (0 (g-a-f-l-add-overlay 'url))))))
    (font-lock-remove-keywords nil keywords)
    (remove-hook 'font-lock-unfontify-region-function
                 'g-a-f-l-remove-overlays t)
    (when goto-address-font-lock-mode
      (font-lock-mode 1)
      (font-lock-add-keywords nil keywords)
      (add-hook 'font-lock-unfontify-region-function
                'g-a-f-l-remove-overlays nil t)))
  (font-lock-fontify-buffer))


But then you could also hook into jit-lock rather than font-lock, which
would have the advantage of not depending on (on infringing on) the user's
choice to use font-lock or not.


        Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Patch: goto-address -vs- font-lock-mode
  2007-07-03 18:03 Patch: goto-address -vs- font-lock-mode Tom Tromey
  2007-07-05  1:29 ` Richard Stallman
@ 2007-07-05 14:29 ` Stefan Monnier
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2007-07-05 14:29 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Emacs Hackers

> 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.

One way you can do it is instead of:

> +	    . (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)))

Use:

	    . (0 (if (nth 8 (syntax-ppss))
                     '(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))))


-- Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-07-05 14:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-03 18:03 Patch: goto-address -vs- font-lock-mode Tom Tromey
2007-07-05  1:29 ` Richard Stallman
2007-07-05  1:37   ` Tom Tromey
2007-07-05 14:20   ` Stefan Monnier
2007-07-05 14:29 ` Stefan Monnier

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).