[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Lynx-dev] Transferring page text from lynx to vim
From: |
Bela Lubkin |
Subject: |
Re: [Lynx-dev] Transferring page text from lynx to vim |
Date: |
Mon, 3 Oct 2011 10:52:13 -0700 |
Graham Lawrence wrote:
> In lynx, I google my question, choose the response that speaks to my
> level of expertise and either copy and paste some text from lynx to vim
> or in xterm issue the command
> lynx -dump url > file
> then in vim I do
> :r file
> My most recent discovery in vim is :map and so I immediately wanted to
> do something like
> :map ln :!lynx -dump url > file<CR>:r file<CR>
> The sticking point is the value of url, how do I get it
> programmatically, hence my question about a global variable.
In your proposed ":map" macro, Lynx isn't yet running -- you invoke it
right there. So there cannot be a Lynx-internal variable which contains
the URL until you supply one on the Lynx command line issued by the
macro. So your question is really "how do I get the URL into that
macro?"
I don't use such macros, so I am not going to attempt a full solution.
Instead, here are some factoids, little tricks that I *do* use and may
help you get to your goal.
I frequently insert the output of commands into a vi buffer. For a
command which takes no input, I insert a blank line, then while on that
line in edit mode, I type "!!command", e.g. "!!lynx -dump
http://foo.bar". The blank line is because I'm abusing the behavior
"doubled command operates on current line" -- I'm piping one blank line
to the command, knowing that it will ignore stdin.
For you, this would be useful in a macro intended to dump a URL. You
could create a macro which expects the current line to contain a URL;
when you invoke the macro, the URL is replaced by a formatted dump of
the URL's target.
For a filter, I mark two end points: "ma" and "mb"; then
":'a,'b!command". You could use this to create a macro which expects a
range of lines to be HTML source code and replaces it with formatted
output of that code.
For clarity, the two macros I'm suggesting would be used for:
1. line contains "http://en.wikipedia.org/wiki/Spleen"
2. range of lines contains "<html> ... bunch of HTML source</html>";
although of course Lynx is forgiving about such technicalities as
omitting <html>, <head> etc.
I think #1 is what you are seeking, more or less. It is also probably
possible to create a single macro which recognizes the situation and
does the appropriate action.
========================================================================
Getting back to the original question, I think what you are really
asking is: you have Lynx running in one location (X window, terminal
program tab, GNU screen, etc.), and vim in another location. The Lynx
session is looking at a page you want to copy into the vim session.
To do that you would need some sort of inter-process communication. The
default is to view the URL in Lynx ("=") command, use mouse
cut-and-paste to inject it into vim. You could create a Lynx macro
(e.g. PRINTER or DOWNLOADER) which echos the URL into a default
filename, and a vim macro which injects formatted output from the URL
read from that file.
If you want to automate it further -- be able to hit a single command
inside vim, for instance -- you'll need fancier IPC. A way to signal
the Lynx session to save the current URL somewhere accessible to vim. I
can think of various horrible ways to do that, e.g.:
- run Lynx under `ltrace`, use a script to parse URLs (but: insane;
also very difficult to determine which URL is the desired one)
- run Lynx with "-trace" flag, use a script to parse URLs (but:
moderately insane; might also be difficult)
- modify Lynx to use setproctitle() (if available) to always display
the current URL in an accessible location; use `ps e` or local OS's
equivalent to scrape it (but: won't distinguish between multiple
Lynx sessions correctly)
- modify Lynx to use putenv() to establish an environment variable
like LYNX_CURRENT_URL, and maintain it; use /proc/$PID/environ or
local OS's equivalent to scrape it (but: also trouble with multiple
Lynx sessions)
- vim macro runs a script which finds the Lynx session and uses
terminal escape sequences to inject characters into its stdin,
resulting in Lynx executing a macro that saves the URL (but:
extra insane; near-infinite number of things can go wrong)
Those are all ideas of how to do this with a single vim macro. One
could also propose ways to use a single Lynx macro which emits the
current URL to a script, and the script uses another Lynx to format the
output, then some sort of insanity to signal vim to read the resulting
file.
One idea for the signaling method is to use "autocmd", possibly
with one of the events "TermResponse" (not sure how this could be
used); "VimResized" (send SIGWINCH to the vim process to trigger it);
"RemoteReply" (set up a fake "vim server" to do nothing but send replies
when Lynx tells it to); or "User" + ":doautocmd" + I'm not sure what
else.)
OR, modify vim to add a set of user-accessible signal handling
facilities so that you can e.g. send SIGUSR1 and catch that in an
autocmd. <=== Of all the crazy suggestions, this seems the most
plausible to me; also could be very useful for other purposes.
Personally I would stick with the copy-and-paste method...
>Bela<