there.
I'm new to eshell,and now i come to a problem that how can i script it.
i've tried
(rm ~/somefile)
and it worked.so is every command like this?how should i write conditional code and loop?and what should i customize to make the system execute my script using eshell other than others ,like bash,by default?
i'm not very good at english,and i hope you can understand what i mean.i'll also appreciate any correction to my english expression?
You can call any elisp command/function/operator from eshell; I suppose that means you can script it using elisp (See GNU Emacs Lisp Reference manual). Example:
Welcome to the Emacs shell
~ $ (defun foo (n) (if (= 0 (mod n 2)) "Even." "Odd!"))
foo
~ $ foo 2
Even.
~ $ foo 3
Odd!
~ $
Related
I just installed racket-mode in my emacs 24.3, when I run the REPL via racket-repl command, the REPL starts correctly, but some of the racket procedures/functions aren't recognized. i.e
> (class object%)
; class: undefined;
; cannot reference undefined identifier
> (enter! "test.rkt")
; enter!: undefined;
; cannot reference undefined identifier
The current value of the racket-racket-program variable is set to Racket.exe. On the other hand if I just run Racket.exe from the windows command line, then the REPL works as expected.
Any help with this is greatly appreciated.
When you do a racket-repl, that REPL initially opens with the equivalent of #lang racket/base. At the prompt you could type (require racket) to get the bigger language, including the class stuff like object%.
(There's an open issue about this. Feel free to chime in there.)
Instead of typing (enter! "test.rkt") at the prompt, try ,run test.rkt.
In fact, the easiest way to do this is with an Emacs buffer open on test.rkt. Then you can press C-c C-k a.k.a. M-x racket-run.
(Note that this leaves point in the test.rkt buffer. If you prefer point to go to the REPL, instead you can use M-x racket-run-and-switch-to-repl, bound by default to F5 like in DrRacket.)
TL;DR the most common use pattern with racket-mode is:
Visit a .rkt file in a buffer.
"Run" it with C-c C-c (or F5).
Explore in the REPL interactively as you wish.
Goto 2.
If your file contains #lang racket then the REPL picks up what language to use.
If no such line is present my guess is that racket/base is used -- and object% is not defined in racket/base.
Try entering this program:
#lang racket
(+ 1 2)
Then start the REPL and try your snippet again.
So I just got Land of Lisp and started to do the first program.
I have a couple questions.
Is there a way to just write some code and run it through a compiler, or interpreter, and not use the REPL thing? I don't like it much. I can't seem to go back if I messed up. It just kinda says "Ha you screwed up, retype that whole function."
I would also like to know what the point of REPL is.
Non-REPL work flow
Edit your file
Compile the file using compile-file; fix errors and warnings; repeat.
Load the file using load; evaluate the form you want; repeat
Example
$ cat > f.lisp <<EOF
(defun f (x) (if (zerop x) 1 (* (f (1- x)) x)))
EOF
$ clisp -q -norc -c f.lisp
;; Compiling file /home/sds/f.lisp ...
;; Wrote file /home/sds/f.fas
0 errors, 0 warnings
$ clisp -q -norc -i f.fas -x '(f 10)'
;; Loading file f.fas ...
;; Loaded file f.fas
3628800
$
The Right Way
Use an IDE, e.g., Emacs with SLIME.
This way, you edit the code in an editor which supports auto-indent and shows you help for each standard symbol.
You compile and test the functions as soon as you write them, giving you a very short development cycle. Under the hood this is accomplished by the IDE interacting with the REPL (this answers your last question).
What is REPL?
Read-Eval-Print loop is a faster, more versatile version of the Edit-Compile-Run loop.
Instead of operating in terms of whole programs (which can be slow to compile and whose execution can be tedious to navigate to the specific location being tested), you operate in terms of a specific function you work on.
E.g., in gdb, you can execute a function with print my_func(123), but if you change my_func, you have to recompile the file and relink the whole executable, and then reload it into gdb, and then restart the process.
With Lisp-style REPL, all you need to do is re-eval the (defun my-func ...) and you can do (my-func 123) at the prompt.
I am quite new to Emacs.
When running Emacs' python interpretor, it does
>>> print(24)
print(24)
24
Is there a way I can prevent the re-printing of my input and make it as below?
>>> print(24)
24
Thank you so much :)
The trick here is that the buffer you're running the python process in doesn't have comint-process-echoes set.
There are a couple of other questions that are relevant to your problem.
How to turn off the echoing
How to set emacs so it always turns off echoing
But the basic gist is you need to customize the value of comint-process-echoes. If you are new to emacs, you might not know that most customizations are done using emacs lisp, where setting a variable looks something like this:
(setq variable-name new-value)
In this case, the variable we want is comint-process-echoes so the lisp we want to evaluate is:
(setq comint-process-echoes t)
Where t is lisp-speak for "true."
So, to borrow the advice of the first link above, to actually tell emacs to evaluate this lisp code, use the M-: (meta+colon) command. From the python shell buffer, type meta+colon, then type (setq comint-process-echoes t) then hit return. Your problem should be solved.
I'm a beginner programmer and am going through the book "Land of Lisp".
I have been typing in the examples from the book with the REPL. Is it possible to save my current program as a .lisp file so I can load it and continue working on it later? I know I can just create the .lisp files in a text editor and load them in, but I am enjoying using the REPL in full screen mode to do the examples.
Short answer
No. Once you enter a function into the REPL, the source form is gone and you just have the interpreted or compiled form. You can do some clever things, but I suspect you don't want to deal with them right now.
Long answer
Use Emacs and SLIME
First, I know you are enjoying the REPL, but I would encourage you to look at some of the editors with Lisp support, such as Emacs with SLIME (http://common-lisp.net/project/slime/) that gives you the best of both worlds. You type into the editor, it types into the REPL, and you really don't know the difference from how you are doing things now. Then you can copy and paste the functions you like into a "proper" .lisp file. The other advantage of using Emacs is that it's based on a variant of Lisp called Elisp, so you can program your editor in Lisp. You can pretty print your code, reformat and refactor it into multiple functions, and do all kinds of excellent things.
Enough preaching!
If you still just want to type clisp and play at the REPL, then you still have options.
If you want to log the output of your REPL session, check out DRIBBLE. It will log your session to a file, and you can edit that later to extract what you want.
For example, this is a simple session:
ataylor:~ $ clisp
blah blah
[1]> (dribble "/Users/ataylor/jerome.lisp")
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"/Users/ataylor/jerome.lisp">
[2]> (defun add-two (a b) (+ a b))
ADD-TWO
[3]> (add-two 1 2)
3
[4]> (dribble)
#<CLOSED OUTPUT BUFFERED FILE-STREAM CHARACTER #P"/Users/ataylor/jerome.lisp">
[5]>
Bye.
Viewing the contents of the file is easy, but it can get big quickly.
ataylor:~ $ cat jerome.lisp
;; Dribble of #<IO TERMINAL-STREAM> started on 2011-09-14 18:16:57.
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"/Users/ataylor/jerome.lisp">
[2]> (defun add-two (a b) (+ a b))
ADD-TWO
[3]> (add-two 1 2)
3
[4]> (dribble)
;; Dribble of #<IO TERMINAL-STREAM> finished on 2011-09-14 18:17:16.
You could copy (defun add-two (a b) (+ a b)) and paste it into a file for later.
Loading that file (I added it to jerome1.lisp) is very simple.
ataylor:~ $ cat jerome1.lisp
(defun add-two (a b) (+ a b))
ataylor:~ $ clisp
blah blah
[1]> (load "/Users/ataylor/jerome1.lisp")
;; Loading file /Users/ataylor/jerome1.lisp ...
;; Loaded file /Users/ataylor/jerome1.lisp
T
[2]> (add-two 1 2)
3
[3]>
Bye.
Saving a session
The easiest thing you can do is saving your Lisp session to an image. It will save all of the functions you've created or compiled, along with most state. When you load it at your next session, it will be almost as if you hadn't exited clisp. The way to do this is implementation dependent, and differs between clisp, sbcl, etc. I'll show you what you would do for clisp.
The problem with this is that you can't open a file and edit it, post it on github, or whatever. I'll give a brief example.
ataylor:~ $ clisp
blah blah
[1]> (defun add-two (a b) (+ a b))
ADD-TWO
[2]> (add-two 1 2)
3
[3]> (EXT:SAVEINITMEM)
;; Wrote the memory image into lispinit.mem (3,422,616 bytes)
Bytes permanently allocated: 171,840
Bytes currently in use: 3,243,400
Bytes available until next GC: 808,130
3243400 ;
808130 ;
171840 ;
1 ;
65640 ;
7834
[4]>
Bye.
Note that message about where clisp wrote the memory image. You'll give that back to clisp with the -M flag when starting it the next time.
ataylor:~ $ clisp -M lispinit.mem
blah blah
[1]> (add-two 1 2)
3
I wanted a better answer to this same question for the following reasons:
(1) When learning a language, oftentimes you more want to experiment than to actually write a full application.
(2) When experimenting, it can be really nice to look through your history and see what you entered and what results you got.
(3) I come from the world of bash, where you can literally dump your command history into a text file and call it a "script" (though this is only a tiny fraction of the power you can get with actual bash scripting, it still serves the purpose of after-the-fact-automation, with a little cleanup.)
So, from the world of bash, I just went with the simple route—I automated a cleanup of the stdout dump. If you're using a terminal with scrollback history (anything but a virtual terminal—and if you code lisp in a VT you're just strange) to play with clisp interactively, just copy the entire terminal scrollback history to a file, then run:
sed -n -e 's/^\[[0-9]\+\]> //;tstuff' -e 'b;:stuff' -e 'p;:rep' -e 's/([^()]*)//;trep' -e '/^[^(]*$/{s/.*//;h;d;};h;n;p;x;G;brep' myfile.lisphist > myfile.lisp
Magic! You have a working lisp program. (Obviously you should clean it up; the purpose is to have your history, not really to use it as a program. But it will only contain the lisp commands you entered, not the output/result, so it could be used directly as the program file.)
Then you can run clisp -repl myfile.lisp and voila! You're back where you got to in the last session. Of course, to load it interactively, you should take out the final (quit) line. :)
As for that sed command, what it does is look for the clisp prompt [number]>, then print that line (removing the prompt) and then try to balance the parentheses. If it succeeds, it scans for the next prompt; if the parentheses don't balance, it prints lines until they do.
Actually, it can be simplified slightly to:
sed -n -e '/^\[[0-9]\+\]> /{s///;p;:rep' -e 's/([^()]*)//;trep' -e '/^[^(]*$/d;h;n;p;x;G;brep' -e '}' myfile.lisphist > myfile.lisp
(Note: I don't use emacs; I've been using just clisp called directly at the command line. I don't know if there is a scrollback history within SLIME.
I want to run a shell command within Emacs and capture the full output to a variable. Is there a way to do this? For example, I would like to be able to set hello-string to "hello" in the following manner:
(setq hello-string (capture-stdout-of-shell-command "/bin/echo hello"))
Does the function capture-stdout-of-shell-command exist, and if so what is its real name?
Does shell-command-to-string meet your purpose?
For example:
(shell-command-to-string "/bin/echo hello")
Hope this helps.
I have a suggestion to made that extends Ise Wisteria's answer. Try using something like this:
(setq my_shell_output
(substring
(shell-command-to-string "/bin/echo hello")
0 -1))
This should set the string "hello" as the value of my_shell_output, but cleanly. Using (substring) eliminates the trailing \n that tends to occur when emacs calls out to a shell command. It bothers me in emacs running on Windows, and probably occurs on other platforms as well.