From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: jun net Newsgroups: gmane.emacs.help Subject: Re: Function to download a URL and return it as a string Date: Sun, 9 Dec 2012 23:13:13 +0800 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=bcaec50162eb2d6d9904d06ce377 X-Trace: ger.gmane.org 1355080473 4184 80.91.229.3 (9 Dec 2012 19:14:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 9 Dec 2012 19:14:33 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Sean McAfee Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Dec 09 20:14:47 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ThmKq-0004Uf-K4 for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 20:14:44 +0100 Original-Received: from localhost ([::1]:38551 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThmKe-0004Hf-5k for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Dec 2012 14:14:32 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:60387) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThiZ9-0007Cv-Bc for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 10:13:16 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ThiZ8-0007ZP-7t for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 10:13:15 -0500 Original-Received: from mail-vc0-f169.google.com ([209.85.220.169]:41045) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThiZ8-0007ZI-2V for help-gnu-emacs@gnu.org; Sun, 09 Dec 2012 10:13:14 -0500 Original-Received: by mail-vc0-f169.google.com with SMTP id gb23so2070731vcb.0 for ; Sun, 09 Dec 2012 07:13:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=RdpvbShJCxDttZIjs1dLo1qO95A2DgEhBV3W2/ZGwvw=; b=tw5XJam4bhEzQwysBy7DneJrMMAvhyDKwTpF21CM0ejg45QNr9+APN3tXc4t3ZEIjT nUBAd2JyLuOjd+g1htb4161FVETxCZKUR846g7g/fxWQu/eab6CjZ/2iaFZAsN+3uRfn wjmljW8dQcZKsHjnXECXlIowOgvdT/Hof0LBHGxsrOP9/ygWzgjw4GPTYsbQgUHw03U9 1YrftGbKBnK71CiS8hDVXF9/d+Cx3nlFS29030HpRUi61SMlbsg9aGJwR+KuFphdX1Qu kUo8UcwV9PeUmdborviO8IjGehi6/tW40sQoH5siT9+NSnTG3bCxlQdgTCRk0WuOMlK6 Fknw== Original-Received: by 10.52.88.33 with SMTP id bd1mr6349690vdb.70.1355065993293; Sun, 09 Dec 2012 07:13:13 -0800 (PST) Original-Received: by 10.220.230.69 with HTTP; Sun, 9 Dec 2012 07:13:13 -0800 (PST) Original-Received: by 10.220.230.69 with HTTP; Sun, 9 Dec 2012 07:13:13 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.220.169 X-Mailman-Approved-At: Sun, 09 Dec 2012 14:14:28 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88103 Archived-At: --bcaec50162eb2d6d9904d06ce377 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable There is a function I can't remember exactly. Maybe with-temp-string? Like with-current-buffer, but makes all output to a temp string. =D4=DA 2012-12-9 =CF=C2=CE=E71:20=A3=AC"Sean McAfee" =D0= =B4=B5=C0=A3=BA > > I recently wanted to be able to download a document from a URL and > return it as a string. I couldn't find a function that did that > precisely, but I was able to construct my own: > > (defun download (url) > (with-current-buffer (url-retrieve-synchronously url) > (prog2 > (when (not (search-forward-regexp "^$" nil t)) > (error "Unable to locate downloaded data")) > (buffer-substring (1+ (point)) (point-max)) > (kill-buffer)))) > > This seems rather busy for such a basic-seeming operation--in > particular, having to take care to delete the buffer created by > url-retrieve-synchronously. Is there a better way to do it? > > (On the other hand, this way I get to use prog2, which I almost never > do.) --bcaec50162eb2d6d9904d06ce377 Content-Type: text/html; charset=GB2312 Content-Transfer-Encoding: quoted-printable

There is a function I can't remember exactly. Maybe with-temp-string= ? Like with-current-buffer, but makes all output to a temp string.
=D4=DA 2012-12-9 =CF=C2=CE=E71:20=A3=AC"Sean McAfee" <eefacm@gmail.com>=D0=B4=B5=C0=A3=BA
>
> I recently wanted to be able to download a document from a URL and
> return it as a string.  I couldn't find a function that did t= hat
> precisely, but I was able to construct my own:
>
> (defun download (url)
>   (with-current-buffer (url-retrieve-synchronously url)
>     (prog2
>         (when (not (search-forward-regexp "^$= " nil t))
>           (error "Unable to locate downl= oaded data"))
>         (buffer-substring (1+ (point)) (point-max)= )
>       (kill-buffer))))
>
> This seems rather busy for such a basic-seeming operation--in
> particular, having to take care to delete the buffer created by
> url-retrieve-synchronously.  Is there a better way to do it?
>
> (On the other hand, this way I get to use prog2, which I almost never<= br> > do.)

--bcaec50162eb2d6d9904d06ce377--