An article about the skillful use of Emacs by my colleague, who wants to become a citizen, and observe a UFO. Please invite to d.klykvin@gmail.komOnce I discovered the game Godville, where the hero lives on his own,
and it is necessary to podpinyvat when most will not be lazy. But, after the nth
the number of weeks I'm sick of opening the browser to view
my hero's state of health, and I decided to write an extension for emacs,
which will crawl into the network itself and display me
information.
Surely this article will be uninteresting to gurus emacs, but for the first steps
It can be useful
We have:
- GNU Emacs
- xml from server side
- basic elisp knowledge
- google
- sources of other modules
Gutted innards
')
First you need to see what tools we already have
there is. First, we will need to pick up the file from the server, and second,
parsit it
Quick search by / usr / share / emacs / (find out where you are from
emacs loads files by using Mx v load-path) issued that
links are handled by a certain url, and parsing is xml, so we do
Mx load-library url
Mx load-library xml
Now you can create a buffer where our code will be stored (I
put it in ~ / .emacs.d / godville.el)
Looking for functions
I usually look for functions in three ways - Ch a (apropos) scrolling through
source code or by doing Ch f function-name.
in this case, the search by code turned out to be better, because the command "grep
defun url.el "gave me all the functions that are in this file
(I remind you that the url package is not limited to one file - just me
He needed it)
The great advantage of emacs is that it is a lisp machine, and in any
the place of the editor can execute a piece of code. In grep slipped
information about the url-retrieve - immediately and see.
Right at the beginning of the file we write
(url-retrieve "http://ya.ru")
and evaluate the expression with Cx Ce. Ooops:
wrong-number-of-arguments (url callback & optional cbargs)
Judging by Ch f, url-retrieve also needs a callback. Because just
pop-to-buffer can not write, and another function is lazy to fence,
stick a lambda expression
(url-retrieve "http://ya.ru" (lambda (x) (pop-to-buffer (current-buffer))))
Congratulations - most likely you have now opened the source code ya.ru, and
half done. If not opened - see errors and
ask what's the matter with neighbors Lispovodov
We make out the code
Emacs needs to know what it will need for our library to work and
which, in general, our library provides, so the first 3 lines
will be commonplace:
(require 'url)
(require 'xml)
(provide 'godville)
Now that someone does load-library godville, emacs first
make sure that the url and xml is available.
The next step is to create a function that will pick up the XML with
server. url looks like
http://godville.net/gods/api/username.xml ,
so we need to add a variable with the name of god. During development
it's easier to do this with setq:
(setq godville-username "Godville")
And, actually, the function itself:
(defun godville-get-info ()
"Connect to server, get XML and create list with data"
(interactive)
;; Get XML with hero data
(if (not godville-username)
(setq godville-username (read-from-minibuffer "Enter user name:")))
(godville-parse-xml (url-retrieve
(concat "http://godville.net/gods/api/" godville-username ".xml")
(lambda (x) ()
)))
)
Actually, the creation of a function occurs as in most other
languages:
(defun function-name (params)
;; function body
)
The only thing to get used to is the fact that it is not necessary
get a lot of intermediate variables - the result of a single function
passed to the higher one, the higher one, etc. And one more thing - always
leave a comment on the function.
always It will save time as
you and those who will look at the code in the future.
In our case (if) first checks for the presence of a variable.
godville-username, and if that is not set, asks to enter it into
minibuffer. After the name is entered, the list is calculated.
(godville-parse-xml (url-retrieve ...)) Since the callback from
url-retrieve us to nothing (more precisely, I hung there processing 404, but
now it will be superfluous) we insert an empty lambda expression, and
the result of the url-retrieve is passed to godville-parse-xml
url-retrieve returns a new buffer with the data that it
received. Therefore godville-parse-xml will have at least one
parameter is the name of the buffer with our XML:
(defun godville-parse-xml (buffer)
...
)
Going grep on xml.el found xml-parse-file and xml-parse-region. For
xml-parse-file we need to get a buffer, save it,
parsing, deleting the file ... not suitable. Will use
xml-parse-region. Now our function will look something like this:
(defun godville-parse-xml (buffer)
"Parse xml and kill temp buffer"
(switch-to-buffer buffer)
(setq tmp-list (xml-parse-region (point-min) (point-max)))
(kill-buffer buffer))
Using Ch f and reading the documentation you will soon realize that this feature
as simple as 2 kopecks: switched to the buffer, parsed xml, stuffed
the result into a variable and have now nailed an unnecessary buffer.
In principle, this is where the work of the url and xml libraries ends - the file we
Got, made a normal list of xml-file, the rest is business
equipment - we go through the list, beautifully draw the necessary data,
throwing out unnecessary, and lay out the code to the public.
Customization
Omitting the tedious code of this particular task, you cannot miss the opportunity.
settings. You all must have used Mx customize or Mx.
customize-group, so we can do no worse.
The functions defgroup, defcustom, defface, etc. are used for this.
(defgroup godville nil "Godville status viewer"
: group 'applications)
(do not forget to do Ch f for each familiar and unfamiliar function - mastering emacs lisp will go much faster. In this case, you can
see where nil came from in defgroup)
(defcustom godville-user nil
"God name"
: type 'string
: group 'godville)
(defcustom godville-timer 1800
"Update interval"
: type 'integer
: group 'godville)
The final
Actually, like this, by issuing a "cap" with require / provide and appending the code
with small steps you can turn your editor into a small and cozy
an operating system from which it is not necessary
go out. The main thing is not to be afraid to do Cx Ce, read errors and
correct them.
An article about the skillful use of Emacs by my colleague, who wants to become a citizen, and observe a UFO. Please invite to d.klykvin@gmail.kom