Sat Jun 18 01:08:50 CEST 2005

elisp function


Ok, cool. I googled around a bit, and found this function called
read-from-minibuffer, which enables you to give strings to a function
under runtime. Perfect! Now all I had to do was to learn how to write
a function properly.

The first programming language I learned at the Uni was Scheme, which
is a dialect of Lisp. I really liked scheme. The whole idea of
functional programming appeals to me- no global variables, the intense
use of recursion, the lambda calculus- this is all very nice stuff!
Incidentally, I found my first textbook in computer programming on the
web- its the entire book! (Link: Structure and Interpretation
of Computer Programs, also known as SICP)

The fact that I like functional programming, together with the fact
that I have, over the years, fiddled with my .emacs file (mostly
simple edits of what others had done), made me confident to find a
solution to my simple evening project.

It really was simple. But complex programs are made up of less complex
components eh? Below the function:

(defun blog_add_html_code_hostname()   "Given a hostname, this function will insert   the string '<code>hostname.yanson.org</code>'   at the cursor point."   (interactive)   (setq string       (read-from-minibuffer               "Enter hostname: "               nil               nil               nil               nil ) )   (insert       "<code>"       hostname_string       ".yanson.org</code>" ) )
Its quite self-explanatory, but heres a little commentary anyway:
(defun blog_add_html_code_hostname() Defines a new function with the somewhat unwieldy name blog_add_html_code_hostname. This function needs no parameters, so I left the parens after the name empty.
"Given a hostname, this function will insert the string '<code>hostname.yanson.org</code>' at the cursor point." The first element after the function name is always supposed to be a little documentation. Though it is possible to write nil here (for a null element), documentation is its own reward, isn't it? Oh and enclose the documentation in double quotes so that it is evaluated as a single entity, namely a string.
(interactive) The element(s) after the documentation is the meat of the function. Here we start off with the interactive instruction, which simply means that this function is possible to call directly from the mini-buffer by running M-x function_name. This instruction can take arguments, for example that it can take input from a prefix argument (read more here).
(setq hostname_string      (read-from-minibuffer           "Enter hostname: "           nil           nil           nil           nil)) Remember how I originally wanted to be able to type in a hostname to the function? These two lines take care of this. Starting with the innermost parenthesis block, we have the command read-from-minibuffer which does just that. For details on the function arguments, start up your emacs and type C-h f, followed by the name of the function. Anyway, this innermost paren block returns a string which we enter in the minibuffer. The setq command sets the variable (1st arg) with the info in the 2nd. So now we have a set variable, namely the variable hostname_string.
(insert      "<code>"      hostname_string      ".yanson.org</code>" ) This last line takes the contents of the variable hostname_string, and prefixes it with <code> and suffixes it with .yanson.org</code>. It then prints it in the current buffer, where the cursor is located.
Yep, thats that. That was Fun! --