Common Lisp Error Not Understood - lisp

I'm trying to write a number guessing game in Lisp as a time-killing project. However, when I try to load up the program using SBCL, I get the following error:
debugger invoked on a SB-C::INPUT-ERROR-IN-COMPILE-FILE in thread #<THREAD
"initial thread" RUNNING
{AA14959}>:
READ failure in COMPILE-FILE at character 477:
end of file on #<SB-SYS:FD-STREAM
for "file /home/andy/Dropbox/Programming/Common Lisp/number-game.lisp"
{B4F45F9}>
Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE] Ignore runtime option --load "number-game.lisp".
1: [ABORT ] Skip rest of --eval and --load options.
2: Skip to toplevel READ/EVAL/PRINT loop.
3: [QUIT ] Quit SBCL (calling #'QUIT, killing the process).
(SB-C::READ-FOR-COMPILE-FILE
#<SB-SYS:FD-STREAM
for "file /home/andy/Dropbox/Programming/Common Lisp/number-game.lisp"
{B4F45F9}>
477)
What does this error mean? The code is as follows, and the error appears when loading the file and calling (play) from the REPL:
;;;; number-game.lisp
;;;;
;;;; Andrew Levenson
;;;; 10/25/2010
;;;;
;;;; Simple number guessing game. User has
;;;; five guesses to determine a number between
;;;; one and one hundred, inclusive (1-100).
;;; Set global variable for the target number:
(defparameter *target* nil)
;;; Set the iterator so we may check the number of guesses
(defparameter *number-of-guesses* 0)
;;; Welcome the user
(defun welcome-user ()
(format t "Welcome to the number guessing game!~%"))
;;; Prompt for a guess
(defun prompt-for-guess ()
(format t "Please enter your guess (1-100): ")
(finish-output nil) ; nil directs finish-output to standard IO
(check-guess((read-guess)))
;;; Read in a guess
(defun read-guess ()
(let ((guess (read)))
(if (numberp guess) ; If true, return guess. Else, call prompt-for-guess
(progn
(setq *number-of-guesses* (+ *number-of-guesses* 1))
guess)
(prompt-for-guess))))
;;; Check if the guess is higher than, lower than, or equal to, the target
(defun check-guess (guess)
(if (equal guess *target*)
(equal-to)
(if (> guess *target*)
(greater-than (guess))
(if (< guess *target*)
(less-than (guess))))))
;;; If the guess is equal to the target, the game is over
(defun equal-to ()
(format t "Congratulations! You have guessed the target number, ~a!~%" *target*)
(y-or-n-p "Play again? [y/n] "))
;;; If the guess is greater than the target, inform the player.
(defun greater-than (guess)
(format t "Sorry, ~a is greater than the target.~%" guess)
(if (< *number-of-guesses* 6)
(prompt-for-guess)
(game-over)))
;;; If the guess is less than the target, inform the player.
(defun less-than (guess)
(format t "Sorry, ~a is less than the target.~%" guess)
(if (< *number-of-guesses* 6)
(prompt-for-guess)
(game-over)))
;;; If the player has run out of guesses, give them the option
;;; of playing the game again.
(defun game-over ()
(y-or-n-p "You have run out of guesses. Play again? [y/n] "))
;;; Play the game
(defun play ()
;; If it's their first time playing this session,
;; make sure to greet the user.
(unless (> *number-of-guesses* 0)
(welcome-user))
;; Reset their remaining guesses
(setq *number-of-guesses* 0)
;; Set the target value
(setq *target*
;; Random can return float values,
;; so we must round the result to get
;; an integer value.
(round
;; Add one to the result, because
;; (random 100) yields a number between
;; 0 and 99, whereas we want a number
;; from 1 to 100 inclusive.
(+ (random 100) 1)))
(if (equal (prompt-for-guess) "y")
(play)
(quit)))
(I'm fairly certain that the program doesn't work minus that one error, I'm still a complete novice when it comes to Lisp. This is just the first error I've encountered that I can't figure out on my own.)
Oh, and the issue most likely has to do with the prompt-for-guess, read-guess and check-guess functions, because those were the ones I was messing with when this error cropped up.

It looks like you didn't close enough parens on your prompt-for-guess defun.
The reader is getting to the end of the file and noticing that it has a form still open, and can't figure out where it's from.
An easy way I use to find errors like this is to have my text editor indent the region, and make sure everything lines up like I think it should.

The command in Emacs is M-x check-parens (it checks everything that needs to balance, like quotes, as well). It can be a bit mystifying if everything balances, because it does nothing in that case.
check-parens
Command: Check for unbalanced parentheses in the current buffer.
More accurately, check the narrowed part of the buffer for unbalanced
expressions ("sexps") in general. This is done according to the
current syntax table and will find unbalanced brackets or quotes as
appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
found, an error is signaled and point is left at the first unbalanced
character.

end of file during read, there is a closing parenthesis (or similar) missing. Character 477. Move the cursor in your text to 477 and check which expression it is.
Check your IDE for a command to find unbalanced expressions.
In LispWorks this would be M-x Find Unbalanced Parentheses.
SLIME should have some command for that, too.

Related

M-x compile better final state

When I say M-x compile the make program is typically called. When finished, the buffer *compilation* is often left in an undesirable state: half of the window is left empty (probably expecting some more output). With C-l C-l I can move to last line in the buffer to the bottom of the window - so I see more of the actual compilation process.
Is there a clean way to configure compile such that the window will always show the maximal number of lines - at least at the end?
Here's what I came up with (put it in your .emacs).
(defun compilation-redisplay (proc msg)
"Scroll the current window to fit the tail of the buffer
in. This only fires if `compilation-scroll-output' is true"
(when (memq (process-status proc) '(exit signal))
(let ((buffer (process-buffer proc)))
;; Check that the buffer hasn't already been killed
(unless (null (buffer-name buffer))
(with-current-buffer buffer
;; Check we're at the bottom of the buffer and that we're there
;; because compile.el put us there
(when (and compilation-scroll-output
(= (point) (point-max)))
(with-selected-window (get-buffer-window)
;; This logic is pinched from recenter-top-bottom (window.el)
(recenter
(- -1 (min (max 0 scroll-margin)
(truncate (/ (window-body-height) 4.0))))))))))))
(advice-add 'compilation-sentinel :after #'compilation-redisplay)
The idea is that it runs when the compilation process finishes. It then does a little bit of checking to make sure that there is a sensible buffer, that compilation-scroll-output is set, and that point is at the bottom of the buffer (you don't want stuff jumping around if you have a long compilation process and you've already started poring over the output). Finally, the recenter call is stolen from the code in recenter-top-bottom.

AUCTeX: Run Compile Command n-times

I'd like to have a function that asks for a number n and executes the default compile command n-times afterwards. That is to say unlike C-c C-c (i.e. TeX-command-master) I don't want to be asked which command to run, it should select the default compile command based on the AUCTeX settings. Naturally if any error occurs the execution should stop.
I know about TeX-texify, however, this doesn't statisfy my needs because sometimes I just want emacs to run pdflatex five times indepent of what the AUCTeX parser thinks is adequate.
Any help is much appreciated!
Edit: I have looked into this a little further and using code from the above reference I have started writing a function that does this. However, it has one major flaw. Let me first give you the code:
(defcustom TeX-MultiTeX-Command "LaTeX" "Default MultiTeX command" :type 'string :group 'TeX-command)
(defun TeX-MultiTeX (n)
"Run TeX-command n-times"
(interactive "nRun TeX/LaTeX how many times: ")
(while (> n 0)
(TeX-command TeX-MultiTeX-Command 'TeX-master-file)
(setq n (- n 1))))
As you can see, I have implemented a config variable for selecting the correct compilation command. Now let me present the problem:
The compilation of the LaTeX document takes some time, however, my function instantly calls the second (and following) executions of the compile command. Maybe someone can provide help in finding a solution that checks whether compilation has finished successfully prior to executing (TeX-command TeX-MultiTeX-Command 'TeX-master-file), then executes said function or prints some error message if compilation finished with an error.
With the help of the code of the TeX-texify function I have developed a function that does what I want, the code is given below.
I'd like to thank user4815162342; although this solution is not based on his suggestion, I think his solution might be of use for a different problem. Also I'd like to thank TN, the author of TeX-texify, I shamelessly took and adapted his code for my problem. ;)
(defcustom TeX-MultiTeX-Command "LaTeX"
"Default MultiTeX command"
:type 'string :group 'TeX-command)
(defun TeX-MultiTeX-sentinel (&optional proc sentinel)
"Non-interactive! Call the standard-sentinel of the current LaTeX-process.
If there is still something left do do start the next latex-command."
(set-buffer (process-buffer proc))
(funcall TeX-MultiTeX-sentinel proc sentinel)
(let ((case-fold-search nil))
(when (string-match "\\(finished\\|exited\\)" sentinel)
(set-buffer TeX-command-buffer)
(unless (plist-get TeX-error-report-switches (intern (TeX-master-file)))
(TeX-MultiTeX TeX-MultiTeX-num-left)))))
(defun TeX-MultiTeX (n)
"Run TeX-command n-times"
(interactive "nRun TeX/LaTeX how many times: ")
(when (or (called-interactively-p 'any)
(null (boundp 'TeX-MultiTeX-num-left)))
(setq TeX-MultiTeX-num-left n))
(if (>= TeX-MultiTeX-num-left 1)
(progn
(TeX-command TeX-MultiTeX-Command 'TeX-master-file)
(setq TeX-MultiTeX-num-left (- TeX-MultiTeX-num-left 1))
(setq proc (get-buffer-process (current-buffer)))
(setq TeX-MultiTeX-sentinel (process-sentinel proc))
(set-process-sentinel proc 'TeX-MultiTeX-sentinel))))
It seems that you need a synchronous way to run TeX-command. I haven't word with TeX-command, but if it uses the compilation API, it can be made to wait for the compilation to finish, although it's not exactly obvious how to do that. Here is an example that uses compilation-finish-functions to achieve the desired effect:
(require 'cl) ; for lexical-let
(defun compile-and-wait (compilefun)
(interactive)
(lexical-let ((done nil) finish-callback)
(setq finish-callback
;; when the compilation is done, remove the callback from
;; compilation-finish-functions and interrupt the wait
(lambda (buf msg)
(setq compilation-finish-functions
(delq finish-callback compilation-finish-functions))
(setq done t)))
(push finish-callback compilation-finish-functions)
(funcall compilefun)
(while (not done)
(sleep-for .1))))
EDIT
AUC TeX is not using compilation mode to spawn TeX, so the above cannot work. Since it's still useful for other compilation buffers, I'm leaving it in the answer. Another way to implement TeX-MultiTeX is by binding TeX-process-asynchronous to nil, which should ensure that AUC TeX waits for the command to finish.

Emacs completion-at-point-functions

I'm writing a derived mode, based on comint-mode. The mode is an interface to a command line program (GRASS gis), and the comint mode completion works for the programs. I'm trying to add on support for completing the arguments to the program, via completion-at-point-functions. A toy example is:
(setq my-commands
'(("ls"
("my-completion-1")
("my-completion-2"))
("mv"
("my-completion-3")
("my-completion-4"))))
(defun my-completion-at-point ()
(interactive)
(let ((pt (point)) ;; collect point
start end)
(save-excursion ;; collect the program name
(comint-bol)
(re-search-forward "\\(\\S +\\)\\s ?"))
(if (and (>= pt (match-beginning 1))
(<= pt (match-end 1)))
() ;; if we're still entering the command, pass completion on to
;; comint-completion-at-point by returning nil
(let ((command (match-string-no-properties 1)))
(when (member* command my-commands :test 'string= :key 'car)
;; If the command is one of my-commands, use the associated completions
(goto-char pt)
(re-search-backward "\\S *")
(setq start (point))
(re-search-forward "\\S *")
(setq end (point))
(list start end (cdr (assoc command my-commands)) :exclusive 'no))))))
(push 'my-completion-at-point completion-at-point-functions)
This almost works. I get normal completion of program names. However, if I have entered ls at the command line, hitting tab inserts my-completion- and doesn't offer the two options. Hitting tab again inserts my-completion- a second time, so that I now have ls my-completion-mycompletion-.
My actual code includes a few lines to check for multi-line commands, but makes no changes to the completion code. With this version of the code, I hitting tab on a line that starts with one of the program names in my-commands I am presented with a list of the possible arguments to complete the command with, but nothing is inserted in the buffer, and the list does not get narrowed by typing the first few letters of an argument.
I've been over the manual, but I can't figure out the correct way to write a completion-at-point function. Any ideas what I'm missing?
I have looked briefly at pcomplete, but the didn't really understand the 'documentation', and didn't make any progress.
The problem seems to be with the way you're finding start and end to return the boundaries of the argument at point. I didn't spend long enough debugging it to be sure of the details, but I think if you call the function interactively you'll see that it returns the same value for start and end, and this means that the completion UI doesn't know to use the argument at point to select from the completion table you've passed it.
Changing the last part of your function to the following seems to be one fix:
(when (member* command my-commands :test 'string= :key 'car)
;; If the command is one of my-commands, use the associated completions
(goto-char pt)
(let ((start
(save-excursion
(skip-syntax-backward "^ ")
(point))))
(list start pt (cdr (assoc command my-commands)) :exclusive 'no)))))))
This gives the expected results when added as an element of completion-at-point-functions.
Here I've used skip-syntax-backward instead of regexp search, which I think is slightly more idiomatic Elisp for this kind of thing. It just says to move point backwards across anything that is not in syntax class "whitespace". The skip-syntax functions return the distance moved rather than the value of point, so we have to add a call to point at the end of the save-excursion.
If you do use regexp searches in a function like this, it's usually a good idea to pass t for the fourth argument, noerror, so that it doesn't pass on errors to the user if it fails to match. This does mean that you have to check for yourself whether the return value is nil, though.
Finally, instead of push to add the completion function you might want to use add-hook as follows:
(add-hook 'completion-at-point-functions 'my-completion-at-point nil t)
This does two useful things: it checks whether your function is already in the hook before adding it, and (by passing t for the fourth argument, local) it only adds the function to the buffer-local value of the completion-at-point hook. This is almost certainly what you want, since you don't want to use these completions in every other Emacs buffer when you press the TAB key.

Copy Character Down in Emacs

I wrote an interactive function which inserts the "character above the point" in to the current line. For instance, given a line containing "12345" followed by a line "abcdef" and the point sitting at the letter "c", copy-down would make the second line become "ab3cdef". copy-down again would make the second line become "ab34cdef".
My function fails (using GNU Emacs 23.3.1 under windows 7) the second time I invoke it by inserting the text from the first invocation and not advancing properly. If I put any emacs "manipulations" in-between invocations, it works fine. (For instance if I do a copy-down, "left arrow", "right arrow", copy-down it works fine for both invocations.)
Here's my function:
(defun copy-down ()
"Grab the character in the line above and insert at the current location."
(interactive)
(let ((beg (progn (previous-line 1) (point)))
(end (progn (forward-char) (point))))
(backward-char)
(next-line 1)
(insert-buffer-substring (current-buffer) beg end)))
If it matters, I usually tie my function to a key: (global-set-key [f5] 'copy-down)
PS. I got used to using this capability in the editor I used before switching to emacs many years ago and I miss it in GNU Emacs. :-(
What you have works just fine for me. That said, previous-line has interaction with other settings (specifically goal-column) and generally shouldn't be used when writing elisp. Instead you should use (forward-line -1). But, of course, your code relies on the goal-column... You can test this by running Emacs without your other configurations, ala emacs -q.
Here's a slightly different version of your code that doesn't rely on goal-column:
(defun copy-down ()
"Grab the character in the line above and insert at the current location."
(interactive)
(let* ((col (current-column))
(to-insert (save-excursion
(forward-line -1)
(move-to-column col)
(buffer-substring-no-properties (point) (1+ (point))))))
(insert to-insert)))
If the problem isn't with using previous-line, then I don't imagine my code would make much of a difference.
Another option you have is to try running it in the debugger to see where your code breaks down. Move the point inside the defun for copy-down and type M-x edebug-defun, and the next time you run it you'll be able to step through the code. Docs for edebug can be found here.
You need to use let* instead of let. The former allows you to use earlier values in later forms in the same statement.
BTW, that's an unconventional way to write elisp, you might want to look at some other code samples.
EDIT:
Hey, someone completely rearranged your function! It might work now.
Try
(defun copy-down (arg)
(interactive "p")
(let ((p (+ (current-column) (point-at-bol 0))))
(insert-buffer-substring (current-buffer) p (+ p arg))))
which has the additional functionality of taking a prefix argument to copy n (default to 1) characters down.

Read Statement Being Skipped Over, Unbound Variable

I'm still working on my number guessing game in Common Lisp, and I've reached a standstill. When the following code is invoked:
;;;; number-game.lisp
;;;;
;;;; Andrew Levenson
;;;; 10/25/2010
;;;;
;;;; Simple number guessing game. User has
;;;; five guesses to determine a number between
;;;; one and one hundred, inclusive (1-100).
;;; Set global variable for the target number:
(defparameter *target* nil)
;;; Set the iterator so we may check the number of guesses
(defparameter *number-of-guesses* 0)
;;; Welcome the user
(defun welcome-user ()
(format t "Welcome to the number guessing game!~%"))
;;; Prompt for a guess
(defun prompt-for-guess ()
(format t "Please enter your guess (1-100): ")
(finish-output nil) ; nil directs finish-output to standard IO
(check-guess 'read-guess))
;;; Read in a guess
(defun read-guess ()
(let (guess (read)))
(if (numberp guess) ; If true, return guess. Else, call prompt-for-guess
(progn
(setq *number-of-guesses* (+ *number-of-guesses* 1))
guess)
(prompt-for-guess)))
;;; Check if the guess is higher than, lower than, or equal to, the target
(defun check-guess (fn)
(setq guess (funcall fn))
(if (equal guess *target*)
(equal-to)
(if (> guess *target*)
(greater-than (guess))
(if (< guess *target*)
(less-than (guess))))))
;;; If the guess is equal to the target, the game is over
(defun equal-to ()
(format t "Congratulations! You have guessed the target number, ~a!~%" *target*)
(y-or-n-p "Play again? [y/n] "))
;;; If the guess is greater than the target, inform the player.
(defun greater-than (guess)
(format t "Sorry, ~a is greater than the target.~%" guess)
(if (< *number-of-guesses* 6)
(prompt-for-guess)
(game-over)))
;;; If the guess is less than the target, inform the player.
(defun less-than (guess)
(format t "Sorry, ~a is less than the target.~%" guess)
(if (< *number-of-guesses* 6)
(prompt-for-guess)
(game-over)))
;;; If the player has run out of guesses, give them the option
;;; of playing the game again.
(defun game-over ()
(y-or-n-p "You have run out of guesses. Play again? [y/n] "))
;;; Play the game
(defun play ()
;; If it's their first time playing this session,
;; make sure to greet the user.
(unless (> *number-of-guesses* 0)
(welcome-user))
;; Reset their remaining guesses
(setq *number-of-guesses* 0)
;; Set the target value
(setq *target*
;; Random can return float values,
;; so we must round the result to get
;; an integer value.
(round
;; Add one to the result, because
;; (random 100) yields a number between
;; 0 and 99, whereas we want a number
;; from 1 to 100 inclusive.
(+ (random 100) 1)))
(if (equal (prompt-for-guess) "y")
(play)
(quit)))
I get the error
* (play)
Welcome to the number guessing game!
Please enter your guess (1-100):
debugger invoked on a UNBOUND-VARIABLE in thread #<THREAD
"initial thread" RUNNING
{AA14959}>:
The variable GUESS is unbound.
Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [ABORT] Exit debugger, returning to top level.
(READ-GUESS)
Why is it skipping over the read statement in read-guess? I'm pretty sure the variable is unbound because it's not letting me enter anything in.
Check the syntax of LET.
Example:
(let ((variable (compute-some-value)))
(do-something-with variable)
(do-some-more))