How to use (read) correctly in mit-scheme? - scheme

I read in the documentation and rosetta code that (read) is used to get input from the console. So I wrote this code to check this:
(display (+ (read) 1))
But mit-scheme never asks for user input and the program just terminates. Why is this the case?

In the REPL, (display (+ (read) 1)) works as expected.
When (display (+ (read) 1)) is placed in a source file, and the file is run as a script using mit-scheme --quiet < program.scm (reference), mit-scheme never asks for user input and the program just terminates. Why?
To see the reason, place this in the source file instead:
(define n (read))
2
(display (+ n 1))
You get 3, as expected.
This is all caused by the shell input redirection (i.e. <). read gets its input from the current input port by default. With shell input redirection, the current input port is the source file. Hence, (read) does not prompt for user input because stdin is the source file.
To the best of my knowledge, there is currently no easy way to correctly run an MIT Scheme script directly from the command line (surprise! surprise! MIT Scheme is antiquated). Relevant mailing list discussion: [MIT-Scheme-devel] How to run a script and exit?.

Related

How emacs take present file as argument to a function automatically?

I am making my own shell command. What is the command that automatically takes the present file as argument of a function when i press enter (just like % in .vimrc file. basically i want that interactive command should invoke automatically when i don't pass any argument as filename while calling the function and automatically takes the present file as the input to the command.
From how I understood your question, you may be looking for the variant of interactive with a lisp form that produces the arguments:
(defun echo-current-buffer-filename (f)
(interactive (list (buffer-file-name)))
(message "Current buffer filename is %s" f))
See the documentation of the interactive function at https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Interactive.html for more information.

How to make executable File using Gambit

I wrote this Scheme source file on notepad. I have gambit scheme installed.
(define hello-world
(lambda ()
(begin
(write ‘Hello-World)
(newline)
(hello-world))))
I use windows command line. i type in 'gsc hello.scm' in the command line. It spits out a file on my desktop called "hello.o2". I want to see "Hello-World" pop up on my command line. For example, when I compile stuff in c++ it gives me a file called a.exe and I am able to observe it on the command line.
how can I do this with the gambit compiler for scheme?
You can create an executable by adding the -exe compiler switch:
gsc -exe hello.scm
will produce hello.exe . Alternatively you can produce the .o1 (or .o2, etc) file and execute it with:
gsc hello.scm
gsi hello
If you want an executable that will run on its own, you need to do a couple of things to make it work correctly.
#;gsi-script %~f0 %*
;
(define hello-world
(lambda ()
(begin (write `Hello-World) (newline) (hello-world))))
(define (main)
(hello-world))
That first line is for DOS/Windows only. The Unix version of that top line is
;#!/usr/local/bin/gsi-script -:d0
Those lines tell the compiler how to execute the code once its compiled.
Also, you need a main procedure. If you're not passing any parameters, then you can use the form I gave you. If you need to pass parameters, you will need to write the main procedure appropriately, noting that all parameters are passed as strings and may need to be parsed or converted before use.

Sending bash commands to an open terminal buffer in emacs

I've been trying to improve my emacs life lately, and one thing I've done is make use of projectile and perspective to organize my buffers sensibly.
As part of this I wrote an elisp function to open up (or return to) a named ansi-term buffer that is project-specific. This allows me to quickly drop into a bash terminal for the project I'm currently looking at.
What I have been having trouble finding out after plumbing the interwebs is whether or not it is possible to send bash commands to an open ansi-term buffer from within emacs. Specifically, I'm trying to make sure the ansi-term buffer cds to the correct project root directory when it's first opened. This requires grabbing context from the projectile package first, so it's not something I can plop into my .bashrc.
Ideally I would be able to write an elisp function that:
1) selects an ansi-term buffer by name (since I can have one open with a unique name for every project)
2) sends and executes a command in that buffer
Is there any way to do this?
EDIT
Final solution for anyone who is interested:
(defun visit-project-term-buffer ()
"Create or visit a terminal buffer."
(interactive)
(if (not (get-buffer (persp-ansi-buffer-name)))
(progn
(split-window-sensibly (selected-window))
(other-window 1)
(ansi-term (getenv "SHELL"))
(rename-buffer (persp-ansi-buffer-name))
(end-of-buffer)
(insert (format "cd %s" (projectile-project-root)))
(term-send-input))
(switch-to-buffer-other-window (persp-ansi-buffer-name))))
Does this work for you? It switches to a buffer named *terminal* and runs echo hello:
(defun my-echo ()
(interactive)
(switch-to-buffer "*terminal*")
(end-of-buffer)
(insert "echo hello")
(term-send-input))

Emacs how to run command in Interactive command line mode in elisp

I am newbie to Emacs.
I want to define a function in elisp to run a command in interactive command line mode (Asynchronously if possible).
my current code is:
(defun ma () ;run maxima batch on the current file
(interactive)
(let*
((fn (buffer-file-name)) (cmd (concat "maxima -b " fn)))
(message "cmd:%s" cmd)
(shell-command cmd)
)
)
this works fine when I do not have break points in the maxima code. When I have break points "break()", I have to interact with the program. The current shell-command function does not work.
I also like the mechanism of "shell-command" function that the screen will automatically split into two and show the programming running info in a second window. If possible, I still want this feature in the code that you can help me with.
Any help would be appreciated.
I want to define a function in elisp to run a command in interactive
command line mode (Asynchronously if possible).
Maybe async-shell-command is what you are looking for do C-h f async-shell-command RET for help on the function.
Use the built in compile function in commint mode.
(defun ma (&optional filename)
(interactive)
(compile (format "maxima -b %s" (or filename (buffer-file-name))) t))
This will open up a new window and will show you the output of the program running. Commint mode means that the compilation process is interactive, you will be able to send input to the program from the compilation buffer.

How to trigger or instrument with edebug programmatically?

C-u C-M-x evaluates a defun form with edebug instrumented. Can I do that programmatically? I want to do that because I want to write an elisp file of the following form:
;;; define a function with edebug instrumented.
...
;;; do something that invokes the function with particular arguments.
...
then I can run emacs -q --load on that elisp file, step through code, get an idea on further investigation on the bug, edit the elisp file in my original emacs session, run emacs -q --load on it again, and repeat.
In ~/test.el:
(defun square (x)
(* x x))
In ~/testtest.el:
(with-current-buffer (find-file-noselect "~/test.el")
(re-search-forward "square")
(edebug-defun))
(square 5)
In bash:
emacs -q -l ~/testtest.el

Resources