Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts

Tuesday, July 7, 2009

Basic Functions in (emacs) lisp

In this post, I summarise a few functions in emacs lisp and give a few example programs. Do try them out in your emacs editor and leave your comments if you are not able to understand something.

Function Name Syntax Input form Output form
quote or '
(' is a short form of quote)
(quote arg1) or 'arg1 arg1 is any symbol return the symbol arg1
without modification
+ (+ a1 a2 ... an) 'n' numbers a1,a2 ... an the sum of a1,a2, ... ,an
=> (a1 + a2 + .. +an)
* (* a1 a2 ... an) 'n' numbers a1,a2 ... an product of a1 a2 a3 ...an
=> (a1 * a2 * ... *an)
- (- a1 a2 ... an) 'n' numbers a1,a2 ... an subtract from a1,the sum
of a2,a3,... an
=>(a1 - (a2 + a3 + ... an))
/ (/ a1 a2 ... an) 'n' numbers a1,a2 ... an Divide a1 by the product,
of a2,a3 ... an
=>(((a1/a2)/a3).../an) or
=>(a1/(a2*a3*...an))
% (% a1 a2) 2 numbers a1,a2 Return the remainder when a1 is divided by a2
=>(a1 % a2)

Now onto some example programs that use the functions described above. Do evaluate them and see for yourself.
Code :
  1. (quote 5)
  2. (quote (+ 1 2))
  3. (+ 1 2)
  4. (+ 1 (+ 2 3))
  5. (- 100 (+ 20 5))
  6. (% (* 10 5) (+ 5))
  7. (* (-) (+))
  8. (quote (+ 1 2 (+ 3 4)))

When you evaluate the above programs in emacs you will get the following results :
  1. (quote 5) => 5
  2. (quote (+ 1 2)) => (+ 1 2)
  3. (+ 1 (+ 2 3)) => (+ 1 5) => 6
and so on. You may try the other programs and also think of some programs of your own and evaluate them.

Wednesday, June 10, 2009

Emacs usage (for survival)

When I first opened emacs, I remember being perplexed and was wondering how to go about editing files the way I used to do with MS-Word. No doubt, emacs is very different and if you are a newbie, emacs can be a little intimidating but I assure you, once that minor hurdle has been crossed it is immensely rewarding. Emacs has a rich collection of commands that can be invoked by pressing a combination of keys on the keyboard. In order to make life easy there are a few abbreviations given to the sequences of key commands.
C-<chr> Hold the control key down while typing the character represented by chr. For example, C-f would be: hold the CONTROL key and type f
M-<chr> Hold the Alt key down while pressing the character represented by chr given in brackets
C-<chr1> <chr2> Hold the control key down while pressing chr1, then release both keys and then press chr2 and so on so forth if you get the drift.
In case you are having trouble in reading and understanding the above abbreviations I recommend that you check the following video tutorials : Update: Do check out this link too for some very good content on emacs.
/*some basic commands that serve as gateways to more interesting stuff*/
C-h tThe help tutorial
C-h i Takes you to the info manual in emacs provided it is installed
/* some essential commands*/
C-s Search the current file
C-x C-f Used to find or create a new file
C-gTo cancel some command while midway. For e.g. type C-h and then cancel it with C-g
C-x C-c A neat way to quit emacs
/* A few commands for moving around the text file */
C-f Move forward one character (left arrow)
C-b Move backward one charactber (right arrow)
C-n Move down one line (down arrow)
C-p Move up one line (up arrow)
Similarly C-a, C-e, M-a, M-e, M-b, M-n, M-p are more commands related to moving around the file you are currently editing.Now, you might wonder why there are so many commands related to movement. Well, in case you have not already discovered, I suggest that you find the difference between M-a, M-e, C-a and C-e. Besides the variation in functionality, it is also good to practice moving around using the above commands as it helps you to get used to using the key combination sequences which will later help you do some advanced stuff with emacs.

Tuesday, June 9, 2009

Emacs

Emacs is an excellent text editor, in fact it is the best editor in my opinion. It is available across all major platforms, be it Apple's Mac, or Windows and of course Linux. It is built and maintained by the Free Software Foundation. One of the best features of this software is that it behaves almost the same across all platforms. An amazing thing in and of itself even if you do not consider the level of functionality provided. No other editor comes even close to matching this level of cross-platform compatibility or level of sophistication. The primary reason for the superiority of emacs over other editors and the reason why other editors have not been able to match it is because of the language emacs has been implemented in, emacs lisp which is a variant of lisp. Lisp is a language that has plenty of variants and emacs lisp is one among them. Over the next few days I intend to mull over the emacs text editor and then move over to learning and posting about the emacs lisp language. Emacs, unlike other editors demands that its users behave in a smart manner and hence it can be quite challenging for a newbie to get started. A few useful links that will help you in this regard, In order to learn emacs lisp, there are two excellent tutorials offered by gnu. One tutorial is for beginners and by beginners I mean people who do not know how to program in lisp. You may be an expert in some "x" language but lisp being fundamentally different, it is recommended that you learn it from scratch. On the other hand, if you are a wannabe programmer but do not know any language, then take this tutorial and it might help you on your way to becoming a good if not great programmer. The other tutorial is a reference manual that contains major parts of the source code of emacs as well as a tutorial for emacs lisp. Both tutorials are pretty amazing. The emacs lisp tutorials Enjoy !!! :-)