How to use arrow keys in mzscheme/guile REPL? - terminal

When I'm typing in the REPL of guile or mzscheme
(define x 15)
and then press arrow key I get
(define x 15)^[[D
Is it a terminal problem? or something needs to be configured? It's painful to rewrite a line for a simple edit

I found the answer for Guile, adding this in ~/.guile will cut it:
(use-modules (ice-9 readline))
(activate-readline)

I do not know what guile is, but for mzscheme, you can input
(require readline)
To interactively enable the arrow-key behavior you're looking for. To enable this behavior when the REPL starts, run it using
mzscheme -il readline

Related

How do you terminate a process inside a shell in emacs?

I work a lot in a shell inside emacs (on a Mac). It used to be that, when I typed control-Q control-C, that would kill a process running in the shell. About a year ago, though, that stopped working for some reason; now it has no effect. Anyone know why, or another way of doing this?
I had the same problem using term-mode.
I bound term-interrupt-subjob to my keybinding of choice with:
(add-hook 'term-mode-hook (lambda ()
(define-key term-raw-map (kbd "C-'") 'term-interrupt-subjob)
I am not sure which mode you are using to interact with a shell from Emacs (the inferior shell, Emacs shell, or the terminal emulator). So you might have to modify this a little by replacing term-interrupt-subjob by, for instance, comint-interrupt-subjob and changing the mode hook.
To answer your questions in the comments:
You add the function in your .emacs file. For it to take effect, you need to evaluate it: you can select it and run M-x eval-region or you can restart Emacs.
If you are running M-x shell, you are using shell-mode.
Note that to know which major mode you are using, you can run describe-mode.
Since you are using shell-mode, you want to use add-hook 'shell-mode-hook instead of add-hook 'term-mode-hook and shell-mode-map instead of term-raw-map.
However, in shell-mode, C-c C-c does by default run the command comint-interrupt-subjob. So this should work out of the box as Rorschach said. I am not sure why it isn't working for you.
You can try:
(add-hook 'shell-mode-hook (lambda ()
(define-key shell-mode-map (kbd "C-c C-c") 'comint-interrupt-subjob)
to see if that helps. But this should already be set since it is the default, so it is really unclear whether it will solve your problem.

How do I type the code to the exercises in SICP?

I have been using the print eval loop, but should I be using something else?
Use DrRacket. Download it here: http://download.racket-lang.org/
The REPL is the place to do it, yes.
Most people use something on top of the REPL in the command line, like emacs, for example. I use xscheme an emacs library. The alternatives are not very good in my opinion, and using command line only is just intolerable.
If you're using Racket, just use DrRacket.
If you're using MIT Scheme, you may wish to use Edwin, a derivative of Emacs bundled with Mit Scheme, the scheme implementation SICP was originally built for. Edwin is an emacs derivative, so if you already know Emacs, you'll get along with Edwin fine. If you don't know emacs, the basics are:
C-f, C-b, C-n, and C-p for forwards, backwards, up and down, respectively.
C-Space to start selecting a region (highlight a piece of text to operate on). The region will stay there until you get rid of it with C-Space, or do something with it.
C-w to cut the region
C-k to cut the line, from your cursor onwards
C-y to paste
C-x C-e to execute the line of code directly behind your cursor in the scratch/repl buffer (the default window)
C-c C-c to halt execution of something you ran with C-x C-e
C-x C-c to quit
That should be enough to start using Edwin. It's incredibly nice to be able to just edit your definition of a function, re-evaluate the definition you edited, and have the new version start working in your repl environment on the fly.
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.
You can write the scripts in GNU Guile Scheme to implement the exercises in SICP. Most of the inbuilt procedures work with no problem and there is no difference in syntax.
In Linux,
Write the scripts using shebang notation at the beginning of the script file
#!/usr/..<address to guile interpreter> \
-e <name of procedure which should run first> -s
!#
An Example
#!/usr/local/bin/guile \
-e main -s
!#
(define main (args)
(display (+ 3 4))
(newline))
Make the file executable using chmod + <filename> and run it ./<filename>
P.S. Scheme files are saved using .scm file extension.

Copy/Paste in emacs ansi-term shell

I have configured my emacs to run zsh shell within ansi-term. However, copy/paste no longer works i.e. nothing is getting pasted from kill-ring to the terminal.
Changing the TERM to vt100, or eterm doesn't solve the problem.
Would appreciate any ideas or solution.
To provide context I have configured ansi-term as follows:
(global-set-key "\C-x\C-a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))
(global-set-key "\C-x\ a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))
You may want to simply switch between character mode and line mode while using the terminal. C-c C-j will run term-line-mode, which treats the terminal buffer more like a normal text-buffer in which you can move the cursor and yank text. You can switch back to character mode by running term-char-mode with C-c C-k.
As described in this lovely blog snippet, there's a function, term-paste, in term.el, that does exactly what you want. By default it's bound only to S-insert but the blog's recommended C-c C-y seems like a good suggestion.
ansi-term, in char-mode, takes the ordinary bindings for the terminal emulation. You need a new binding, plus a way to output to ansi-term correctly. I use this:
(defun ash-term-hooks ()
;; dabbrev-expand in term
(define-key term-raw-escape-map "/"
(lambda ()
(interactive)
(let ((beg (point)))
(dabbrev-expand nil)
(kill-region beg (point)))
(term-send-raw-string (substring-no-properties (current-kill 0)))))
;; yank in term (bound to C-c C-y)
(define-key term-raw-escape-map "\C-y"
(lambda ()
(interactive)
(term-send-raw-string (current-kill 0)))))
(add-hook 'term-mode-hook 'ash-term-hooks)
When you do this, C-c C-y will yank. It only does one yank, though, and you can't cycle through your kill-buffer. It's possible to do this, but I haven't implemented it yet.
The above solutions work well for copying text from some buffer to ansi-term, but they aren't able to copy text from ansi-term to another buffer (eg copy a command you just ran to a shell script you're editing). Adding this to my .emacs file solved that problem for me (in Emacs 24.4):
(defun my-term-mode-hook ()
(define-key term-raw-map (kbd "C-y") 'term-paste)
(define-key term-raw-map (kbd "C-k")
(lambda ()
(interactive)
(term-send-raw-string "\C-k")
(kill-line))))
(add-hook 'term-mode-hook 'my-term-mode-hook)
Note that if you want to bind kill/yank to a keystroke that starts with the ansi-term escape characters (by default C-c and C-x), and want this to work in the unlikely event that those change, you can instead define your keystrokes (without the leading escape) to term-raw-escape-map, as is done in user347585's answer.
These other solutions don't work well for me, switching between character mode and line mode causes ansi-term to stop working properly randomly, and setting ansi-term's term-paste to C-c C-y (based on Glyph's link), didn't work the code snippet was for term, not ansi-term:
(eval-after-load "ansi-term"
'(define-key ansi-term-raw-map (kbd "C-c C-y") 'term-paste))
I enabled xterm-mouse-mode, after that I was able to select text using mouse and copy using standard Mac command C button in ansi-term in emacs GUI in Mac OS X,

echo in inf-ruby

I am trying to get inf-ruby to work in Emacs. For the most part it works fine, except for the very annoying habit of echoing every input entered. Does anyone know what could be wrong?
I am using Carbon Emacs on OSX 10.5 with the default Ruby 1.8.6. My irb version is 0.9.5
The odd bit is that inf-ruby worked perfectly one time I opened it, but I can't figure out what I did differently that single time.
Add this to your init.el
;;
(defun echo-false-comint ()
(setq comint-process-echoes t))
(add-hook ’comint-mode-hook ’echo-false-comint)
;;
A little more explanation is on my blog post.

Opening browser from emacs script?

I have the following emacs lisp snippet that will launch my browser from within emacs and open the specified pages. However when I run it as a script from a shell nothing happens. What more do I need to do? I tried dropping (interactive).
#!/usr/bin/emacs --script
(defun surf-news ()
(interactive)
(progn
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
))
(surf-news)
A neat function I discovered yesterday is M-x webjump which has recently been added to emacs.
As Ryan Thompson mentioned above, you can use xdg-open in Emacs.
Change:
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
To:
(browse-url-xdg-open "http://news.ycombinator.com")
(browse-url-xdg-open "http://stackoverflow.com"))
I found the answer on this site, at the bottom, where it describes a Mac OS pty bug. I had to add this line and a slight delay between urls.
(setq process-connection-type nil)
Here's the complete solution.
#!/usr/bin/emacs --script
(setq process-connection-type nil);; pty's broken on the Mac
(defun surf ()
(progn
(browse-url "http://news.ycombinator.com")
(sleep-for 0.5); We need a delay
(browse-url "http://stackoverflow.com")
))
;;
;; This is what's going on behind the scenes
;;(setq url "http://www.google.com")
;;(start-process (concat "open " url) nil "open" url)
(surf)
On linux, if you are in GNOME, KDE, or probably almost any graphical environment, there is probably a command called xdg-open. This command basically takes its argument and opens it with the desktop default application. It handles both files and URLs. So generally when I need to say "Open this in the default application," I use xdg-open.
I think Mac OS has a similar command just called open.

Resources