Ok, noob question maybe but I am having trouble finding information about this stuff. I want to write a function that takes whatever is selected in a region and passes it to curl. Curl returns something and I want that something to replace the selected region. In other words, after selecting http://www.foo.com in a region and running the function, the it would be replaced with whatever curl would return if ran on the command line with curl www.foo.com.
Here is my attempt which is very very wrong. Basically, I can’t figure out how to pass that selected region as a variable to shell-command-on-region:
(defun curl-something()
(interactive)
(setq region ((region-beginning)(region-end)))
(shell-command-on-region (region-beginning) (region-end) (shell-command (concat "curl" region)) nil t))
Also, if anyone has any pointers on where to learn basic elisp text manipulation programming, please share them (not reference manuals please). Thanks!
Edit:
Thanks to Randy Morris, I was able to figure out the answer:
(defun curl-something (begin end)
(interactive "r")
(shell-command-on-region begin end (concat "curl -s " (buffer-substring begin end)) nil t))
You need to pass the “r” flag to
(interactive). Your function will then be passed the start and end of the region as numeric arguments which you can use to get the text in the region.Here is an example taken from ErgoEmacs:
The following function will grab the url and pass it to curl, then dump the contents of that website in a new buffer:
Also worth noting are the native
url-functions for this sort of thing, but I realize that this is beyond the scope of this question.