2

I'm customizing spacemacs for my workflow. I'm new to elisp and can't figure out how to do this after looking at other functions in .el files and other posts.

Basically, I wrote an elisp function that executes a shell script on the currently open file(the shell script compiles a special kind of LaTeX file that includes something called Lilypond code which is used for music typesetting). The code for the function I wrote looks like this:

(defun lytex-to-pdf () (interactive) (shell-command-to-string (format "bash ~/lytex-to-pdf.sh %s" buffer-file-name))) (evil-leader/set-key "cb" 'lytex-to-pdf) )

It works fine, but I'd like it to pop up a buffer with a terminal showing the std-out of the shell script. That way I can debug in case it fails to compile. How can I do this? Is shell-command-to-string the right emacs command I should be executing?

As an aside, how do I change my custom keybinding to only show up in the LaTeX major mode?

5
  • I don't have the ability to test this function and I don't use evil/spacemacs, but you may wish to consider using start-process with a set-process-sentinel and use buffer-string or buffer-substring to gather some/everything when the process ends successfully. start-process has an optional argument to generate a buffer, and you can use display-buffer or other function to pop it up wherever/however you see fit. For long command lines, you may wish to consider using something like (start-process "my-process" "*OUTPUT-BUFFER*" "/bin/bash" "-c" "LONG-COMMAND-LINE-WITH-ARGS").
    – lawlist
    Jul 15, 2018 at 22:52
  • See also filter functions to help gather only the output desired from an ongoing process: gnu.org/software/emacs/manual/html_node/elisp/…
    – lawlist
    Jul 15, 2018 at 23:57
  • start-process did exactly what I needed, thanks! Please add as an answer so I can mark this question as answered. Also, figured out the major-mode key binding.
    – HelmetFace
    Jul 16, 2018 at 1:26
  • What major mode are you using for latex? Jul 16, 2018 at 3:10
  • I'm using latex-mode, i.e. the one that labels your buffers as "LaTeX/MPS". My next step is to actually integrate my function better with this mode, and get spacemacs to recognize .lytex files with the latex/build command bound to SPC-m-b and to do the same with the TeX-view command bound to SPC-m-v(right now it throws an error because it's looking for "<file>.lytex.pdf" instead of just "<file>.pdf". Once I figure that out, I'll have things all set up how I want to for type-setting documents with musical notation! If I can't figure this out in a couple days, I'll post another question.
    – HelmetFace
    Jul 16, 2018 at 3:28

1 Answer 1

3

start-process has an optional argument to generate an output buffer. A set-process-sentinel may be used in conjunction with start-process to monitor the termination of a process, so that certain action may be taken thereafter. One can use display-buffer or another function of that family to display the output buffer as the user see fit. For long command lines that may not necessarily be ideally suited for individual arguments to be passed along to start-process, a user may wish to consider using something like (start-process "my-process" "*OUTPUT-BUFFER*" "/bin/bash" "-c" "LONG-COMMAND-LINE-WITH-ARGS"). See also filter functions that can be used to gather only the output desired from an ongoing process: https://www.gnu.org/software/emacs/manual/html_node/elisp/Filter-Functions.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.