* change "word" definition (syntax table) for double-click?
@ 2008-11-29 21:36 ead-gnu-emacs
2008-11-30 2:13 ` Xah Lee
0 siblings, 1 reply; 7+ messages in thread
From: ead-gnu-emacs @ 2008-11-29 21:36 UTC (permalink / raw)
To: help-gnu-emacs
Hello,
How can I have a double-click on for instance the middle "o" in
"http://www.foo.com/" highlight the entirety of "http://www.foo.com/"
rather than merely the word "foo"?
I'm using this version of Emacs:
GNU Emacs 22.2.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll bars)
of 2008-11-09 on raven, modified by Debian
and I've looked at this web page:
Hacking the syntax table
http://www.emacswiki.org/emacs/EmacsSyntaxTable#toc3
, specifically this note:
You can also set which syntax table is used on a per command
basis.
Here is how to use my-wacky-syntax-table with the C-M-f shortcut,
bound to forward-sexp by default.
(global-set-key "\C-\M-f" '(lambda ()
(interactive)
(with-syntax-table my-wacky-syntax-table (forward-sexp))))
; and I've created my own syntax table wherein all non-whitespace
characters are word characters:
(defvar my-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\n "-" table)
(modify-syntax-entry ?! "w" table)
(modify-syntax-entry ?\" "w" table)
(modify-syntax-entry ?# "w" table)
(modify-syntax-entry ?$ "w" table)
(modify-syntax-entry ?% "w" table)
(modify-syntax-entry ?& "w" table)
(modify-syntax-entry ?' "w" table)
(modify-syntax-entry ?\( "w" table)
(modify-syntax-entry ?\) "w" table)
(modify-syntax-entry ?* "w" table)
(modify-syntax-entry ?+ "w" table)
(modify-syntax-entry ?, "w" table)
(modify-syntax-entry ?- "w" table)
(modify-syntax-entry ?. "w" table)
(modify-syntax-entry ?/ "w" table)
(modify-syntax-entry ?: "w" table)
(modify-syntax-entry ?\; "w" table)
(modify-syntax-entry ?< "w" table)
(modify-syntax-entry ?= "w" table)
(modify-syntax-entry ?> "w" table)
(modify-syntax-entry ?? "w" table)
(modify-syntax-entry ?@ "w" table)
(modify-syntax-entry ?\[ "w" table)
(modify-syntax-entry ?\\ "w" table)
(modify-syntax-entry ?\] "w" table)
(modify-syntax-entry ?^ "w" table)
(modify-syntax-entry ?_ "w" table)
(modify-syntax-entry ?` "w" table)
(modify-syntax-entry ?{ "w" table)
(modify-syntax-entry ?| "w" table)
(modify-syntax-entry ?} "w" table)
(modify-syntax-entry ?~ "w" table)
table))
; and I've attempted to bind mouse-set-point to [(double-mouse-1)]
using
the above syntax table:
(global-set-key [(double-mouse-1)] 'my-double-mouse-1)
(defun my-double-mouse-1 (event)
"Run mouse-set-point with my syntax table."
(interactive "e")
(with-syntax-table my-syntax-table (mouse-set-point)))
. However, I get an error message when I perform a double-click:
progn: Wrong number of arguments: #[(event) "\301^H!\210\302^H
\211^X
:\203]^@\303^H8:\203^W^@\303\202^X^@\304^H8\202#^@\305 `
\306\307F)!
\207" [event mouse-minibuffer-check posn-set-point 2 1 selected
window
(0 . 0) 0] 6 581237 "e"], 0
So, where am I going wrong? How can I have my syntax table be applied
to
a double-click?
Thanks a million,
Eric
--
Eric De Mund | Ixian Systems | Jab: eadixian@jabber.org/
main
ead@ixian.com | 650 Castro St, #120-210 | Y!M: ead0002
ixian.com/ead/ | Mountain View, CA 94041 | ICQ: 811788
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: change "word" definition (syntax table) for double-click?
2008-11-29 21:36 change "word" definition (syntax table) for double-click? ead-gnu-emacs
@ 2008-11-30 2:13 ` Xah Lee
2008-12-01 5:08 ` ead-gnu-emacs
0 siblings, 1 reply; 7+ messages in thread
From: Xah Lee @ 2008-11-30 2:13 UTC (permalink / raw)
To: help-gnu-emacs
ead-gnu-em...@ixian.com wrote:
> Hello,
>
> How can I have a double-click on for instance the middle "o" in
> "http://www.foo.com/" highlight the entirety of "http://www.foo.com/"
> rather than merely the word "foo"?
>
> I'm using this version of Emacs:
> GNU Emacs 22.2.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll bars)
> of 2008-11-09 on raven, modified by Debian
>
> and I've looked at this web page:
> Hacking the syntax table
> http://www.emacswiki.org/emacs/EmacsSyntaxTable#toc3
syntax table is not much fitting here. You can do it like this:
(defun select-url ()
"Mark the url under cursor."
(interactive)
; (require 'thingatpt)
(let (bds)
(setq bds (bounds-of-thing-at-point 'url))
(set-mark (car bds))
(goto-char (cdr bds))
)
)
am not sure how to bind it to double clicking though.
if you want to bind it mid button, you can:
(global-set-key (kbd "<mouse-2>") 'select-url)
the above code assumes you have transient-mark-mode on.
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: change "word" definition (syntax table) for double-click?
2008-11-30 2:13 ` Xah Lee
@ 2008-12-01 5:08 ` ead-gnu-emacs
2008-12-01 5:57 ` Xah Lee
2008-12-01 18:29 ` Drew Adams
0 siblings, 2 replies; 7+ messages in thread
From: ead-gnu-emacs @ 2008-12-01 5:08 UTC (permalink / raw)
To: help-gnu-emacs
Xah,
] How can I have a double-click on for instance the middle "o" in
] "http://www.foo.com/" highlight the entirety of "http://
www.foo.com/"
] rather than merely the word "foo"?
Thanks for your reply. I appreciate it. Unfortunately, I see that I
sent
you (and everyone else) down the wrong path with my poor wording. Let
me
try again.
The double-clicking-on-a-URL was just one sample application. What I
want is for double-clicking with the mouse to highlight, as a word,
all
contiguous non-whitespace characters.
Some sample applications I want this for are:
o Double-clicking in the middle of URLs.
o Double-clicking in the middle of RFC822-compliant email addresses.
o Double-clicking in the middle of passwords and other strings
having
non-alphanumeric characters.
In short, I want the same double-clicking control inside emacs that
character classes give me inside xterm and cutchars give me inside
rxvt.
xterm(1)
CHARACTER CLASSES
Clicking the left pointer button twice in rapid
succession (double-clicking) causes all characters of
the same class (e.g., letters, white space, punctuation)
to be selected as a ``word''. Since different people
have different preferences for what should be selected
(for example, should filenames be selected as a whole or
only the separate subnames), the default mapping can be
overridden through the use of the charClass (class
CharClass) resource.
rxvt(1)
cutchars: string
The characters used as delimiters for double-click word
selection (whitespace delimiting is added automatically
if resource is given).
Thank you for any pointers,
Eric
--
Eric De Mund | Ixian Systems | Jab: eadixian@jabber.org/
main
ead@ixian.com | 650 Castro St, #120-210 | Y!M: ead0002
ixian.com/ead/ | Mountain View, CA 94041 | ICQ: 811788
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: change "word" definition (syntax table) for double-click?
2008-12-01 5:08 ` ead-gnu-emacs
@ 2008-12-01 5:57 ` Xah Lee
2008-12-01 18:29 ` Drew Adams
1 sibling, 0 replies; 7+ messages in thread
From: Xah Lee @ 2008-12-01 5:57 UTC (permalink / raw)
To: help-gnu-emacs
On Nov 30, 9:08 pm, ead-gnu-em...@ixian.com wrote:
> Xah,
>
> ] How can I have a double-click on for instance the middle "o" in
> ] "http://www.foo.com/" highlight the entirety of "http://www.foo.com/"
> ] rather than merely the word "foo"?
>
> Thanks for your reply. I appreciate it. Unfortunately, I see that I
> sent
> you (and everyone else) down the wrong path with my poor wording. Let
> me
> try again.
>
> The double-clicking-on-a-URL was just one sample application. What I
> want is for double-clicking with the mouse to highlight, as a word,
> all
> contiguous non-whitespace characters.
>
> Some sample applications I want this for are:
>
> o Double-clicking in the middle of URLs.
> o Double-clicking in the middle of RFC822-compliant email addresses.
> o Double-clicking in the middle of passwords and other strings
> having
> non-alphanumeric characters.
>
> In short, I want the same double-clicking control inside emacs that
> character classes give me inside xterm and cutchars give me inside
> rxvt.
>
> xterm(1)
> CHARACTER CLASSES
> Clicking the left pointer button twice in rapid
> succession (double-clicking) causes all characters of
> the same class (e.g., letters, white space, punctuation)
> to be selected as a ``word''. Since different people
> have different preferences for what should be selected
> (for example, should filenames be selected as a whole or
> only the separate subnames), the default mapping can be
> overridden through the use of the charClass (class
> CharClass) resource.
>
> rxvt(1)
> cutchars: string
> The characters used as delimiters for double-click word
> selection (whitespace delimiting is added automatically
> if resource is given).
>
> Thank you for any pointers,
> Eric
That's easy.
here's a sample code:
(defun get-english-word-boundary ()
"Return the boundary of the current word.
The return value is of the form: (cons pos1 pos2).
A “word” is a sequence of letters and hyphen.
"
(save-excursion
(let (p1 p2)
(progn
(skip-chars-backward "-A-Za-z")
(setq p1 (point))
(skip-chars-forward "-A-Za-z")
(setq p2 (point)))
(cons p1 p2)
))
)
(defun select-word ()
"Mark the url under cursor."
(interactive)
; (require 'thingatpt)
(let (bds)
(setq bds (get-english-word-boundary))
(set-mark (car bds))
(goto-char (cdr bds))
)
)
you can add chars to the skip-chars-backward and skip-chars-forward so
that it'll skip to what you consider word boundary. If you want
everything except whitspace, you can use like:
(skip-chars-backward "^\n\t")
unless you are writing a mode, syntax table is probably not a good
choice here.
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: change "word" definition (syntax table) for double-click?
2008-12-01 5:08 ` ead-gnu-emacs
2008-12-01 5:57 ` Xah Lee
@ 2008-12-01 18:29 ` Drew Adams
2008-12-01 18:39 ` Drew Adams
2008-12-01 18:52 ` Drew Adams
1 sibling, 2 replies; 7+ messages in thread
From: Drew Adams @ 2008-12-01 18:29 UTC (permalink / raw)
To: ead-gnu-emacs, help-gnu-emacs
> What I want is for double-clicking with the mouse to highlight,
> as a word, all contiguous non-whitespace characters.
By "highlight", I think you mean "select", as in setting the active region to
that text.
> Some sample applications I want this for are:
> o Double-clicking in the middle of URLs.
> o Double-clicking in the middle of RFC822-compliant email addresses.
> o Double-clicking in the middle of passwords and other strings
> having non-alphanumeric characters.
Easy solution: `M-x goto-address'. That will put links on URLs and email
addresses, which you can then click (single-click) with mouse-2 (and mouse-1, if
`mouse-1-click-follows-link' is non-nil). See the Emacs manual, node
Hyperlinking, and its subnodes.
If you don't like that for some reason, and you really want to change
double-clicking mouse-2 so that it selects such things, then read on.
Emacs already does that too, at least for many URLs etc. and at least in some
modes, such as Emacs-Lisp mode. That is, provided you double-click (with
mouse-2) on a symbol-constituent character, such as :/.?=@. If you instead
double-click a word-constituent character, then just that word is selected.
For example, in Emacs-Lisp mode, if you double-click the URL
http://www.emacswiki.org/emacs/SiteMap at the : or a / or a ., then the whole
URL is selected. And in the email address help-gnu-emacs@gnu.org, if you
double-click a - or the @ or the ., then the whole address is selected.
However, if you double-click the ; in this URL, then only the ; itself is
selected:
http://www.emacswiki.org/cgi-bin/wiki?action=rc;days=7;all=0;rollback=1;showedit
=1.
The ;'s divide the URL into sections that you can double-click. The reason for
this is that in Emacs-Lisp mode a ; has the syntax of a comment-starter.
In other modes things are different. In text mode, for instance, a double-click
gets you much less text, because of how words and symbols are defined in text
mode.
Know too that you can use mouse-3 (right button) to extend a selection:
double-click, then right-click farther to the right. That can be pretty quick,
to pick up a URL or address.
None of that answers your question completely, but it is good to know.
For a more general answer, the code to look at for inspiration is in standard
library mouse.el. Function `mouse-skip-word' is used to select a range of text
when you double-click. You could try advising or redefining that function to get
the behavior you want. Here is `mouse-skip-word' (it is the same, from Emacs 20
through Emacs 23, at least):
(defun mouse-skip-word (dir)
"Skip over word, over whitespace, or over identical punctuation.
If DIR is positive skip forward; if negative, skip backward."
(let* ((char (following-char))
(syntax (char-to-string (char-syntax char))))
(cond ((string= syntax "w")
;; Here, we can't use skip-syntax-forward/backward because
;; they don't pay attention to word-separating-categories,
;; and thus they will skip over a true word boundary. So,
;; we simulate the original behavior by using forward-word.
(if (< dir 0)
(if (not (looking-at "\\<"))
(forward-word -1))
(if (or (looking-at "\\<") (not (looking-at "\\>")))
(forward-word 1))))
((string= syntax " ")
(if (< dir 0)
(skip-syntax-backward syntax)
(skip-syntax-forward syntax)))
((string= syntax "_")
(if (< dir 0)
(skip-syntax-backward "w_")
(skip-syntax-forward "w_")))
((< dir 0)
(while (and (not (bobp)) (= (preceding-char) char))
(forward-char -1)))
(t
(while (and (not (eobp)) (= (following-char) char))
(forward-char 1))))))
It checks the syntax class of the character you click, then extends the region
according to that class. The first `cond' clause deals with word-constituent
characters. The second clause treats whitespace chars. The third treats
symbol-constituent chars, such as - and _.
A quick semi-solution is to change the first clause to something like this:
((or (string= syntax "w") (string= syntax "_"))
(if (< dir 0)
(skip-syntax-backward "w_")
(skip-syntax-forward "w_")))
That just treats word-constituent and symbol-constituent characters the same
way. It still won't treat the ; in the second URL above the way you want. To
include ; in Emacs-Lisp mode, you could add (string= syntax "<") to the `or', to
include comment-starter constituents.
I'm not recommending that you do any of this, mind you; I'm just explaining a
bit.
And again, the behavior depends on the mode, because different modes give
different characters different syntax classes.
> In short, I want the same double-clicking control inside emacs that
> character classes give me inside xterm and cutchars give me inside
> rxvt.
> xterm(1)
> CHARACTER CLASSES
> Clicking the left pointer button twice in rapid
> succession (double-clicking) causes all characters of
> the same class (e.g., letters, white space, punctuation)
> to be selected as a ``word''. Since different people
> have different preferences for what should be selected
> (for example, should filenames be selected as a whole or
> only the separate subnames), the default mapping can be
> overridden through the use of the charClass (class
> CharClass) resource.
That seems to suggest that you want word constituents, whitespace chars, and
punctuation constituents to act the same. That would mean something like this in
the first clause:
((or (string= syntax "w") (string= syntax " ") (string= syntax "."))
But again, it won't work to double-click the ; in a URL, in Emacs-Lisp mode.
Anyway, you get the idea. You can define the syntax classes that you want
double-clicking to work with.
Another approach is to look at code that recognizes URLs and such, and then
adapt that for use with the mouse. Places to start for that are libraries
ffap.el, browse-url.el, and goto-address.el (the libraries that Emacs manual
node Hyperlinking deals with).
These roll-your-own approaches can be messy, and they're a whole lot more work
than simply using `goto-address', which recognizes just what you want, in all
modes.
HTH
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: change "word" definition (syntax table) for double-click?
2008-12-01 18:29 ` Drew Adams
@ 2008-12-01 18:39 ` Drew Adams
2008-12-01 18:52 ` Drew Adams
1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2008-12-01 18:39 UTC (permalink / raw)
To: ead-gnu-emacs, help-gnu-emacs
I wrote:
> Easy solution: `M-x goto-address'. That will put links on
> URLs and email addresses, which you can then click (single-click) with
> mouse-2 (and mouse-1, if `mouse-1-click-follows-link' is non-nil).
> See the Emacs manual, node Hyperlinking, and its subnodes.
BTW, I see that Emacs 23 has improved this by providing minor mode
`goto-address-mode'. Repeat it to turn off the highlighting, mouse sensitivity,
C-c RET, etc.
Unfortunately, the Emacs 23 Emacs manual is not yet up-to-date on this - it
doesn't mention `goto-address-mode', which leaves you knowing no way to turn it
off. I'll file a doc bug for that.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: change "word" definition (syntax table) for double-click?
2008-12-01 18:29 ` Drew Adams
2008-12-01 18:39 ` Drew Adams
@ 2008-12-01 18:52 ` Drew Adams
1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2008-12-01 18:52 UTC (permalink / raw)
To: ead-gnu-emacs, help-gnu-emacs
> See the Emacs manual, node Hyperlinking, and its subnodes.
I should also have mentioned that FFAP is an alternative to using
`goto-address(-mode)'. Load library ffap, then evaluate (ffap-bindings).
Then you can use `S-mouse-3' to follow URLs and email addresses. The differences
from `goto-address(-mode)' are that there is no highlighting and mouse-2 keeps
its normal behavior.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2008-12-01 18:52 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-29 21:36 change "word" definition (syntax table) for double-click? ead-gnu-emacs
2008-11-30 2:13 ` Xah Lee
2008-12-01 5:08 ` ead-gnu-emacs
2008-12-01 5:57 ` Xah Lee
2008-12-01 18:29 ` Drew Adams
2008-12-01 18:39 ` Drew Adams
2008-12-01 18:52 ` Drew Adams
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).