darcs patch: add XSelection.hs

So I said I'd send in Andrea's X selection stuff when 0.3 was finalized and XMonadContrib/ opened up again. Here it is! I hope someone besides myself finds it useful. -- gwern SSCI Coderpunks David tekka CCS NMS CDA Verisign Keyer IRS

On Sat, Sep 15, 2007 at 09:07:01AM -0400, Gwern Branwen wrote:
So I said I'd send in Andrea's X selection stuff when 0.3 was finalized and XMonadContrib/ opened up again. Here it is! I hope someone besides myself finds it useful.
I'm thinking about some improvements that probably should be provided before pushing this patch (the same applies to Hxsel, the companion of this piece of code):
+----------------------------------------------------------------------------- +-- | +-- Module : XMonadContrib.XSelection +-- Copyright : (C) 2007 Andrea Rossato
we should at least share the copyright...;-)
+ ty <- internAtom dpy "UTF8_STRING" False
this should be protected with a `catch` \_ -> internAtom dpy "sTRING" False, or even better, falling back first to COMPOUND_TEXT and then to string, right? Just in case UTF8_STRING is not present.
+{- UTF-8 decoding for internal use in getSelection. This code is totally stolen from Eric Mertens's utf-string library [...] +decode :: [Word8] -> String
I may be wrong but there should be an accepted patch to x11-extras that provides the function needed here, but, I repeat it, I cuold be plainly wrong. Cheers, Andrea

On 2007.09.14 18:52:10 +0200, Andrea Rossato
On Sat, Sep 15, 2007 at 09:07:01AM -0400, Gwern Branwen wrote:
So I said I'd send in Andrea's X selection stuff when 0.3 was finalized and XMonadContrib/ opened up again. Here it is! I hope someone besides myself finds it useful.
I'm thinking about some improvements that probably should be provided before pushing this patch (the same applies to Hxsel, the companion of this piece of code):
+-- | +-- Module : XMonadContrib.XSelection +-- Copyright : (C) 2007 Andrea Rossato
we should at least share the copyright...;-)
Well, like I've said before: I'm not a huge fan of copyright, and I put everything into the public domain. So no need to credit me.
+ ty <- internAtom dpy "UTF8_STRING" False
this should be protected with a `catch` \_ -> internAtom dpy "sTRING" False, or even better, falling back first to COMPOUND_TEXT and then to string, right? Just in case UTF8_STRING is not present.
I suppose you could do something like: import Control.Exception as E (catch) ... ty ← E.catch (E.catch (internAtom dpy "sTring" False) (λ_ → internAtom dpy "COMPOUND_TEXT" False⦆ (λ_ → internAtom dpy "UTF8_STRING" False) I mean, this seems to compile and to do the right thing, but it looks kind of ugly. Does this actually help for anything? What systems that could run XMonad wouldn't return some sort of UTF-8 string?
+{- UTF-8 decoding for internal use in getSelection. This code is totally stolen from Eric Mertens's utf-string library [...] +decode :: [Word8] -> String
I may be wrong but there should be an accepted patch to x11-extras that provides the function needed here, but, I repeat it, I cuold be plainly wrong.
Well, I looked through x11-extra's Haddock, but I didn't see anything with that type signature, but I probably missed something.
Cheers, Andrea
I recently thought of another thing, though. I use Surfraw elvi (https://secure.wikimedia.org/wikipedia/en/wiki/Surfraw) a lot for running Wikipedia and Google searches, and wayback even when I was using Ratpoison lo those many years ago, I had problems with my queries sometimes containing shell metacharacters. This was a problem with ratpoison, StumpWM and XMonad all because the obvious and easy way is to go through whatever their equivalent of 'spawn' is. So I wrote a 'safeSpawn' which avoids the shell: safeSpawn :: FilePath -> String -> X () safeSpawn prog arg = io (try (forkProcess $ executeFile prog True [arg] Nothing) >> return ()) This is pretty neat. D'you think it'd be worth including in XSelection, since I figure people will often be highlighting and manipulating URLs, and URLs often include characters like '&' (which has bad effects when passed to a shell...)? -- gwern DSD e95 BfV h MI5 Bush TRANSEC passwd Council Rivera

Hi Gwern, first of all sorry if I come back to our Xselection stuff so late, but the new class branch was quite a huge job form me (I also need to get to understand what I'm doing before actually doing it, and that unusually takes more time then the actual getting things done, if you know what I mean.. :) On Sat, Sep 22, 2007 at 04:11:49PM -0400, Gwern Branwen wrote:
we should at least share the copyright...;-)
Well, like I've said before: I'm not a huge fan of copyright, and I put everything into the public domain. So no need to credit me.
Well, me too (I'm a lawyer after all), but I don't want to take credits for others' job. Anyway I'll respect your decision here.
import Control.Exception as E (catch) ... ty ← E.catch (E.catch (internAtom dpy "sTring" False) (λ_ → internAtom dpy "COMPOUND_TEXT" False⦆ (λ_ → internAtom dpy "UTF8_STRING" False)
This seems fine to me.
I mean, this seems to compile and to do the right thing, but it looks kind of ugly. Does this actually help for anything? What systems that could run XMonad wouldn't return some sort of UTF-8 string?
Well, as far as I know, and if I understand the point you are making, it is the application that must provide the UTF8_STRING property. Since we call internAtom with a False, if that application doesn't provide an UTF8_STRING, xmonad is going to crash - am I right here? I'm thinking about legacy application some user may be running, and then she comes up with a bug you cannot even chase..:) But I repeat it, I may be wrong.
I recently thought of another thing, though. I use Surfraw elvi (https://secure.wikimedia.org/wikipedia/en/wiki/Surfraw) a lot for running Wikipedia and Google searches, and wayback even when I was using Ratpoison lo those many years ago, I had problems with my queries sometimes containing shell metacharacters. This was a problem with ratpoison, StumpWM and XMonad all because the obvious and easy way is to go through whatever their equivalent of 'spawn' is.
So I wrote a 'safeSpawn' which avoids the shell:
safeSpawn :: FilePath -> String -> X () safeSpawn prog arg = io (try (forkProcess $ executeFile prog True [arg] Nothing) >> return ())
There's a module I contributed, RunInXTerm. I'm wondering if this function wouldn't be perfect for that module (function to be exported for other to use) more then in Xselection.
This is pretty neat. Do you think it'd be worth including in XSelection, since I figure people will often be highlighting and manipulating URLs, and URLs often include characters like '&' (which has bad effects when passed to a shell...)?
As you know I achieve this result with hxsel from the command line, but this could be a useful addition I think. I was also thinking about a prompt (with, possibly, a completion of available searches) where you type a search engine and a query and firefox is going to open up the appropriate page. What would you think? Something like SearchPrompt. This is something I would like to have (and if nobody does I'm going to write as soon as I'll have some time). Once again, sorry for the delay of my answer. Andrea

On 2007.09.29 13:07:51 +0200, Andrea Rossato
Hi Gwern,
first of all sorry if I come back to our Xselection stuff so late, but the new class branch was quite a huge job form me (I also need to get to understand what I'm doing before actually doing it, and that unusually takes more time then the actual getting things done, if you know what I mean.. :)
On Sat, Sep 22, 2007 at 04:11:49PM -0400, Gwern Branwen wrote:
we should at least share the copyright...;-)
Well, like I've said before: I'm not a huge fan of copyright, and I put everything into the public domain. So no need to credit me.
Well, me too (I'm a lawyer after all), but I don't want to take credits for others' job. Anyway I'll respect your decision here.
import Control.Exception as E (catch) ... ty ??? E.catch (E.catch (internAtom dpy "sTring" False) (??_ ??? internAtom dpy "COMPOUND_TEXT" False??? (??_ ??? internAtom dpy "UTF8_STRING" False)
This seems fine to me.
I mean, this seems to compile and to do the right thing, but it looks kind of ugly. Does this actually help for anything? What systems that could run XMonad wouldn't return some sort of UTF-8 string?
Well, as far as I know, and if I understand the point you are making, it is the application that must provide the UTF8_STRING property. Since we call internAtom with a False, if that application doesn't provide an UTF8_STRING, xmonad is going to crash - am I right here? I'm thinking about legacy application some user may be running, and then she comes up with a bug you cannot even chase..:)
But I repeat it, I may be wrong.
I recently thought of another thing, though. I use Surfraw elvi (https://secure.wikimedia.org/wikipedia/en/wiki/Surfraw) a lot for running Wikipedia and Google searches, and wayback even when I was using Ratpoison lo those many years ago, I had problems with my queries sometimes containing shell metacharacters. This was a problem with ratpoison, StumpWM and XMonad all because the obvious and easy way is to go through whatever their equivalent of 'spawn' is.
So I wrote a 'safeSpawn' which avoids the shell:
safeSpawn :: FilePath -> String -> X () safeSpawn prog arg = io (try (forkProcess $ executeFile prog True [arg] Nothing) >> return ())
There's a module I contributed, RunInXTerm. I'm wondering if this function wouldn't be perfect for that module (function to be exported for other to use) more then in Xselection. ...
Yes, I've been thinking about that a lot lately. I recently sent some patches in for XMonadContrib which implements that suggestion. -- gwern Centro RPC RAID piz Scully Fiel Fiel CTP GCHQ IWG
participants (2)
-
Andrea Rossato
-
Gwern Branwen