send command to emacs daemon from shell - shell

Emacs command org-html-convert-region-to-html can convert org mode to html file and I have a project which is a Java program where I need handle the org mode file and convert it to html.
I want use system call to interactive with emacs daemon, which similar as execute shell command at terminal, and I could get the response from shell output stream to my program.
Can I start a emacs daemon and send the org-html-convert-region-to-html command to it which response a output html format stream in shell?
Thanks.

Emacs can be set-up to run a server. Then you could run emacsclient commands (perhaps with the -e option, to evaluate expressions)
Emacs could also be started specially to run some Elisp commands (and could be started without opening any windows).
(however, I don't think your design is a good one. You could consider parsing org files in Java instead, and that is probably more suitable in your case)

Related

How to correctly emulate terminal in linux/macOS using exex(go)?

I need to emulate a terminal in go. I try to do it like this:
lsCmd := exec.Command("bash", "-c", "ls")
lsOut, err := lsCmd.Output()
if err != nil {
panic(err)
}
fmt.Println(string(lsOut))
And it seems to work correctly (the native ubuntu terminal displays a horizontal list, and the result of this function goes vertically).
But if I specifically call the wrong command, for example exec.Command ("bash", "-c", "lss"), I get:
panic: exit status 127
And in the native ubuntu terminal I get the following result:
Command 'lss' not found, did you mean:
and enumeration of commands.
I need to communicate with the native terminal, and get the same thing as the result of the command if I wrote the command in the standard ubuntu terminal.
What is the best way to do this? Maybe the exec library is not suitable for this? All this is necessary for front-end communication with the OS terminal. On a simple html/css/js page, the user enters a command, after go it sends it to the native terminal of the operating system and returns the result to the front-end.
How I can get the same result of executing commands as if I were working in a native terminal?
The problem
But if I specifically call the wrong command, for example exec.Command
("bash", "-c", "lss"), I get:
panic: exit status 127
And in the native ubuntu terminal I get the following result:
Command 'lss' not found, did you mean:
and enumeration of commands.
This has nothing to do with Go, and the problem is actually two-fold:
Ubuntu ships with a special package, command-not-found, which is usually preinstalled, which tries make terminal more friently for mere mortals by employing two techniques:
It tries to suggest corrections for misspellings (your case).
It tries to suggest packages to install when the user tries to execute a program which would have been be available if the user had a specific package installed.
When the command is not found, "plain" (see below) shell fails the attempt by returning a non-zero exit code.
This is absolutely expected and normal.
I mean, panicking on it is absolutely unwise.
There's a historical difference on how a shell is run on a Unix system.
When a user logs into the system (remember that back in the days the concept of the shell was invented you'd be logging in via a hardware computer terminal which was basically what your GNOME Terminal window is but in hardware, and connected over a wire),
the so-called login shell is started.
The primary idea of a logic shell is to provide interactive environment for the user.
But as you surely know, shells are also capable of executing scripts.
When a shell executes a script, it's running in a non-interactive mode.
The modes a Unix shell can work in
Now let's dig deeper into that thing about interactive vs non-interactive shells.
In the interactive mode:
The shell is usually connected to a real terminal (either hadrware or a terminal emulator; your GNOME Terminal window is a terminal emulator).
"Connected" means that the shell's standard I/O streams are connected to the terminal, so that what the shell prints is displayed by the terminal.
It enables certain bells and whistles for the user, usually providind limited means for editing what is being input (bash, for instance, engages GNU readline.
In the non-interactive mode:
The shell's standard I/O streams are connected to some files (or to "nowhere" — like /dev/null).
No bells and whistles are enabled — as there is nobody to make use of them.
GNU bash is able to run in both modes, and which mode it runs in depends
on how it was invoked.
When initializing in different modes, bash reads different initialization scripts, and this explains why the machinery provided by the command-not-found package gets engaged in the interactive mode and does not when bash is run otherwise — like in your invocation from Go.
What do do about the problem
The simplest thing to try is to run bash with the --login command-line option or otherwise make it think it runs as an interactive shell.
This might solve the problem for your case but not necessarily.
The next possible problem is that some programs do really check whether they're running at a terminal — usually these are programs which insist on real interaction with the user, usually for security purposes, and there are programs which simply cannot run when not connected to a real terminal — these are "full-screen" text UI programs such as GNU Midnight Commander, Vim, Emacs, GNU Nano and anything like this.
To solve this problem, the only solution is to run the shell in a pseudo-terminal environment, and that's what #eudore hinted at in their comment.
The github.com/creack/pty might be a package to start looking at; golang.org/x/crypto/ssh also provides some means to wrangle PTYs.

How to save a Python 2 idle/shell/command prompt interactive session on Windows?

I run calculations on Windows for hours and would like to have the calculation report/log inside the interactive IDLE/shell window be saved to a file at the end by a command.
Would be nice to exit() and close the window too.
As much as I like Linux, this is an Unattended Windows machine, hence, some modules/commands are not available, sadly, and the ability to install other software is limited.
The fact that the developers did not think of a command to save the transactions within the IDLE/shell is surprising.
I know in some environments you can direct the output of a job, like a report to another text file by using the key -o, --o, --output, > to a text file. Surprisingly Python does not support anything like that!
Any help would be appreciated
Thanks
Windows Command Prompt supports stdout redirection and probably stderr redirection. I just tested python -c "print(test)" > F:/dev/tem/out.txt and the print out went to out.txt. Replace -c "print('test') with script.py and the same should happen. Piping stdout of one program to stdin of another might work. You might be able to chain programs with a .bat file. PowerShell likely is more powerful and flexible, but I have never used it.
I am not completely clear on what you are asking, but I hope the following answers your questions.
Python runs in 2 modes: batch and interactive. Interactive mode is intended for ephermeral interaction with a human. Batch mode is for unattended computation, with occasional screen messages, but with most results sent to a file other than the screen. Both modes are combined when you run python -i xyz.py. The file is first run in batch mode, and then Python shifts to interactive mode.
It sounds like you should be using batch mode rather than either Python's or IDLE's interactive mode. If your code runs from IDLE, you should be able to run it directly with the same python.exe that you used to run IDLE. There are exceptions, of course, if one makes one's code dependent on running within IDLE, but this is unlikely to be an accident or to be needed for unattended running.
The IDLE Shell simulates interactive Python. When a file is run from an editor window, IDLE simulates python -i file-being-edited.py, with screen output going to Shell. In either case, an interactive user can save the contents of Shell with the File => Save As menu command. So there is such a command. There are also close window and exit commands and shortcuts.
In IDLE's intended use, as an interactive python learning and program development environment, there is no need from for a program to issue these commands. To save data in a file, a program can open a file and write data directly.
Try to see if you can install Jupyter Notebook (not separate software, but just a python module)
pip install jupyter
Jupyter notebook is highly helpful for saving and sharing your code. It can be used as both a shell and as a script editor.

Call vim command from shell script

I am using vimwiki as my local wiki and keep it in git in order to be able to sync it with various pcs. I am trying to automate the process of putting the generated HTML from vimwiki on my server so I can easily look stuff up.
My idea is to checkout the repository on a regular basis on the server and have shell script in place which calls vim and tells him to execute VimwikiAll2HTML, ending afterwards. I can then symlink the html folder somewhere or point nginx there or whatever.
I was able to figure out that I can directly execute a command when calling vim by using the -c parameter:
vim -c "VimwikiAll2HTML" -n index.wiki
This command automatically generates the correct HTML. However, I have to press a key and then quit vim (:q) in order to get back into the shell. It doesn't seem suited to be run inside a bash script run by cron? Can I change the command somehow in order to exit after the html generation finished? Or is there any other way I'm not aware of? I looked into the vimwiki plugin because I thought that it maybe uses an external library for HTML generation which I can call in my script but it seems that the plugin does everything by itself.
This command should work:
$ vim -c VimwikiAll2HTML -c q index.wiki

Emacs: How to use shell command prompt with emacs?

I am trying to use the shell command to facilitate commands like adding directories, etc. while in emacs. I am having problems getting it to work.
I enter M-! and then my minibuffer says Shell command: but none of my commands are accepted. For example, entering pwd or ls I get messages like pwd is not recognized as an internal or external command, operable program, or batch file.. Any suggestions?
I used this reference, but it does not seem to help with my situation:
http://www.nongnu.org/emacsdoc-fr/manuel/shell.html
Extra Info
I am using Windows 7 OS. I also have cygwin64 installed, if that matters. Is emacs just connecting to an external shell, or is the shell built into emacs? If the shell is external, can I connect to cygwin64 (although it may not be worth the trouble).
Any suggestions on how to configure this properly? I found the following resource, but I am not sure whether this is the right direction: http://www.emacswiki.org/emacs/EmacsApp
Sounds like you need to make sure that variable exec-path includes the directories where those commands (ls etc.) are located.
That error message does not appear to be an Emacs error message (from the Emacs Lisp or C source code), but rather it seems to come from the shell that tries to execute your command.

MSYS - open a file as a separate process

When I open a file in MSYS, i.e. subl init.js the terminal hangs until I quit that process. Is there a way to run it as a separate process like in Mac terminal?
The MSYS shell is a standard unix-like shell such as bash, so if you want to run programs in the background on the shell, you need to put an & at the end of the command. This causes it to run in the background.
I would recommend reading this SuperUser question about Shell Tutorials, which has some excellent links to shell tutorials and resources.

Resources