How to use key-bindings when using SBCL & Clozure CL on Terminal - terminal

I'd like to use key-bindings when I use SBCL & Clozure CL on Terminal.
You can use key-bindings when you use Clisp on Terminal. For example, when you type "Control-p", you can list the history of codes you typed. You can even delete a letter when you type "Control-d".
Do you know how to do the same things on SBCL & Clozure CL on terminal as you can do with Clisp?
I use Macbook, and the OS is 10.11.6.
I'd appreciate your help.

These characteristics are present in CLISP since it is integrated with readline, a package that provides a way to edit the command line in a shell (see the CLISP summary). SBCL and CCL are not integrated with readline, so it is not possible to use those commands with them.
The best solution to use them in a powerful environment with lot of commands is through the combination of the Emacs editor together with the SLIME package. Many tutorials exists on the net on how to install and use them. An added benefit is that Emacs and SLIME work for any implementation of Common Lisp.

It's possible if you install "rlwrap" through MacPorts.
If you run sbcl with "rlwrap sbcl" on Terminal, you can use the key-bindings.
(Note: This answer is based on the article: How to customize the SBCL REPL?
How to customize the SBCL REPL?
)

Related

Without Emacs, how does Bash command line editing work in Emacs mode?

Without Emacs installed on my Linux system, Bash command line editing default mode is still Emacs. How does this work without Emacs present?
I tried to search the Bash source code, but still can't understand. Does Bash integrate Emacs within itself?
$ set -o
allexport off
braceexpand on
emacs on
: :
vi off
xtrace off
The GNU(1) readline library is what does the heavy lifting for bash (and any other interactive input systems that choose to use it).
That's the source code you should be looking at, if you want to understand how it works.
The readline packages are hosted alongside bash.
(1) Yes, the same GNU that's responsible for the emacs editor.
The reference to emacs has little to do with the emacs editor itself. It refers to the 'style' of key bindings used by the GNU readline library which bash uses. Readline supports two key binding modes - emacs style and vi style. The default is usually emacs style. The readline library is very powerful, but to be honest, in 25 years of Linux use, I've never bothered with most of the advanced features and have never even tried the vi mode (even when VI was my default editor).
Read the section on readline in the bash manual for more details.

How to get rlwrap to work inside Emacs' shell / eshell?

When I try to get rlwrap to work inside an Emacs (version 24.3.50.1) shell buffer (either M-x shell or M-x eshell), I get this error message:
rlwrap: Oops, crashed (caught SIGFPE) - this should not have happened!
For example:
rlwrap telnet google.com 80
wors fine from a regular terminal (like xterm), but crashes from a shell inside an Emacs buffer.
Several people are having similar issue but the only "answer" I could find so far is a "RTFM" style answer on mailing lists. I did read that part of Emacs' doc and I don't understand what I'm supposed to do to make rlwrap work from within Emacs.
Try using M-xansi-term instead of shell/eshell.
shell and eshell do not provide terminal emulation functionality, but readline requires terminal emulation capabilities to move the cursor etc.

Making the PATH and other environment variables available in emacs

Working with emacs in OSX can some times be troubling.
I interchange with using the Terminal.app application and the emace shell both of these use bash, and both seem to work, I do however have the problem that the environment is not setup identically in both.
I'm running a clean installation of OSX Lion, and emacs 24 from emacs and would like for the environment variables in the emacs shell to make the same as in the Terminal.app under osx. How do I fix that?
Just install the awesome little package exec-path-from-shell and you're done! It will automatically set your emacs exec-path to be same as the PATH in your zsh/bash config.
It will also allow you to copy the values of other shell variables like this:
(exec-path-from-shell-copy-env "PYTHONPATH")
If your environment variables are being set in ~/.bash_profile, Create a file called ~/.emacs_bash and put in it the following:
. ~/.bash_profile
Don't forget the newline at the end of that line or it won't be executed.
~/.emacs_bash is loaded by emacs when you run bash from within it, such as when using M-x shell or shell-command so you can use it to set any environment variables you want available.
Open Emacs.app from within Terminal.app (instead of from Finder):
$ emacs
This works for me using Emacs from Homebrew with Cocoa (brew install emacs --with-cocoa). It launches Emacs.app in a Cocoa GUI window (use emacs -nw to fallback to terminal UI). All the environment variables I have set in my bash environment appeared to be set in Emacs.
I have not tried this with Emacsformacosx. I personally prefer Emacs from Homebrew as I heard Emacsformacosx is not compiled with certain extensions I use such as TLS and Imagemagick... you can compile Emacs with those extensions using brew install emacs --with-XXX.
As of 2019, exec-path-from-shell will only pass PATH to Emacs, If you want to pass all environments, you can try:
https://github.com/ersiner/osx-env-sync/

How can I make bash deal with long param using "getopt" command in mac?

I want to make my bash script deal with long parameters. I found getopt, but it isn't supported in OS X. Can anyone tell me why getopt was implemented by BSD, but not GNU?
I tried building getopt in GNU C lib, but it failed for my poor skills with Linux.
Did anyone do this work?
There is a brew bottle for getopt.
Just run brew install gnu-getopt.
You can either specify the path for it like
/usr/local/Cellar/gnu-getopt/1.1.6/bin/getopt
Or use brew link --force gnu-getopt so it will be linked in /usr/local/bin/
Just be aware that forcing linking might be corrupting your system (as it replaces the system getopt by the gnu one).
See maybe later answer suggesting to define FLAGS_GETOPT_CMD (though few comments state issues with it).
I recommend using Homebrew to install gnu-getopt and then adding $FLAGS_GETOPT_CMD to your ~/.bash_profile file to specify the cmd path for getopt, pointing at the homebrew location, like so:
brew install gnu-getopt
Then follow directions from brew to add to your local path:
sudo echo 'export PATH="/usr/local/opt/gnu-getopt/bin:$PATH"' >> ~/.bash_profile
Then you can add FLAGS_GETOPT_CMD:
sudo echo 'export FLAGS_GETOPT_CMD="$(brew --prefix gnu-getopt)/bin/getopt"' >> ~/.bash_profile
Open a new terminal, or run . ~/.bash_profile in existing terminal to load changes
Run echo $FLAGS_GETOPT_CMD to confirm it was actually set in your console
It's generally a better idea to use getopts instead, and stick with short options. You can see getopts in action in this StackOverflow Q&A. Short options are more standard throughout OSX command line tools, and consistency is a good thing.
Also, getopts is built in to bash, so it's definitely available in OSX, as well as every other platform that can run bash.
Note that there is a getopt is also available in OSX. From Terminal, type man getopt to see its documentation. It doesn't support long options. This is a good reason not to use long options when you're writing tools to run on OSX.
If you want to do this anyway, you can install getopt from macports. Alternately, if you want better portability, you can roll your own long argument handling.
Post some code, and we'll help debug it.

mit-scheme REPL with command line history and tab completion

I'm reading SICP and I'm using mit-scheme installed on my os x 10.8 laptop via homebrew.
Everything works as advertised, however I'm spoiled by the ease with which I get tab completion and command line history in REPL's for runtimes like Python and Node.js.
I'm not looking for anything heavy duty, but these features are pretty easy to come by in modern REPL's (it's just a simple startup file in Python and can be implemented in a few lines in Node.js).
Is there an easy way to get tab completion and command history in the mit-scheme REPL without a heavy-duty application or having to switch to emacs (i.e. in an xterm terminal)?
Install the readline wrapper:
brew install rlwrap
Once installed, rlwrap scheme will give you persistent history, paren matching, and tab completion. I typically use rlwrap with the following arguments:
-r Put all words seen on in- and output on the completion list.
-c Complete filenames
-f Specify a list of words to use for tab completion. I'm using an abridged list of bindings from the MIT Scheme Reference Manual.
Rather than republish the list here, you can find it in this gist.
I have this file stored in "$HOME"/scheme_completion.txt
rlwrap -r -c -f "$HOME"/scheme_completion.txt scheme
1 ]=> (flo:a <tab tab>
flo:abs flo:acos flo:asin flo:atan flo:atan2
1 ]=> (flo:abs -42.0)
;Value: 42.
I would actually recommend that you use Emacs, and use Geiser within that to access both the REPL and to help with scheme file editing. It also makes dealing with various Scheme REPLs such as Chez Scheme, Racket, MIT Scheme, Guile, Chicken, Gambit and Chibi Scheme effortless. Emacs remains very well tuned to use with Scheme and LISP. Highly recommended.

Resources