unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Help with lexical binding
@ 2013-08-10 17:32 E Sabof
  0 siblings, 0 replies; 6+ messages in thread
From: E Sabof @ 2013-08-10 17:32 UTC (permalink / raw)
  To: help-gnu-emacs

I have the snippet below. I can save it to a file, and run `eval-buffer' on
it. Or I can use `load'. In both cases it wlll work. However if I paste it
into scratch, and do M-: (setq lexical-binding t) and then M-x eval-buffer,
closures aren't created, and it fails. Is it a bug, or am I missing
something?

;; -*- lexical-binding: t -*-
(require 'cl-lib)
(defun es-scrape-web-listing
    (start-url collect-function next-url-function done-function
     &optional pages-limit silent)
  "Retrieve a list of things from a multi-page web document.

START-URL is the location from which to start scraping.

COLLECT-FUNCTION should return a list of collected things.

NEXT-URL-FUNCTION should be a function that will return the
link to the next page, or nil, when on the last page.

DONE-FUNCTION will be called once processing is finished, with
one argument - the results list.

One can limit the number of retrieved pages, by setting PAGES-LIMIT to a
number.

When SILENT is non-nil, no progress messages will be displayed.

The function returns a function that will stop the scraping process."
  (cl-assert (and start-url collect-function next-url-function
done-function))
  (let (retrieve-recursively
        collected
        next-url
        ( visited-urls (list start-url)))
    (setq retrieve-recursively
          (lambda (&rest args)
            (goto-char (point-min))
            (setq collected
                  (nconc collected
                         (funcall collect-function)))
            (goto-char (point-min))
            (unless silent
              (message (format "Scraped \"%s\". Collected so far: %s"
                               next-url
                               (length collected))))
            (cond ( (and pages-limit (<= (cl-decf pages-limit) 0))
                    (funcall done-function collected))
                  ( (and (setq next-url (funcall next-url-function))
                         (not (member next-url visited-urls)))
                    (push next-url visited-urls)
                    (url-retrieve next-url retrieve-recursively))
                  ( t (funcall done-function collected)))
            (kill-buffer)))
    (url-retrieve start-url
                  retrieve-recursively))
  (lambda ()
    (setq next-url-function (lambda ()))))

(defvar ncz-posts nil)
(defvar ncz-scraper-stop nil)
(setq ncz-scraper-stop
      (es-scrape-web-listing
       "http://www.nczonline.net/"
       (lambda ()
         (cl-loop with link-holder
                  with name-holder
                  while (and (search-forward "class=\"post-snippet" nil t)
                             (re-search-forward "href=\"\\(?1:.+?\\)\"" nil
t)
                             (setq link-holder (match-string 1))
                             (re-search-forward ">\\(?1:.+?\\)<" nil t)
                             (setq name-holder (match-string 1)))
                  collecting (cons link-holder name-holder)
                  ))
       (lambda ()
         (ignore-errors
           (search-forward "<div class=\"navigation\">")
           (re-search-forward "<a href=\"\\(?1:.+?\\)\" >")
           (match-string 1)))
       (lambda (result)
         (setq scrape-result result)
         (message (concat "Done. "
                          (number-to-string
                           (length scrape-result))
                          " items found.")))
       1))


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

* Re: Help with lexical binding
       [not found] <mailman.2986.1376155982.12400.help-gnu-emacs@gnu.org>
@ 2013-08-10 18:06 ` esabof
  2013-08-10 18:34   ` esabof
  0 siblings, 1 reply; 6+ messages in thread
From: esabof @ 2013-08-10 18:06 UTC (permalink / raw)
  To: help-gnu-emacs

It will also work if I use `eval-last-sexp' instead of `eval-buffer'.


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

* Re: Help with lexical binding
  2013-08-10 18:06 ` Help with lexical binding esabof
@ 2013-08-10 18:34   ` esabof
  2013-08-11  2:56     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: esabof @ 2013-08-10 18:34 UTC (permalink / raw)
  To: help-gnu-emacs

On Saturday, August 10, 2013 7:06:24 PM UTC+1, esa...@gmail.com wrote:
> It will also work if I use `eval-last-sexp' instead of `eval-buffer'.

Evidently `eval-buffer' ignores the buffer local value of `lexical-binding', and is only sensitive to the first line (I had scratch comments in the beginning, that's why it didn't work). 

Might it be better to use the buffer-local value, unless a file-local variable exists? In any case, I think this peculiarity is worth documenting.

Evgeni


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

* Re: Help with lexical binding
  2013-08-10 18:34   ` esabof
@ 2013-08-11  2:56     ` Stefan Monnier
  2013-08-11 13:18       ` esabof
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2013-08-11  2:56 UTC (permalink / raw)
  To: help-gnu-emacs

> Evidently `eval-buffer' ignores the buffer local value of `lexical-binding',
> and is only sensitive to the first line (I had scratch comments in the
> beginning, that's why it didn't work).

Looks like a bug, indeed.  Please M-x report-emacs-bug so we don't lose
track of it.


        Stefan


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

* Re: Help with lexical binding
  2013-08-11  2:56     ` Stefan Monnier
@ 2013-08-11 13:18       ` esabof
  2013-08-11 17:46         ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: esabof @ 2013-08-11 13:18 UTC (permalink / raw)
  To: help-gnu-emacs


> Looks like a bug, indeed.  Please M-x report-emacs-bug so we don't lose
> 
> track of it.
> 
>         Stefan

Done

Evgeni


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

* Re: Help with lexical binding
  2013-08-11 13:18       ` esabof
@ 2013-08-11 17:46         ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2013-08-11 17:46 UTC (permalink / raw)
  To: help-gnu-emacs

>> Looks like a bug, indeed.  Please M-x report-emacs-bug so we don't lose
>> track of it.
> Done

Thanks, seen it,


        Stefan




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

end of thread, other threads:[~2013-08-11 17:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.2986.1376155982.12400.help-gnu-emacs@gnu.org>
2013-08-10 18:06 ` Help with lexical binding esabof
2013-08-10 18:34   ` esabof
2013-08-11  2:56     ` Stefan Monnier
2013-08-11 13:18       ` esabof
2013-08-11 17:46         ` Stefan Monnier
2013-08-10 17:32 E Sabof

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