Creating a new buffer with text using EmacsClient - emacs

I have a program that can send text to any other program for further analysis (eg sed, grep, etc). I would like it to send the data to Emacs and do analysis there. How would I do that? EmacsClient takes a filename by default, this is a data string not a file and I really don't want to create and delete files just to send data to Emacs.
EmacsClient has an "eval" command-line option that let's you execute lisp code instead of open files. Is there a simple lisp function that will open a new buffer with the given text?
Edit: I'm looking for something like:
emacsclientw.exe -eval (open-new-buffer 'hello world')
And a new buffer would automatically appear with the word "hello world". I'm not sure how the name of the buffer would be set. Hopefully something auto-numbered.

This does what you ask for:
emacsclient -e '(open-buffer-with "some\nstuff\nhere")'
(defun open-buffer-with (txt)
"create a new buffer, insert txt"
(pop-to-buffer (get-buffer-create (generate-new-buffer-name "something")))
(insert txt))
Obviously you can customize open-buffer-with to do what you want.
There's a similar question you might want to look at: How do I get basic App<->Emacs integration?.

How about this approach?
emacsclient -e '
(progn
(pop-to-buffer (generate-new-buffer "Piped"))
(insert (decode-hex-string "
'$(perl -e 'print unpack "H*", qq("Hello, World!")'
)'")))
'
I've inserted newlines to break up this very long line for display purposes.
When I run this from a terminal window, a new buffer named Piped opens in my Emacs window, containing the text "Hello, World!" (complete with quotes). When I run it again, another buffer named Piped<2> opens, with the same text.
The hex-escaping (which can probably be just as easily accomplished with any other high-level language, not just Perl) is for escaping quotes that would otherwise terminate the string constant being fed to (insert).
This approach feeds text to Emacs via Emacsclient on the command line, so very long input text might give it a problem. A more general solution might be able to break up long input data and feed it to Emacs across several Emacsclient invocations.

Related

Open file from Emacs terminal without find-file

If I'm in a term-mode buffer and there is a file path displayed, how would I go about making the path "clickable", opening the file in a new buffer? It doesn't have to be mouse-clickable, in fact I'd prefer a key binding that works when the point is on the file path. Other than the common case of using ls, this function could be used when viewing a log file. Some debug info contains the file path and line number. Something like lib/library.rb:34 for example. Ideally, Emacs could open a new buffer and move the cursor to line 34.
The short answer is: don't work against Emacs. Let Emacs work for you.
While you can use find-file-at-point or put together something yourself, you will be much better off running make, grep and other stuff which prints "dir/file:pos" using M-x compile or M-x grep.
If you need to interact with your program which prints "dir/file:pos", you can pass a prefix argument to compile and the compilation buffer will be interactive.
If you have an arbitrary program whose output starts with "dir/file:pos", e.g., rails server, all you need to do is run it as (grep "rails server").

How to make emacsclient open each file in a separate 'window' within the same 'frame'

I am trying to open two files, say 'hello.txt' and 'world.txt' in emacsclient from a terminal and I want them to be opened in two different windows(as in the emacs sense of the word) but in the same frame.
I invoke emacsclient like this:
emacsclient -nw hello.txt world.txt
What presently happens is that a single emacsclient frame shows a single window where hello.txt is displayed. The other file, is opened into a buffer which is not visible.
If I instead use emacs instead of emacsclient I get the intended result(i.e two files get opened within the same frame but in two windows). How can I make emacsclient behave the same way as emacs?
I am not asking for ways to make emacsclient spawn multiple frames, rather I was asking for some way to make emacsclient to open multiple files in split-windows inside the same frame.
It doesn't seem like you can do this directly using emacsclient and a file list
You can achieve the same effect, with a bit of a kludge, by passing lisp to emacsclient to do what you want, although it gets a bit verbose
emacsclient -t -e '(progn (find-file "file1")(find-file-other-window "file2"))'
You perhaps could wrap this is a little shell script ec2files.sh, that takes two parameters and interpolates them into that lisp form.
Or write a defun that you load in emacs init that takes 2 file arguments and opens them.
(defun example-split-window-2-files (f1 f2)
(find-file f1)
(find-file-other-window f2))
and then invoke that from emacsclient -e
emacsclient -t -e '(example-split-window-2-files "file1" "file2")'
The variable server-window is what you want to look at. You can set this variable to a function that chooses which window to open.
I have the following in my emacs config:
(setq server-window 'pop-to-buffer)
That ensures that when you open a (single) file it uses another window (creating it if necessary) to display the file. You will need to either find or write a function to pass to server-window that will keep creating new windows for you to display files in.
Section 37.1, "Invoking `emacsclient'", of the emacs 24.3.1 manual says:
You can also force emacsclient' to open a new frame on a graphical
display, or on a text terminal, using the-c' and `-t' options.

Run a program from Emacs and don't wait for output

How to make Emacs run a program and don't wait for output/respond? I tried to open a pdf in an external program:
(shell-command (concat "start sumatrapdf " (shell-quote-argument path) " -page " search))))
But it won't open another files until the existing sumatrapdf process is closed. I tried async-shell-command, but it opens a new buffer with Async output which I don't need.
What is the right way to open files in external programs?
start-process function can handle that:
(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)
Start a program in a subprocess. Return the process object for it.
NAME is name for process. It is modified if necessary to make it unique.
BUFFER is the buffer (or buffer name) to associate with the process.
Process output (both standard output and standard error streams) goes
at end of BUFFER, unless you specify an output stream or filter
function to handle the output. BUFFER may also be nil, meaning that
this process is not associated with any buffer.
PROGRAM is the program file name. It is searched for in `exec-path'
(which see). If nil, just associate a pty with the buffer. Remaining
arguments are strings to give program as arguments.
If you want to separate standard output from standard error, invoke
the command through a shell and redirect one of them using the shell
syntax.
If you don't want to associate bufer with open process — pass nil as BUFFER argument
See C-h k M-!
...
If COMMAND ends in ampersand, execute it asynchronously. The output
appears in the buffer `Async Shell Command'. That buffer is in shell
mode.
...
IOW, M-! my_command --opt=foo arg1 arg2 & will start my_command and create a *Async Shell Command* buffer with my_command running in it but emacs will give control back to you right away.

On Emacs, getting the output of a command sent to a buffer via Emacs-Lisp

I would like to write a small script in ELisp that would:
send a command to a given buffer
get its output
parse it
send it to another buffer
I am struggling with point 2: I cant get the output of a command. For example, if I have a shell buffer on, I can use
(process-send-string "shell" "help\n")
to send "help" to my shell buffer. It will then show the list of the commands available. But how can I get this list to use it somewhere else?
Thanks,
S4m
(buffer-string) returns the contents of the current buffer, so (with-current-buffer <buf> (buffer-string)) will return the contents of <buf>.
I don't know the exact emacs commands for this off the top of my head, but one option would be to do the following:
Set the mark in the shell buffer right below the command line
Execute the command.
Move the point to the end of the file and kill the text between there and the mark.
Move to the destination buffer and yank the text into there.
Have you considered using the shell-command or shell-command-to-string functions?
The don't "send a command to a buffer" like you asked, but they do both allow running a command through a process that will be started just for that purpose and either dumping the output into a target buffer or collecting it into a string.

How can I script vim to run perltidy on a buffer?

At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
After trying #hobbs answer I noticed that when filtering the entire buffer through perltidy the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy completed.
So building on #hobbs' and #Ignacio's answers, I added the following to my .vimrc:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(closing " added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy() will return the cursor to its original position plus or minus at most X bytes, where X is the number of bytes added/removed by perltidy relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim version: 7.2]
EDIT: Updated DoTidy() to incorporate #mikew's comment for readability and for compatibility with Vim 7.0
My tidy command:
command -range=% -nargs=* Tidy <line1>,<line2>!
\perltidy (your default options go here) <args>
If you use a visual selection or provide a range then it will tidy the selected range, otherwise it will use the whole file. You can put a set of default options (if you have any) at the point where I wrote (your default options go here), but any arguments that you provide to :Tidy will be appended to the perltidy commandline, overriding your defaults. (If you use a .perltidyrc you might not have default args -- that's fine -- but then again you might want to have a default like --profile=vim that sets up defaults only for when you're working in vim. Whatever works.)
The command to filter the entire buffer through an external program is:
:%!command
Put the following in ~/.vimrc to bind it to Ctrl-F6 in normal mode:
:nmap <C-F6> :%!command<CR>
For added fun:
:au Filetype perl nmap <C-F6> :%!command<CR>
This will only map the filter if editing a Perl file.
Taking hobbs' answer a step further, you can map that command to a shortcut key:
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
noremap <C-F6> :Tidy<CR>
And another step further: Only map the command when you're in a Perl buffer (since you probably wouldn't want to run perltidy on any other language):
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm command! -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm noremap <C-F6> :Tidy<CR>
Now you can press Ctrl-F6 without an active selection to format the whole file, or with an active selection to format just that section.
Instead of creating a new keyboard shortcut, how about replacing the meaning of the = command which is already in people's finger memory for indenting stuff? Yes, perlcritic does more than just indent but when you use perlcritic anyways, then you probably don't want to go back to the inferior "just indent" = command. So lets overwrite it!
filetype plugin indent on
autocmd FileType perl setlocal equalprg=perltidy
And now we can use = just like before but with the added functionality of perlcritic that goes beyond just indenting lines:
== run perlcritic on the current line
5== run perlcritic on five lines
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
gg=G run perlcritic on the entire buffer
And the best part is, that you don't have to learn any new shortcuts but can continue using the ones you already used with more power. :)
I'm used to select text using line oriented visual Shift+V and then I press : an I have !perltidy -pbp -et4 somewhere in history so I hit once or more up arrow ⇧.