unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Retrieve a web page into buffer and insert some text into it.
@ 2010-07-28 15:36 ken
  2010-07-28 15:46 ` Lennart Borgman
  2010-07-28 23:46 ` ken
  0 siblings, 2 replies; 11+ messages in thread
From: ken @ 2010-07-28 15:36 UTC (permalink / raw)
  To: help-gnu-emacs


How would I fetch a web page into a new buffer, then programmatically
insert some text into it?  (Of course subsequently saving the buffer to
a file may done interactively.)

I know a little elisp, but not yet the polished programmer (as this
nonfunctioning code shows):

load url.el

(defun www-edit-web-page (url)
  "Retrieve web page and load into new buffer for editing.
Automatically insert after <body> tag URL, appropriately html-tagged URL."
  (interactive "sLoad URL: "
  (with-temp-buffer (url-retrieve url edit-web-page))))


(defun edit-web-page (status)
      "Switch to the buffer returned by `url-retreive'.
    The buffer should contain the web page sent by the server."
      (switch-to-buffer (current-buffer))
    (goto-char 0)
    (re-search-forward "<body.*>" nil t) ;go to end of <body ...> tag.
    ;insert URL into page, properly html-coded.
    (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n </p>\n\n"))




-- 
Find research and analysis on US healthcare, health insurance,
and health policy at: <http://healthpolicydaily.blogspot.com/>



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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-28 15:36 Retrieve a web page into buffer and insert some text into it ken
@ 2010-07-28 15:46 ` Lennart Borgman
  2010-07-28 23:46 ` ken
  1 sibling, 0 replies; 11+ messages in thread
From: Lennart Borgman @ 2010-07-28 15:46 UTC (permalink / raw)
  To: gebser; +Cc: help-gnu-emacs

See the thread about getting the title of a web page.

On Wed, Jul 28, 2010 at 5:36 PM, ken <gebser@mousecar.com> wrote:
>
> How would I fetch a web page into a new buffer, then programmatically
> insert some text into it?  (Of course subsequently saving the buffer to
> a file may done interactively.)
>
> I know a little elisp, but not yet the polished programmer (as this
> nonfunctioning code shows):
>
> load url.el
>
> (defun www-edit-web-page (url)
>  "Retrieve web page and load into new buffer for editing.
> Automatically insert after <body> tag URL, appropriately html-tagged URL."
>  (interactive "sLoad URL: "
>  (with-temp-buffer (url-retrieve url edit-web-page))))
>
>
> (defun edit-web-page (status)
>      "Switch to the buffer returned by `url-retreive'.
>    The buffer should contain the web page sent by the server."
>      (switch-to-buffer (current-buffer))
>    (goto-char 0)
>    (re-search-forward "<body.*>" nil t) ;go to end of <body ...> tag.
>    ;insert URL into page, properly html-coded.
>    (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n </p>\n\n"))
>
>
>
>
> --
> Find research and analysis on US healthcare, health insurance,
> and health policy at: <http://healthpolicydaily.blogspot.com/>
>
>



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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-28 15:36 Retrieve a web page into buffer and insert some text into it ken
  2010-07-28 15:46 ` Lennart Borgman
@ 2010-07-28 23:46 ` ken
  2010-07-29 15:22   ` filebat Mark
  1 sibling, 1 reply; 11+ messages in thread
From: ken @ 2010-07-28 23:46 UTC (permalink / raw)
  To: GNU Emacs List

Lennart suggested I use a different defun, url-copy-file.  I tried that
instead, but it didn't work.  But then I went back to my original code,
moved a single parenthesis and... it worked... mostly.  Here's the code:

------------------------ start ---------------------------
load url.el

(defun www-edit-web-page (url)
  "Retrieve web page and load into new buffer for editing.
Automatically insert after <body> tag URL, appropriately html-tagged URL."
  (interactive "sLoad URL: ")
  (with-temp-buffer (url-retrieve url 'edit-web-page)))


(defun edit-web-page (status)
      "Switch to the buffer returned by `url-retreive'.
    The buffer should contain the web page sent by the server."
      (switch-to-buffer (current-buffer))
    (goto-char 0)
    (re-search-forward "<body.*>" nil t) ;go to end of <body ...> tag.
    ;insert URL into page
    (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n </p>\n\n"))

------------------------ ende ---------------------------

This properly fetches the web page and loads it into a new, unsaved
buffer (exactly what I want), but the last line in the second defun
doesn't execute.  The error messages are telling me that edit-web-page
doesn't know the value of "url".  So how do I pass this variable-- with
its assignment from www-edit-web-page to edit-web-page?  (I have a
guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a bit
mysterious.)





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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-28 23:46 ` ken
@ 2010-07-29 15:22   ` filebat Mark
  2010-07-29 20:01     ` ken
  0 siblings, 1 reply; 11+ messages in thread
From: filebat Mark @ 2010-07-29 15:22 UTC (permalink / raw)
  To: GNU Emacs List, gebser

[-- Attachment #1: Type: text/plain, Size: 1764 bytes --]

Hi Ken

Where do you set the value of url in the second function?

One big enhancement shall be setting the coding system of the temp buffer,
based on the charset of the html page.

Regards,
Denny

On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com> wrote:

> Lennart suggested I use a different defun, url-copy-file.  I tried that
> instead, but it didn't work.  But then I went back to my original code,
> moved a single parenthesis and... it worked... mostly.  Here's the code:
>
> ------------------------ start ---------------------------
> load url.el
>
> (defun www-edit-web-page (url)
>  "Retrieve web page and load into new buffer for editing.
> Automatically insert after <body> tag URL, appropriately html-tagged URL."
>  (interactive "sLoad URL: ")
>  (with-temp-buffer (url-retrieve url 'edit-web-page)))
>
>
> (defun edit-web-page (status)
>      "Switch to the buffer returned by `url-retreive'.
>    The buffer should contain the web page sent by the server."
>      (switch-to-buffer (current-buffer))
>    (goto-char 0)
>    (re-search-forward "<body.*>" nil t) ;go to end of <body ...> tag.
>    ;insert URL into page
>     (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n </p>\n\n"))
>
> ------------------------ ende ---------------------------
>
> This properly fetches the web page and loads it into a new, unsaved
> buffer (exactly what I want), but the last line in the second defun
> doesn't execute.  The error messages are telling me that edit-web-page
> doesn't know the value of "url".  So how do I pass this variable-- with
> its assignment from www-edit-web-page to edit-web-page?  (I have a
> guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a bit
> mysterious.)
>
>
>
>


-- 
Thanks & Regards

Denny Zhang

[-- Attachment #2: Type: text/html, Size: 2409 bytes --]

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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-29 15:22   ` filebat Mark
@ 2010-07-29 20:01     ` ken
       [not found]       ` <AANLkTi=7Y3=H1CeY_OvKZWwZp4R1JwpJCJQ20OMHTEKu@mail.gmail.com>
  0 siblings, 1 reply; 11+ messages in thread
From: ken @ 2010-07-29 20:01 UTC (permalink / raw)
  To: GNU Emacs List


On 07/29/2010 11:22 AM filebat Mark wrote:
> Hi Ken

Hi, Denny.  Thanks for replying


> Where do you set the value of url in the second function?

It needs to be passed from the first defun, but I don't know the syntax
for doing that.  The url-retrieve function is a little complex in terms
of its arguments.  I think it's stumped even the experts on this list.


> One big enhancement shall be setting the coding system of the temp
> buffer, based on the charset of the html page.

Thanks for bringing that up.  Yeah, I followed that on your thread.  I
might have to deal with that too.  If, after I get the main part of my
code working how I want it, I find it's having that problem, I'll review
your emails (which I still have) and perhaps get back to you for some tips.


> Regards,
> Denny

Back at ya!
ken


> 
> On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com
> <mailto:gebser@mousecar.com>> wrote:
> 
>     Lennart suggested I use a different defun, url-copy-file.  I tried that
>     instead, but it didn't work.  But then I went back to my original code,
>     moved a single parenthesis and... it worked... mostly.  Here's the code:
> 
>     ------------------------ start ---------------------------
>     load url.el
> 
>     (defun www-edit-web-page (url)
>      "Retrieve web page and load into new buffer for editing.
>     Automatically insert after <body> tag URL, appropriately html-tagged
>     URL."
>      (interactive "sLoad URL: ")
>      (with-temp-buffer (url-retrieve url 'edit-web-page)))
> 
> 
>     (defun edit-web-page (status)
>          "Switch to the buffer returned by `url-retreive'.
>        The buffer should contain the web page sent by the server."
>          (switch-to-buffer (current-buffer))
>        (goto-char 0)
>        (re-search-forward "<body.*>" nil t) ;go to end of <body ...> tag.
>        ;insert URL into page
>        (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n </p>\n\n"))
> 
>     ------------------------ ende ---------------------------
> 
>     This properly fetches the web page and loads it into a new, unsaved
>     buffer (exactly what I want), but the last line in the second defun
>     doesn't execute.  The error messages are telling me that edit-web-page
>     doesn't know the value of "url".  So how do I pass this variable-- with
>     its assignment from www-edit-web-page to edit-web-page?  (I have a
>     guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a bit
>     mysterious.)
> 
> 
> 
> 
> 
> 
> -- 
> Thanks & Regards
> 
> Denny Zhang
> 



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

* Re: Retrieve a web page into buffer and insert some text into it.
       [not found]       ` <AANLkTi=7Y3=H1CeY_OvKZWwZp4R1JwpJCJQ20OMHTEKu@mail.gmail.com>
@ 2010-07-30 11:23         ` ken
  2010-07-30 16:32           ` filebat Mark
  0 siblings, 1 reply; 11+ messages in thread
From: ken @ 2010-07-30 11:23 UTC (permalink / raw)
  To: GNU Emacs List

Thanks, Denny.  I got the args working right, so it's basically working.
 So now I'm on to another issue: the re-search-forward function.  Again,
it's a syntax thing.

As the code suggests, I'm looking for the html opening body tag.  That
text could be as simple as "<body>", but could also be:

<
 BodY
 fgcolor="hazel"
>

I thought this would work:

(re-search-forward "<\s*[bB][oO][dD][yY]\s.*>" nil t)

but apparently "\s" is being treated as a literal and not as
representing [whitespace].  Also, it doesn't seem that elisp has a
function for doing a case-insensitive RE search.



On 07/29/2010 10:37 PM filebat Mark wrote:
> Hi Ken
> 
> Yes, the parameter of CBARGS in url-retrieve function is confusing.
> I also tried this, but it complains of "wrong number of arguments".
> 
> Let's wait to see whether others have any comment. I will spend some
> time, when I'm free.
> 
> 
> On Fri, Jul 30, 2010 at 4:01 AM, ken <gebser@mousecar.com
> <mailto:gebser@mousecar.com>> wrote:
> 
> 
>     On 07/29/2010 11:22 AM filebat Mark wrote:
>     > Hi Ken
> 
>     Hi, Denny.  Thanks for replying
> 
> 
>     > Where do you set the value of url in the second function?
> 
>     It needs to be passed from the first defun, but I don't know the syntax
>     for doing that.  The url-retrieve function is a little complex in terms
>     of its arguments.  I think it's stumped even the experts on this list.
> 
> 
>     > One big enhancement shall be setting the coding system of the temp
>     > buffer, based on the charset of the html page.
> 
>     Thanks for bringing that up.  Yeah, I followed that on your thread.  I
>     might have to deal with that too.  If, after I get the main part of my
>     code working how I want it, I find it's having that problem, I'll review
>     your emails (which I still have) and perhaps get back to you for
>     some tips.
> 
> 
>     > Regards,
>     > Denny
> 
>     Back at ya!
>     ken
> 
> 
>     >
>     > On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com
>     <mailto:gebser@mousecar.com>
>     > <mailto:gebser@mousecar.com <mailto:gebser@mousecar.com>>> wrote:
>     >
>     >     Lennart suggested I use a different defun, url-copy-file.  I
>     tried that
>     >     instead, but it didn't work.  But then I went back to my
>     original code,
>     >     moved a single parenthesis and... it worked... mostly.  Here's
>     the code:
>     >
>     >     ------------------------ start ---------------------------
>     >     load url.el
>     >
>     >     (defun www-edit-web-page (url)
>     >      "Retrieve web page and load into new buffer for editing.
>     >     Automatically insert after <body> tag URL, appropriately
>     html-tagged
>     >     URL."
>     >      (interactive "sLoad URL: ")
>     >      (with-temp-buffer (url-retrieve url 'edit-web-page)))
>     >
>     >
>     >     (defun edit-web-page (status)
>     >          "Switch to the buffer returned by `url-retreive'.
>     >        The buffer should contain the web page sent by the server."
>     >          (switch-to-buffer (current-buffer))
>     >        (goto-char 0)
>     >        (re-search-forward "<body.*>" nil t) ;go to end of <body
>     ...> tag.
>     >        ;insert URL into page
>     >        (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n
>     </p>\n\n"))
>     >
>     >     ------------------------ ende ---------------------------
>     >
>     >     This properly fetches the web page and loads it into a new,
>     unsaved
>     >     buffer (exactly what I want), but the last line in the second
>     defun
>     >     doesn't execute.  The error messages are telling me that
>     edit-web-page
>     >     doesn't know the value of "url".  So how do I pass this
>     variable-- with
>     >     its assignment from www-edit-web-page to edit-web-page?  (I have a
>     >     guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a bit
>     >     mysterious.)
>     >
>     >
>     >
>     >
>     >
>     >
>     > --
>     > Thanks & Regards
>     >
>     > Denny Zhang
>     >
> 
> 
> 
> 
> -- 
> Thanks & Regards
> 
> Denny Zhang
> 



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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-30 11:23         ` ken
@ 2010-07-30 16:32           ` filebat Mark
  2010-07-30 16:51             ` filebat Mark
  0 siblings, 1 reply; 11+ messages in thread
From: filebat Mark @ 2010-07-30 16:32 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 4640 bytes --]

(setq case-fold-search t), then re-search-forward is case insensitive.

To leverage your effort, can you post your code for the sample of args
parameter.

On Fri, Jul 30, 2010 at 7:23 PM, ken <gebser@mousecar.com> wrote:

> Thanks, Denny.  I got the args working right, so it's basically working.
>  So now I'm on to another issue: the re-search-forward function.  Again,
> it's a syntax thing.
>
> As the code suggests, I'm looking for the html opening body tag.  That
> text could be as simple as "<body>", but could also be:
>
> <
>  BodY
>  fgcolor="hazel"
> >
>
> I thought this would work:
>
> (re-search-forward "<\s*[bB][oO][dD][yY]\s.*>" nil t)
>
> but apparently "\s" is being treated as a literal and not as
> representing [whitespace].  Also, it doesn't seem that elisp has a
> function for doing a case-insensitive RE search.
>
>
>
> On 07/29/2010 10:37 PM filebat Mark wrote:
> > Hi Ken
> >
> > Yes, the parameter of CBARGS in url-retrieve function is confusing.
> > I also tried this, but it complains of "wrong number of arguments".
> >
> > Let's wait to see whether others have any comment. I will spend some
> > time, when I'm free.
> >
> >
> > On Fri, Jul 30, 2010 at 4:01 AM, ken <gebser@mousecar.com
> > <mailto:gebser@mousecar.com>> wrote:
> >
> >
> >     On 07/29/2010 11:22 AM filebat Mark wrote:
> >     > Hi Ken
> >
> >     Hi, Denny.  Thanks for replying
> >
> >
> >     > Where do you set the value of url in the second function?
> >
> >     It needs to be passed from the first defun, but I don't know the
> syntax
> >     for doing that.  The url-retrieve function is a little complex in
> terms
> >     of its arguments.  I think it's stumped even the experts on this
> list.
> >
> >
> >     > One big enhancement shall be setting the coding system of the temp
> >     > buffer, based on the charset of the html page.
> >
> >     Thanks for bringing that up.  Yeah, I followed that on your thread.
>  I
> >     might have to deal with that too.  If, after I get the main part of
> my
> >     code working how I want it, I find it's having that problem, I'll
> review
> >     your emails (which I still have) and perhaps get back to you for
> >     some tips.
> >
> >
> >     > Regards,
> >     > Denny
> >
> >     Back at ya!
> >     ken
> >
> >
> >     >
> >     > On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com
> >     <mailto:gebser@mousecar.com>
> >     > <mailto:gebser@mousecar.com <mailto:gebser@mousecar.com>>> wrote:
> >     >
> >     >     Lennart suggested I use a different defun, url-copy-file.  I
> >     tried that
> >     >     instead, but it didn't work.  But then I went back to my
> >     original code,
> >     >     moved a single parenthesis and... it worked... mostly.  Here's
> >     the code:
> >     >
> >     >     ------------------------ start ---------------------------
> >     >     load url.el
> >     >
> >     >     (defun www-edit-web-page (url)
> >     >      "Retrieve web page and load into new buffer for editing.
> >     >     Automatically insert after <body> tag URL, appropriately
> >     html-tagged
> >     >     URL."
> >     >      (interactive "sLoad URL: ")
> >     >      (with-temp-buffer (url-retrieve url 'edit-web-page)))
> >     >
> >     >
> >     >     (defun edit-web-page (status)
> >     >          "Switch to the buffer returned by `url-retreive'.
> >     >        The buffer should contain the web page sent by the server."
> >     >          (switch-to-buffer (current-buffer))
> >     >        (goto-char 0)
> >     >        (re-search-forward "<body.*>" nil t) ;go to end of <body
> >     ...> tag.
> >     >        ;insert URL into page
> >     >        (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n
> >     </p>\n\n"))
> >     >
> >     >     ------------------------ ende ---------------------------
> >     >
> >     >     This properly fetches the web page and loads it into a new,
> >     unsaved
> >     >     buffer (exactly what I want), but the last line in the second
> >     defun
> >     >     doesn't execute.  The error messages are telling me that
> >     edit-web-page
> >     >     doesn't know the value of "url".  So how do I pass this
> >     variable-- with
> >     >     its assignment from www-edit-web-page to edit-web-page?  (I
> have a
> >     >     guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a
> bit
> >     >     mysterious.)
> >     >
> >     >
> >     >
> >     >
> >     >
> >     >
> >     > --
> >     > Thanks & Regards
> >     >
> >     > Denny Zhang
> >     >
> >
> >
> >
> >
> > --
> > Thanks & Regards
> >
> > Denny Zhang
> >
>
>


-- 
Thanks & Regards

Denny Zhang

[-- Attachment #2: Type: text/html, Size: 6506 bytes --]

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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-30 16:32           ` filebat Mark
@ 2010-07-30 16:51             ` filebat Mark
  2010-07-30 17:00               ` filebat Mark
  2010-07-30 17:12               ` Teemu Likonen
  0 siblings, 2 replies; 11+ messages in thread
From: filebat Mark @ 2010-07-30 16:51 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 5219 bytes --]

Does the below solve your problem?

(defun match-web-body()
  (interactive)
   (setq case-fold-search t);;Make searches case insensitive
   (goto-char 0)
   (re-search-forward "\\(< *\n* *body\n* +fgcolor=\".*\" *\n*>\\)" nil t 1)
   (setq match_str (match-string 1))
   (message match_str)
)


On Sat, Jul 31, 2010 at 12:32 AM, filebat Mark <filebat.mark@gmail.com>wrote:

> (setq case-fold-search t), then re-search-forward is case insensitive.
>
> To leverage your effort, can you post your code for the sample of args
> parameter.
>
>
> On Fri, Jul 30, 2010 at 7:23 PM, ken <gebser@mousecar.com> wrote:
>
>> Thanks, Denny.  I got the args working right, so it's basically working.
>>  So now I'm on to another issue: the re-search-forward function.  Again,
>> it's a syntax thing.
>>
>> As the code suggests, I'm looking for the html opening body tag.  That
>> text could be as simple as "<body>", but could also be:
>>
>> <
>>  BodY
>>  fgcolor="hazel"
>> >
>>
>> I thought this would work:
>>
>> (re-search-forward "<\s*[bB][oO][dD][yY]\s.*>" nil t)
>>
>> but apparently "\s" is being treated as a literal and not as
>> representing [whitespace].  Also, it doesn't seem that elisp has a
>> function for doing a case-insensitive RE search.
>>
>>
>>
>> On 07/29/2010 10:37 PM filebat Mark wrote:
>> > Hi Ken
>> >
>> > Yes, the parameter of CBARGS in url-retrieve function is confusing.
>> > I also tried this, but it complains of "wrong number of arguments".
>> >
>> > Let's wait to see whether others have any comment. I will spend some
>> > time, when I'm free.
>> >
>> >
>> > On Fri, Jul 30, 2010 at 4:01 AM, ken <gebser@mousecar.com
>> > <mailto:gebser@mousecar.com>> wrote:
>> >
>> >
>> >     On 07/29/2010 11:22 AM filebat Mark wrote:
>> >     > Hi Ken
>> >
>> >     Hi, Denny.  Thanks for replying
>> >
>> >
>> >     > Where do you set the value of url in the second function?
>> >
>> >     It needs to be passed from the first defun, but I don't know the
>> syntax
>> >     for doing that.  The url-retrieve function is a little complex in
>> terms
>> >     of its arguments.  I think it's stumped even the experts on this
>> list.
>> >
>> >
>> >     > One big enhancement shall be setting the coding system of the temp
>> >     > buffer, based on the charset of the html page.
>> >
>> >     Thanks for bringing that up.  Yeah, I followed that on your thread.
>>  I
>> >     might have to deal with that too.  If, after I get the main part of
>> my
>> >     code working how I want it, I find it's having that problem, I'll
>> review
>> >     your emails (which I still have) and perhaps get back to you for
>> >     some tips.
>> >
>> >
>> >     > Regards,
>> >     > Denny
>> >
>> >     Back at ya!
>> >     ken
>> >
>> >
>> >     >
>> >     > On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com
>> >     <mailto:gebser@mousecar.com>
>> >     > <mailto:gebser@mousecar.com <mailto:gebser@mousecar.com>>> wrote:
>> >     >
>> >     >     Lennart suggested I use a different defun, url-copy-file.  I
>> >     tried that
>> >     >     instead, but it didn't work.  But then I went back to my
>> >     original code,
>> >     >     moved a single parenthesis and... it worked... mostly.  Here's
>> >     the code:
>> >     >
>> >     >     ------------------------ start ---------------------------
>> >     >     load url.el
>> >     >
>> >     >     (defun www-edit-web-page (url)
>> >     >      "Retrieve web page and load into new buffer for editing.
>> >     >     Automatically insert after <body> tag URL, appropriately
>> >     html-tagged
>> >     >     URL."
>> >     >      (interactive "sLoad URL: ")
>> >     >      (with-temp-buffer (url-retrieve url 'edit-web-page)))
>> >     >
>> >     >
>> >     >     (defun edit-web-page (status)
>> >     >          "Switch to the buffer returned by `url-retreive'.
>> >     >        The buffer should contain the web page sent by the server."
>> >     >          (switch-to-buffer (current-buffer))
>> >     >        (goto-char 0)
>> >     >        (re-search-forward "<body.*>" nil t) ;go to end of <body
>> >     ...> tag.
>> >     >        ;insert URL into page
>> >     >        (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n
>> >     </p>\n\n"))
>> >     >
>> >     >     ------------------------ ende ---------------------------
>> >     >
>> >     >     This properly fetches the web page and loads it into a new,
>> >     unsaved
>> >     >     buffer (exactly what I want), but the last line in the second
>> >     defun
>> >     >     doesn't execute.  The error messages are telling me that
>> >     edit-web-page
>> >     >     doesn't know the value of "url".  So how do I pass this
>> >     variable-- with
>> >     >     its assignment from www-edit-web-page to edit-web-page?  (I
>> have a
>> >     >     guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is a
>> bit
>> >     >     mysterious.)
>> >     >
>> >     >
>> >     >
>> >     >
>> >     >
>> >     >
>> >     > --
>> >     > Thanks & Regards
>> >     >
>> >     > Denny Zhang
>> >     >
>> >
>> >
>> >
>> >
>> > --
>> > Thanks & Regards
>> >
>> > Denny Zhang
>> >
>>
>>
>
>
> --
> Thanks & Regards
>
> Denny Zhang
>
>


-- 
Thanks & Regards

Denny Zhang

[-- Attachment #2: Type: text/html, Size: 7420 bytes --]

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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-30 16:51             ` filebat Mark
@ 2010-07-30 17:00               ` filebat Mark
  2010-07-30 17:12               ` Teemu Likonen
  1 sibling, 0 replies; 11+ messages in thread
From: filebat Mark @ 2010-07-30 17:00 UTC (permalink / raw)
  To: gebser; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 5748 bytes --]

Below is the revised version. A little ugly, but more precise.
   (re-search-forward
"\\(<[[:blank:]]*\n*[[:blank:]]*body[[:blank:]]*\n*[[:blank:]]*fgcolor[[:blank:]]*=\".*\"[[:blank:]]*\n*[[:blank:]]*>\\)"
nil t 1)


On 7/31/10, filebat Mark <filebat.mark@gmail.com> wrote:
>
> Does the below solve your problem?
>
> (defun match-web-body()
>   (interactive)
>    (setq case-fold-search t);;Make searches case insensitive
>    (goto-char 0)
>    (re-search-forward "\\(< *\n* *body\n* +fgcolor=\".*\" *\n*>\\)" nil t
> 1)
>    (setq match_str (match-string 1))
>    (message match_str)
> )
>
>
>
> On Sat, Jul 31, 2010 at 12:32 AM, filebat Mark <filebat.mark@gmail.com>wrote:
>
>> (setq case-fold-search t), then re-search-forward is case insensitive.
>>
>> To leverage your effort, can you post your code for the sample of args
>> parameter.
>>
>>
>> On Fri, Jul 30, 2010 at 7:23 PM, ken <gebser@mousecar.com> wrote:
>>
>>> Thanks, Denny.  I got the args working right, so it's basically working.
>>>  So now I'm on to another issue: the re-search-forward function.  Again,
>>> it's a syntax thing.
>>>
>>> As the code suggests, I'm looking for the html opening body tag.  That
>>> text could be as simple as "<body>", but could also be:
>>>
>>> <
>>>  BodY
>>>  fgcolor="hazel"
>>> >
>>>
>>> I thought this would work:
>>>
>>> (re-search-forward "<\s*[bB][oO][dD][yY]\s.*>" nil t)
>>>
>>> but apparently "\s" is being treated as a literal and not as
>>> representing [whitespace].  Also, it doesn't seem that elisp has a
>>> function for doing a case-insensitive RE search.
>>>
>>>
>>>
>>> On 07/29/2010 10:37 PM filebat Mark wrote:
>>> > Hi Ken
>>> >
>>> > Yes, the parameter of CBARGS in url-retrieve function is confusing.
>>> > I also tried this, but it complains of "wrong number of arguments".
>>> >
>>> > Let's wait to see whether others have any comment. I will spend some
>>> > time, when I'm free.
>>> >
>>> >
>>> > On Fri, Jul 30, 2010 at 4:01 AM, ken <gebser@mousecar.com
>>> > <mailto:gebser@mousecar.com>> wrote:
>>> >
>>> >
>>> >     On 07/29/2010 11:22 AM filebat Mark wrote:
>>> >     > Hi Ken
>>> >
>>> >     Hi, Denny.  Thanks for replying
>>> >
>>> >
>>> >     > Where do you set the value of url in the second function?
>>> >
>>> >     It needs to be passed from the first defun, but I don't know the
>>> syntax
>>> >     for doing that.  The url-retrieve function is a little complex in
>>> terms
>>> >     of its arguments.  I think it's stumped even the experts on this
>>> list.
>>> >
>>> >
>>> >     > One big enhancement shall be setting the coding system of the
>>> temp
>>> >     > buffer, based on the charset of the html page.
>>> >
>>> >     Thanks for bringing that up.  Yeah, I followed that on your thread.
>>>  I
>>> >     might have to deal with that too.  If, after I get the main part of
>>> my
>>> >     code working how I want it, I find it's having that problem, I'll
>>> review
>>> >     your emails (which I still have) and perhaps get back to you for
>>> >     some tips.
>>> >
>>> >
>>> >     > Regards,
>>> >     > Denny
>>> >
>>> >     Back at ya!
>>> >     ken
>>> >
>>> >
>>> >     >
>>> >     > On Thu, Jul 29, 2010 at 7:46 AM, ken <gebser@mousecar.com
>>> >     <mailto:gebser@mousecar.com>
>>> >     > <mailto:gebser@mousecar.com <mailto:gebser@mousecar.com>>>
>>> wrote:
>>> >     >
>>> >     >     Lennart suggested I use a different defun, url-copy-file.  I
>>> >     tried that
>>> >     >     instead, but it didn't work.  But then I went back to my
>>> >     original code,
>>> >     >     moved a single parenthesis and... it worked... mostly.
>>>  Here's
>>> >     the code:
>>> >     >
>>> >     >     ------------------------ start ---------------------------
>>> >     >     load url.el
>>> >     >
>>> >     >     (defun www-edit-web-page (url)
>>> >     >      "Retrieve web page and load into new buffer for editing.
>>> >     >     Automatically insert after <body> tag URL, appropriately
>>> >     html-tagged
>>> >     >     URL."
>>> >     >      (interactive "sLoad URL: ")
>>> >     >      (with-temp-buffer (url-retrieve url 'edit-web-page)))
>>> >     >
>>> >     >
>>> >     >     (defun edit-web-page (status)
>>> >     >          "Switch to the buffer returned by `url-retreive'.
>>> >     >        The buffer should contain the web page sent by the
>>> server."
>>> >     >          (switch-to-buffer (current-buffer))
>>> >     >        (goto-char 0)
>>> >     >        (re-search-forward "<body.*>" nil t) ;go to end of <body
>>> >     ...> tag.
>>> >     >        ;insert URL into page
>>> >     >        (insert "\n<p>From: <a href=\"" url "\">" url "</a>\n
>>> >     </p>\n\n"))
>>> >     >
>>> >     >     ------------------------ ende ---------------------------
>>> >     >
>>> >     >     This properly fetches the web page and loads it into a new,
>>> >     unsaved
>>> >     >     buffer (exactly what I want), but the last line in the second
>>> >     defun
>>> >     >     doesn't execute.  The error messages are telling me that
>>> >     edit-web-page
>>> >     >     doesn't know the value of "url".  So how do I pass this
>>> >     variable-- with
>>> >     >     its assignment from www-edit-web-page to edit-web-page?  (I
>>> have a
>>> >     >     guess, but i'm more a C/bash/blah/blah/blah guy, so elisp is
>>> a bit
>>> >     >     mysterious.)
>>> >     >
>>> >     >
>>> >     >
>>> >     >
>>> >     >
>>> >     >
>>> >     > --
>>> >     > Thanks & Regards
>>> >     >
>>> >     > Denny Zhang
>>> >     >
>>> >
>>> >
>>> >
>>> >
>>> > --
>>> > Thanks & Regards
>>> >
>>> > Denny Zhang
>>> >
>>>
>>>
>>
>>
>> --
>> Thanks & Regards
>>
>> Denny Zhang
>>
>>
>
>
> --
> Thanks & Regards
>
> Denny Zhang
>
>


-- 
Thanks & Regards

Denny Zhang

[-- Attachment #2: Type: text/html, Size: 8619 bytes --]

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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-30 16:51             ` filebat Mark
  2010-07-30 17:00               ` filebat Mark
@ 2010-07-30 17:12               ` Teemu Likonen
  2010-07-31  2:05                 ` filebat Mark
  1 sibling, 1 reply; 11+ messages in thread
From: Teemu Likonen @ 2010-07-30 17:12 UTC (permalink / raw)
  To: filebat Mark; +Cc: GNU Emacs List

* 2010-07-31 00:51 (+0800), filebat Mark wrote:

> Does the below solve your problem?

I don't know but I'll comment your code in general.

> (defun match-web-body()
>   (interactive)
>    (setq case-fold-search t);;Make searches case insensitive
>    (goto-char 0)
>    (re-search-forward "\\(< *\n* *body\n* +fgcolor=\".*\" *\n*>\\)" nil t 1)
>    (setq match_str (match-string 1))
>    (message match_str)
> )

If you want to set case-fold-search or other state-changing variable for
certain operation create a local binding for the variable, do not assign
new value to the existing binding. In other words, do not do this:

    (setq case-fold-search t)
    (re-search-forward ...)

Do this instead:

    (let ((case-fold-search t))
      (re-search-forward ...))

Also, do not introduce new global variables in functions like you did
here:

>    (setq match_str (match-string 1))
>    (message match_str)

In that case there is no need for the variable at all. It could be
written like this:

    (message "%s" (match-string 1))

But even if you needed a variable you shouldn't introduce it with SETQ
but create a local binding with LET:

    (let ((match-str (match-string 1)))
      (message "%s" match-str)
      ;; Plus other uses of the variable
      )

In short, keep things local.



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

* Re: Retrieve a web page into buffer and insert some text into it.
  2010-07-30 17:12               ` Teemu Likonen
@ 2010-07-31  2:05                 ` filebat Mark
  0 siblings, 0 replies; 11+ messages in thread
From: filebat Mark @ 2010-07-31  2:05 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: GNU Emacs List

[-- Attachment #1: Type: text/plain, Size: 1677 bytes --]

Thanks a lot, likonen for your suggestion.

Agree,  (let ((case-fold-search t)) is much safer than override global
variable.
I need to do more homework for lisp programming. ^-^

Regards,
Denny

On 7/31/10, Teemu Likonen <tlikonen@iki.fi> wrote:
>
> * 2010-07-31 00:51 (+0800), filebat Mark wrote:
>
> > Does the below solve your problem?
>
>
> I don't know but I'll comment your code in general.
>
>
> > (defun match-web-body()
> >   (interactive)
> >    (setq case-fold-search t);;Make searches case insensitive
> >    (goto-char 0)
> >    (re-search-forward "\\(< *\n* *body\n* +fgcolor=\".*\" *\n*>\\)" nil t
> 1)
> >    (setq match_str (match-string 1))
> >    (message match_str)
> > )
>
>
> If you want to set case-fold-search or other state-changing variable for
> certain operation create a local binding for the variable, do not assign
> new value to the existing binding. In other words, do not do this:
>
>
>     (setq case-fold-search t)
>
>     (re-search-forward ...)
>
> Do this instead:
>
>     (let ((case-fold-search t))
>       (re-search-forward ...))
>
> Also, do not introduce new global variables in functions like you did
> here:
>
>
> >    (setq match_str (match-string 1))
> >    (message match_str)
>
>
> In that case there is no need for the variable at all. It could be
> written like this:
>
>     (message "%s" (match-string 1))
>
> But even if you needed a variable you shouldn't introduce it with SETQ
> but create a local binding with LET:
>
>     (let ((match-str (match-string 1)))
>       (message "%s" match-str)
>       ;; Plus other uses of the variable
>       )
>
> In short, keep things local.
>



-- 
Thanks & Regards

Denny Zhang

[-- Attachment #2: Type: text/html, Size: 2215 bytes --]

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

end of thread, other threads:[~2010-07-31  2:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-28 15:36 Retrieve a web page into buffer and insert some text into it ken
2010-07-28 15:46 ` Lennart Borgman
2010-07-28 23:46 ` ken
2010-07-29 15:22   ` filebat Mark
2010-07-29 20:01     ` ken
     [not found]       ` <AANLkTi=7Y3=H1CeY_OvKZWwZp4R1JwpJCJQ20OMHTEKu@mail.gmail.com>
2010-07-30 11:23         ` ken
2010-07-30 16:32           ` filebat Mark
2010-07-30 16:51             ` filebat Mark
2010-07-30 17:00               ` filebat Mark
2010-07-30 17:12               ` Teemu Likonen
2010-07-31  2:05                 ` filebat Mark

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