Inputing data in OCaml toplevel runnin under Emacs - input

When interactively running the OCaml toplevel on Emacs (via the Tuareg mode), evaluation of expression which do input from the standard input channel does not finish.
For instance, when I enter the following expression in the *ocaml-toplevel* buffer and hit enter
let x = read_int ();;
and type some integer and hit ENTER to finish, evaluation is not finished as it was expected.
The following screenshot demonstrates the situation.
What is the proper way of doing input from the standard input channel in the OCaml toplevel buffer in Emacs?

just type as usal an use
M-x comint-send-input
directly, so tuareg won't check for the terminating ;;
edit: of course, you can define a keyboard shortcut for it, if you need it regularly; e.g
(add-hook 'tuareg-interactive-mode-hook
'(lambda ()
(local-set-key (kbd "C-n") 'comint-send-input)))

Related

How to input null or EOF in kotlin using readline

I'm doing beginner level Kotlin and I'm trying to do a multi line input and I found this code:
var originalTexts: MutableList<String> = mutableListOf()
while (true) {
val originalText = readLine()
if (originalText == null) break
originalTexts.add(originalText)
}
The problem is I dont know how to input null or EOF in the input stream in console using readLine(). I tried ctrl+z or ctrl+d but is not working. Beginner here please help guys.
Edit: I'm using Intellij in Windows OS
A null return from readLine() indicates an end-of-file condition — basically, there can be no more input.
But whether and how that can happen depends on how you're running the program:
If you're running it in a terminal window, then you can signal end-of-file with a special key combination, which depends on the platform:
On macOS and Linux: Ctrl+D
On Windows: Ctrl+X then Return
If you're running it with the input redirected from a file (e.g. by appending <filename to the command), then you'll get an end-of-file condition after reading all the content of the file.
Some IDEs may provide their own combinations. For example:
IntelliJ on macOS: Cmd+D. (Ctrl+D starts the debugger instead.)
IntelliJ on Windows: Ctrl+D.
Some IDEs may provide no way at all.
Some IDEs (e.g. online) don't support user input, and so readLine() will always return null.
Due to the confusion that this can cause, Kotlin 1.6 added a readln() function, which never returns null: instead, it throws a RuntimeException if end-of-file is reached, or on platforms such as Kotlin/JS which don't support user input in this way.
(That may make it slightly easier to write beginner programs, but IMHO it's merely turning a compile-time issue into a hidden run-time one, which seems like a backward step for a language as clear and safe as Kotlin…)

How to interact with an external command in vimscript?

I have a script which interacts with user (prints some questions to stderr and gets input from stdin) and then prints some data to stdin. I want to put the output of the script to a variable in vimscript. It probably should look like this:
let a = system("./script")
The supposed behavior is that script runs, interacts with user, and after all a is assigned with its output to stdout. But instead a is assigned both with outputs to stdout and stderr, so user seed no prompts.
Could you help me fixing it?
Interactive commands are best avoided from within Vim; especially with GVIM (on Windows), a new console window pops up; you may not have a fully functional terminal, ...
Better query any needed arguments in Vimscript itself (with input(); or pass them on from a custom Vim :command), and just use the external script non-interactively, feeding it everything it needs.
What gets captured by system() (as well as :!) is controlled by the 'shellredir' option. Its usual value, >%s 2>&1 captures stdout as well as stderr. Your script needs to choose one (e.g. stdout) for its output, and the other for user interaction, and the Vimscript wrapper that invokes it must (temporarily) change the option.
:let save_shellredir = &shellredir
:set shellredir=>
:let a = system('./script') " The script should interact via stderr.
:let &shellredir = save_shellredir
Call the script within the other as,
. ./script.sh
I think this is what you meant.

elisp: generate LaTeX PDF document

I trying to completely automatize sending job applications. First step, to put the name of the company in a letter. It almost works, but it is stuck because it asks what command to use. Reading the documentation, I thought it could be disabled by a prefix argument, but I got something wrong. Also, it doesn't need to flash by visually, it could be done completely as a background process. I'll paste the code and you'll understand immediately:
(Oh, I'm using LaTeX/P mode in emacs - the goal is to not only update the .tex but also the .pdf file)
(defun edit-letter (comp-name)
(let ((path "~/work/letter/comp"))
(edit-letter-file-path comp-name (concat path "/eng/letter.tex"))
(edit-letter-file-path comp-name (concat path "/swe/brev.tex")) ))
(defun edit-letter-file-path (company-name file-path)
(find-file file-path)
(goto-line 14)
(kill-line)
(insert (format "\\textbf{To %s}\n" company-name))
(setq current-prefix-arg nil)
(call-interactively 'TeX-command-master) ; asks what command
(kill-buffer) ) ; doesn't work
(edit-letter "Digital Power Now")
It's not entirely clear from your question what you're after, but if you want to use AucTeX to call a LaTeX/PDFTex/BibTex process without getting prompted for the command name, you can use this:
(TeX-command "LaTeX" 'TeX-master-file)
Try this in place of (call-interactively 'TeX-command-master) above. When you're using LaTeX/P "LaTeX" really means pdflatex.

Common Lisp equivalent to Haskell's main function?

Haskell's main function does just what I want: evaluate when the file is loaded by itself (e.g. ./myfile.hs or runhaskell myfile.hs) and in no other case. main will not be called when the file is imported by another file. newLISP also has this functionality.
Is there equivalent code for Common Lisp?
I read the source code for CLISP. Here's what happens when the user enters clisp myfile.lisp or ./myfile.lisp:
CLISP saves myfile.lisp as p->argv_execute_file.
CLISP creates the expression (LOAD "p->argv_execute_file") and pushes it onto the Lisp stack.
CLISP saves any additional command-line arguments in a list.
CLISP stores the arguments in the Lisp variable *args*.
CLISP never makes a Lisp variable referring to p->argv_execute_file, so there is no way to discern whether myfile.lisp was loaded directly, by the user in the REPL, or by another Lisp file. If only (car *args*) were myfile.lisp, my task would be easy.
Note: Shebangs give CLISP trouble if the file is loaded from the REPL, so I put this code in ~/.clisprc.lisp:
(set-dispatch-macro-character #\# #\!
(lambda (stream character n)
(declare (ignore character n))
(read-line stream nil nil t)
nil))
I found a solution. It's a bit of shell trickery, but it works. I'll soon modify this to work on CL implementations other than CLISP.
#!/bin/sh
#|
exec clisp -q -q $0 $0 ${1+"$#"}
exit
|#
;;; Usage: ./scriptedmain.lisp
(defun main (args)
(format t "Hello World!~%")
(quit))
;;; With help from Francois-Rene Rideau
;;; http://tinyurl.com/cli-args
(let ((args
#+clisp ext:*args*
#+sbcl sb-ext:*posix-argv*
#+clozure (ccl::command-line-arguments)
#+gcl si:*command-args*
#+ecl (loop for i from 0 below (si:argc) collect (si:argv i))
#+cmu extensions:*command-line-strings*
#+allegro (sys:command-line-arguments)
#+lispworks sys:*line-arguments-list*
))
(if (member (pathname-name *load-truename*)
args
:test #'(lambda (x y) (search x y :test #'equalp)))
(main args)))
(eval-when (situation*) ...)
Update sorry for the confusing answer.
I could be wrong, but it seems to be impossible to do what you want exactly. I would make a shell script and call clisp -i myfile.lisp -x (main).
Is there any reason to not make it executable (described here)?
P.S. Common Lisp is a language and clisp is one of the implementations.

Can't rebind key C-z in xemacs

For some reason, "C-z" is mapped to suspend-or-iconify-emacs and I can't seem to get it to rebind to something less annoying. (I like using ctrl-z for undo, but doing nothing would at least be better than suspending every time I accidentally hit the key)
I've tried doing it interactively:
M-x global-set-key, then Set key C-z to command: undo.
M-x describe-key-briefly gives me C-z runs the command suspend-or-iconify-emacs
I've tried going to the scratch buffer and evaluating:
(global-set-key (kbd "C-z") 'undo) and (global-set-key "\C-z" 'undo), and it is of course in my .xemacs/init.el file.
Nothing seems to actually rebind the key.
This is happening on XEmacs 21.5, in Fundamental mode. Any ideas on how to troubleshoot this?
edit: Ok here is a hack that gets around the problem by redefining the suspend function to undo:
(defun suspend-or-iconify-emacs () (interactive) (undo))
I can't actually suspend emacs anymore, but that's actually ok with me.
Try evaluating this:
(define-key global-window-system-map [(control z)] 'undo)
(assuming that you aren't running XEmacs in tty-mode, but I guess you are not, if you want to iconify :-))
I used C-h b to find out what *-map to modify.
I have the following code in my .emacs:
(global-set-key (kbd "C-z") 'eshell)
It will start an eshell, and it works.
The C-z combination is pretty standard on Unix/Linux, if you're working in a terminal with e.g. vi, lynx or mutt and presses C-z the program will suspend and you will be back in the shell. Issuing the 'fg' command will pop the program back again. As Emacs has its own shell, I like spawning that when pressing C-z in Emacs.
You can also add the following hook, that will remap C-z in the eshell. That way pressing C-z in the eshell with get you back to the buffer you were editing.
(add-hook 'eshell-mode-hook
(lambda ()
(local-set-key (kbd "C-z") 'bury-buffer)))
Put that at the end of your .xemacs/init.el :
(global-set-key (kbd "C-z") 'undo)
Or maybe you have a misconfigured keyboard or operating system.
Do C-h k C-z to see if xemacs really receives a C-z key.
I had the same problem with C-f. I wanted it to map to isearch-forward, but instead it kept moving one character forward.
I finally solved my problem by adding
(global-unset-key [?\C-f])
(global-set-key [?\C-f] 'isearch-forward)
Apparently the issue is that C-f (and C-z) is a "real" key, that is, it's something that a terminal recognizes (it's ASCII 0x06, 0x1a for C-z), so you need the "?\" in front of "C-f".
This is the only thing I got to work.
HTH
(EDIT: I should note that I use emacs, not xemacs)