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

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

Re: REPL


From: Pascal Bourguignon
Subject: Re: REPL
Date: 04 Dec 2004 13:17:45 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Oliver Scholz <address@hidden> writes:

> [Since M-x ielm has already been pointed to by others ...]
> 
> Elvin Peterson <address@hidden> writes:
> [...]
> > IIRC, the REPL code for COMMON LISP is just a couple of lines, so I
> > was hoping someone would post similar stuff for emacs.
> 
> Sure, to get a very basic REPL in Emacs is simple:
> 
> ;; Because we use `loop' for the looks of it:
> (require 'cl)
> 
> (let* ((buffer (generate-new-buffer "*tmp*"))
>        (standard-output buffer))
>   (switch-to-buffer buffer)
>   ;; The REPL:
>   (loop (print (eval (read)))))
> 
> The extra code in ielm is to get something a tiny little bit more
> comfortable.

Good idea. It could be useful to handle errors, history variables and
termination:


(defvar +++ nil)
(defvar ++  nil)
(defvar +   nil)
(defvar /// nil)
(defvar //  nil)
(defvar /   nil)
(defvar *** nil)
(defvar **  nil)
(defvar *   nil)
(defvar -   nil)

(defun repl ()
  (interactive)
  ;; I prefer to do it in the current buffer.
  (let ((standard-output  (current-buffer)))
    (switch-to-buffer standard-output)
    (block nil
      (loop for hist from 1 do
            (setf +++ ++   
                  ++  + 
                  +   -
                  -   (read))
            (insert (format  "[%d]> %S\n" hist -))
            (when (or (member - '((quit)(exit)(continue))))
              (insert "Good bye.\n")
              (return))
            (setf /// //   
                  //  /
                  /   (list (condition-case G61912 (eval -) (error G61912)))
                  *** **
                  **  *
                  *   (first /))
            (insert (format (format "%%%ds %%S\n\n" 
                              (length (format "[%d]>" hist))) "-->" *))))))

M-x repl RET

[1]> (dotimes (i 5) (if (oddp i) (princ "odd: %d
" i) (princ "even: %d
" i)))
 --> (invalid-function 0)

[2]> (princ "toto")
toto --> "toto"

[3]> (dotimes (i 5) (princ (format (if (oddp i) "odd:  %d
" "even: %d
") i)))
even: 0
odd:  1
even: 2
odd:  3
even: 4
 --> nil

[4]> (/ 5550690 30)
 --> 185023

[5]> (quit)
Good bye.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.


reply via email to

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