Gabriele
Gabriele

Reputation: 111

Calling a custom emacs function with arguments

I've got this function in my .bash_rc:

function ForwardSearchXdvi {
latex -src *.tex;
for i in *.dvi; do xdvi -sourceposition "$1 ${i/.dvi/.tex}" $i; done ;
}

it works... I call it by command line with the $1 argument (the target line number in my file.tex) and it's fine.

I'd like to run it directly from emacs so I made this command:

(defun ForwardXdviSearch ()
(interactive)
(shell-command (format "bash -ic %s" (shell-quote-argument "latex -src J[HCI]*.tex; for i in J[HCI]*.dvi; do xdvi -sourceposition \"$1 ${i/.dvi/.tex}\" $i; done ;")))
)

How can I pass the argument $1 to the function when I call it with "M-x Function" ?

Upvotes: 0

Views: 399

Answers (1)

juanleon
juanleon

Reputation: 9380

You would need to use special form interactive for reading arguments. Something like this untested code:

(defun forward-xdvi-search (line-number)
  (interactive "nForward to line: ")
  (shell-command
   (format "bash -ic %s"
           (shell-quote-argument
            (format "latex -src J[HCI]\*.tex; for i in J[HCI]\*.dvi; do xdvi -sourceposition \"%d ${i/.dvi/.tex}\" $i; done ;"
                    line-number)))))

Edited with the improvement suggested by @phils

Upvotes: 2

Related Questions