large output in common lisp linux terminal - lisp

I wrote a clisp program that prints out n sets of x*y random integers. I'd like to make n=100, but I can't copy and paste the whole thing because my linux terminal doesn't go back far enough, for lack of a better word.
I'd like the simplest way possible to capture 2200 lines of linux terminal readout.

From Lisp there are various ways to have your output in a file.
you can have the REPL interaction saved to a file. See the DRIBBLE function.
you can also enclose your code with WITH-OPEN-FILE.
example:
(with-open-file (*standard-output* "/tmp/foo.text" :direction :output)
(your-print-function-here))

Further to the comment above, I use sbcl on the command line to capture output. Simply load your library and then evaluate what you need.
example:
sbcl --noinform --load "compass.lisp" \
--eval "(print (table-egs (cocomo81)))" \
--eval "(quit)" > copy.txt

There are several different Linux terminal programs. They all have more or less accessible ways to configure the number of scrollback lines. I am not on my Linux box right now, but I recall this being in a relatively obvious place under the Preferences menu option for GNOME's terminal, and I would imagine KDE is similar.
I second the recommendation to use shell redirection, though; that's the more generally useful tactic.

Related

How the input is provided in LISP?

I am totally new to lisp, but I came accross this code https://github.com/wjur/sym-diff-lisp/blob/master/sym-diff.lsp which calculates derivatives in lisp and I wanted to know how to run it. I see the examples in comments in the beginning but I am not sure how to run it.
I just installed clisp in ubuntu and tried to run 'clisp sym-diff.lsp' but I dont know where am i supposed to pass the exact functions that I want to differentiate. Should I pass it as arguments when running sym-diff.lsp?
Start CLISP - you should have a terminal window, which is waiting for you to do something. This is your REPL.
You have to load the code, thus:
(cd "file-location")
(load "filename")
Once you done that then you can type in your examples into the REPL.

Setting TERM variable for Emacs shell

Is it possible to set the TERM environment variable for Emacs shell to some other value than "dumb"? In order to make TRAMP work I'm adjusting some parts of .bashrc on remote machines according to $TERM == "dumb" condition but for the shell I'd like these ignored (opposite approach - setting the TERM for TRUMP - would also be applicable).
Let me give your answer in two parts: first, one that technically answers your question but isn't very useful, and second, one that probably answers your needs even though it takes a different tack to get there.
Setting TERM in the shell
When starting shell-mode with M-x shell, Emacs starts the shell you want (usually the same as your login shell, but this can be changed if you really want to) and then sources a file, if it exists, based on the shell's name. The places it looks are
~/.emacs_$SHELLNAME
~/.emacs.d/init_${SHELLNAME}.sh
In this file you can set TERM. For instance, if you use zsh, and create the file
# ~/.emacs_zsh or ~/.emacs.d/init_zsh.sh
export TERM=emacs
then you'll get what you asked for: shells started with M-x shell will have the TERM set to emacs instead of dumb.
While this technically answers your question, though, it's not particularly useful—if you try it, here's what you'll get when you start a shell:
zsh: can't find terminal definition for emacs
$ echo $TERM
emacs
$
The issue here is that shell-mode doesn't implement any terminal emulation. In other words, dumb is exactly the right TERM value to use.
(Note there are modes that emulate terminals—see, for instance, M-x ansi-term—and they set TERM=eterm-color or similar, but they're designed to let you use Emacs as an xterm replacement for visual-mode shell commands, whereas M-x shell is designed to let you run shell commands while still interacting with the input and output in an Emacs-y way.)
When you choose a TERM value that isn't supported by termcap, you'll get the above error and some programs may get confused about what's happening (and some will refuse to run at all). If you select a full-featured TERM value like xterm instead, you'll get "line-noise" characters as the programs attempt to send formatting codes to a terminal emulator that isn't there.
You could probably get away with finding some termcap that was limited enough in capabilities that it won't bother you with noise too much, but if this is just to let you differentiate Emacs interactive shells from either Emacs non-interactive shells or non-Emacs interactive shells, there's a better choice.
This choice, in fact, isn't even sufficient. That's because this special shell script is loaded by both shell-mode and by TRAMP, so the above still wouldn't let you differentiate the two—you'd just get emacs in both cases instead of dumb!
So this is where the better choice comes in:
Using the INSIDE_EMACS environment variable
While, as you noted, the interactive shell and TRAMP both set TERM to dumb by default, they also set an environment variable INSIDE_EMACS.
Its existence (or not) alone is useful for your shell startup scripts, but its power for your use case lies in its value—which, for interactive (M-x shell) use, is something like 25.2.2,comint, but for TRAMP is 25.2.2,tramp.
So, to check for the three cases, here's what you can do (and what I personally have done myself in my ~/.zshrc for many years):
# Setup for all shells--Emacs or not, interactive or not, goes
# here
PATH=...
source $my_functions_file
# Now dumb terminals
if [[ "${TERM}" == "dumb" ]]; then
# Here put anything you want to run in any dumb terminal,
# even outside Emacs.
PATH=...
alias lsF='ls -F'
etc
# Now, just configs for shells inside Emacs
case ${INSIDE_EMACS/*,/} in
(comint)
do_comint_stuff
;;
(tramp)
do_tramp_stuff
;;
(term*)
# For M-x ansi-term, etc., you get a value like
# 25.2.2,term:0.96, but those shouldn't coincide with
# TERM being `dumb`, so warn....
echo "We somehow have a dumb Emacs terminal ${INSIDE_EMACS/*,/}" >&2
;;
("")
# Empty means we're $TERM==dumb but not in Emacs, do nothing
;;
(*)
# We shouldn't get here, so write a warning so we can
# figure out how else Emacs might be running a shell,
# but send it to stderr so that it won't break anything
echo "Something is wrong: INSIDE_EMACS is ${INSIDE_EMACS}" >&2
;;
esac
# finish shell setup for dumb now--the rest of the file will
# be skipped
return
fi
# Stuff for non-dumb, interactive visual, shells goes here
setup_prompt
setup_keybindings
etc
We don't reset TERM to a different value inside the case statement because dumb is exactly what it should be.
Note that above, in the (tramp) section of the case statement, you could do what you mentioned in your question—set TERM to something else just for TRAMP—but that would be a bad idea, since Emacs actually reads and acts on responses it gets from TRAMP shells, and the line noise would be an even bigger problem. TRAMP can do some really amazing things, but only when the shell output it's reading is in the format TRAMP expects.
(One final thing: using the code as above, with the INSIDE_EMACS checks nested instead the dumb terminal check, we don't have a single place to put code to be run in all Emacs-spawned shells regardless of type, including M-x ansi-term and its ilk. You could write a separate statement for that in your shell config... but that's exactly what ~/.emacs.d/init_${SHELLNAME}.sh is for, so probably a better choice if you need this for some reason.)

Perl: console / command-line tool for interactive code evaluation and testing

Python offers an interactive interpreter allowing the evaluation of little code snippets by submitting a couple of lines of code to the console. I was wondering if a tool with similar functionality (e.g. including a history accessible with the arrow keys) also exists for Perl?
There seem to be all kinds of solutions out there, but I can't seem to find any good recommendations. I.e. lots of tools are mentioned, but I'm interested in which tools people actually use and why. So, do you have any good recommendations, excluding the standard perl debugging (perl -d -e 1)?
Here are some interesting pages I've had a look at:
a question in the official Perl FAQ
another Stackoverflow question, where the answer mostly is the perl debugger and several links are broken
Perl Console
Perl Shell
perl -d -e 1
Is perfectly suitable, I've been using it for years and years. But if you just can't,
then you can check out Devel::REPL
If your problem with perl -d -e 1 is that it lacks command line history, then you should install Term::ReadLine::Perl which the debugger will use when installed.
Even though this question has plenty of answers, I'll add my two cents on the topic. My approach to the problem is easy if you are a ViM user, but I guess it can be done from other editors as well:
Open your ViM, and type your code. You don't need to save it on any file.
:w !perl for evaluation (:w !COMMAND pipes the buffer to the process obtained by running COMMAND. In this case the mighty perl interpreter!)
Take a look at the output
This approach is good for any interpreted language, not just for Perl.
In the case of Perl it is extremely convenient when you are writing your own modules, since in my experience the perl interpreter will refuse to reload a module (even when loading was attempted and failed). On the minus side, you will loose all your context every time, so if you are doing some heavy or slow operation, you need to save some intermediate results (whilst the perl console approach preserves the previously computed data).
If you just need the evaluation of an expression - which is the other use case for a perl console program - another good alternative is seeing the evaluation out of a perl -e command. It's fast to launch, but you have to deal with escaping (for this thing the $'...' syntax of Bash does the job pretty well.
Just use to get history and arrows:
rlwrap perl -de1

Get code from REPL

If I'm entering code into the REPL using clisp, as in the program you get when you do sudo apt-get install clisp, is there a way to take all the code you've entered so far and save it in a file? I'm a Lisp beginner so I don't know if that's a ridiculous request or not.
You can start output recording with the function DRIBBLE.
Other than that I would run CLISP from a terminal program which can save input / output.
As the minimum I would usually use Emacs, run a shell via M-x shell and start the Lisp there. That way the I/O goes into an Emacs shell buffer.
There is also SLIME, which sets up quite a bit more functionality inside Emacs to communicate with a 'slave' Common Lisp. A 'listener' (aka REPL) is part of that.
There is probably a better way, but... If you are using a decent terminal program, you should be able to select the text in the terminal and save it to file. This would include your typed input as well as output, so you would have to manually remove the output.

Debugging using emacs

GNU Emacs 23.2.1
GCC 4.4.4
I am using gdb-many-windows to debug.
I am just wondering is there anything better?
At the moment I am debugging a linked-list. The list is not that big. However, it would be nice to see all the elements' values. Instead of having to 'print sorted_queue->next->seconds' all the time.
The watch command works ok, if a value changes. However, sometimes its nice to see all the values you want to watch in a separate buffer for easy review.
Everytime I what to see what a value is I have to issue the command print (p) and the name of the variable. Just a lot of typing. One thing Visual Studio is good for is debugging. It would be nice to see Emacs with some of those features.
Many thanks for any advice,
You might like the Data Display Debugger, a.k.a. DDD:
As far as dumping of the data structures is concerned, GDB Python extensions might interest you. Then you could make 'print list' output '5 10 2 4 50' or whatever presentation you like.
You can read introduction to GDB Python scripting here.