riscy / shx-for-emacs

An Emacs shell-mode (and comint-mode) extension that enables displaying small plots and graphics and lets users write shell commands in Emacs Lisp.
GNU General Public License v3.0
224 stars 11 forks source link

Parsing arguments in custom functions #5

Closed CeleritasCelery closed 7 years ago

CeleritasCelery commented 7 years ago

I saw this on reddit and am super excited to try out this package! I did have one quick question though. I noticed that when writing your own shx functions it will take all the command line arguments present and put them in a big string. It is then the responsibility of the function to parse out the arguments. Why does it not pass the arguments as a list as one might expect? Is this a technical limitation?

riscy commented 7 years ago

Thank you! :)

You're correct, it'll just be one big string, and currently it's the responsibility of the function to figure out what to do.

But I think passing the arguments through as a list is the right thing to do -- it's probably time to make the change you suggest. I've got some free time this week, so keep an eye out for version 0.1.0!

CeleritasCelery commented 7 years ago

Also does the shell preform parameter expansion before the arguments are passed to the function?

riscy commented 7 years ago

Hmm, can you give me an example?

CeleritasCelery commented 7 years ago

If I have a shell variable $var which is set to file.txt could I use :edit $var to edit that file?

riscy commented 7 years ago

Oh, right. This one I'll file under technical limitation until I can figure out an easy way to extract environment variables (live) from a running shell. :\

Ideally it would be as easy as running env and parsing that output, but shx should run just as well on top of bash as on top of a python or ruby interpreter.

riscy commented 7 years ago

As a workaround, you could do something like this:

file=test.txt
echo \<edit `echo $file`\>

i.e. create some markup that looks like <edit test.txt> and echo it.

CeleritasCelery commented 7 years ago

Is it possible in comint buffers to query the underlying REPL without showing the user? You could always Send a shell echo "<original arguments>" and it would do variable expansion for you. Though I suppose you are right that this wouldn't work in interpreters that don't have echo or equivalent.

riscy commented 7 years ago

Yeah, it's definitely possible but there's some bookkeeping involved. For example typically the REPL will emit another prompt, which you have to hide from the user or things will start to get confusing.

riscy commented 7 years ago

After review I noticed many commands won't want the input string tokenized into a list. For now, I decided to leave it up to the function to parse out the arguments.

But, I'd love it if you could let me knowwhat you think of this: https://github.com/riscy/shx-for-emacs/tree/develop#example-invoking-ediff-from-the-shell -- that new example shows how I've made it much clearer how to take that raw string and parse it into a list.

CeleritasCelery commented 7 years ago

That shx-tokenize function is perfect. A simple way to grab arguments. Thanks! I tried out shx and it works great!