Writing A First Emacs Elisp Function
So today I tried to write my first fully custom Emacs Lisp
code. Here are some things I learned (along with the code
Here is my final product, uploaded straight from Emacs.
The final script looks like this at the bottom of my
;;; Region to Gist(defun region-to-gist ()"Sends region to Gist"(interactive)(if (region-active-p)(gist-req (buffer-substring-no-properties (region-beginning) (region-end))))nil)(defun gist-test (buf)(message "%S" `(:content ,buf)))(defun gist-req (buf)(request"https://api.github.com/gists":type "POST":data (json-encode `(:description "Created with Christopher Biscardi's region-to-gist":public t:files (:example.el (:content ,buf))));; :data "key=value&key2=value2" ; this is equivalent:parser 'json-read:success (function*(lambda (&key data &allow-other-keys)(message "I sent: %S" (assoc-default 'html_url data)))))nil)
It is used by setting a mark
C-SPC, selecting a region
(arrow keys work to expand the region) then typing
M-x region-to-gist. The Function then runs and outputs
either an error message (hopefully not) or this to the
I sent: "https://gist.github.com/8812412"
The most important part here is the
allows you to use
M-x region-to-gist to execute the
function. There are a couple different modes you can enable
region-active-p basically returns true if there’s a region
C-SPC and arrows stuff).
gist-test is a function I used to replace the
call so I could test whether or not the region was actually
being sent and resolve some formatting issues. This function
can be wholly deleted with no ramifications.
buffer-substring-no-properties returns undecorated strings
from your buffers. Use this is you’re trying to handle a
string (such as in JSON) and use
you’re trying to put something in the kill-ring, etc.
I’m using request.el
to make my request. It tries to use
curl if available.
The biggest part of the request code is the
function. In this case we grab the key
html_url from the
Gist API Response
The backtick here:
`(:description "Created with Christopher Biscardi'sregion-to-gist" :public t :files (:example.el (:content ,buf)))
means we have to use the
,buf to get the value of
Oh, and also, this function should probably be called
gist-region as per other region functions. oops.