help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

need help with a defun


From: Bernd Wolter
Subject: need help with a defun
Date: Mon, 07 Jul 2003 23:03:27 +0200
User-agent: Mozilla/5.0 (Windows; U; WinNT4.0; de-DE; rv:0.9.4.1) Gecko/20020314 Netscape6/6.2.2

Hi group,

I wrote a little program in elisp, to help me analyze the contents of a text file. The text file has patterns of interest to me and lots of cruft. In the first step I get rid of the cruft. This part works. In the second step I want to get each pattern into a line of its own to in the further steps sort, count and re-sort them according to number of occurrence.

I got it all done and it works - well, sort of and that's why I'm asking here. As you can see below I really don't exactly know what I am doing.

I do the second step like this:

(defun clean ()
  "Doc string"
  (while (not (eobp))
    (re-search-forward ";\\s-*") ;;pattern of interest delimited by "; "
    (search-backward ";")
    (let* ((beg (point)))
(while (looking-at "[; ]") ;;is there another pattern on the same line?
        (forward-char))
      (delete-region beg (point)))
    (if (looking-at "[\n]")  ;;are we looking at the end of a line
        (forward-char)       ;;goto the next line
      (insert-string "\n"))  ;;otherwise divide the line
    (clean)))

As you old hands will see presently (and I have learned in the meantime) the recursive call to "clean" will quickly get me into trouble with "max-specpdl-size" and "max-lisp-eval-depth". I read up on them in the docs and think I understand why they are there to protect me and the other unwary from ourselves.

I searched the web and info (the elisp node) but could not find a generic and generally approved solution. Besides finding it very inelegant, I don't like the idea to increase the variables in my .emacs (and thereby push this onto others also wanting to use my code). I tried to set they two variables in the "let"-expression to keep them function-local but that somehow didn't take effect.

For the time being I ended up with stealing from esh-mode and doing it like this:

  (set (make-local-variable 'max-lisp-eval-depth)
       (max 3000 max-lisp-eval-depth))
  (set (make-local-variable 'max-specpdl-size)
       (max 6000 max-lisp-eval-depth))

This helps with files up to a certain size but then again gives me "Variable binding exceeds max-lisp-eval-depth". If I increase the value further it crashes emacs.

Info gave me the impression that this only happens with badly written code (mine!) and between the lines implies that the real problem is in front of the screen. I even grepped the lisp and site-lisp dirs in the hope of finding solutions by others to little effect - which amplified my suspicion that this is mainly a newbie problem.

Is there a canonical way of dealing with the above situation? How could the code be improved to perhaps not need to do any looping in a recursive defun? Is there a way to switch off the safety net if you (like me) have taken great pains to make sure there won't be an endless loop?

I would rather have a practical solution with explanation than a philosophical flame war about recursion and dynamical scoping or what not - these I already found via google ;-)

Please teach me

Bernd



reply via email to

[Prev in Thread] Current Thread [Next in Thread]