Difference between revisions of "Elisp notes"

From Notes_Wiki
(Created page with "=Elisp notes= ==Executing elisp code== * We can use *:<pre> *:: lisp-interaction-mode *:</pre> *:command to change mode in which current buffer is evaluated from Fundame...")
 
m
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Elisp notes=
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Text editors]] > [[Emacs]] > [[Elisp notes]]
 


==Executing elisp code==
==Executing elisp code==
Line 19: Line 18:


==Basic elisp types are==
==Basic elisp types are==
 
<yambe:breadcrumb self="Elisp notes">Emacs|Emacs</yambe:breadcrumb>
{| class="wikitable" border="1" cellspacing=5" cellpadding="10"
{| class="wikitable" border="1" cellspacing=5" cellpadding="10"
|-
|-
Line 169: Line 168:


More information can be learned from http://ergoemacs.org/emacs/elisp.html
More information can be learned from http://ergoemacs.org/emacs/elisp.html
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Text editors]] > [[Emacs]] > [[Elisp notes]]

Latest revision as of 15:53, 24 August 2022

Home > CentOS > CentOS 6.x > Text editors > Emacs > Elisp notes

Executing elisp code

  • We can use
    lisp-interaction-mode
    command to change mode in which current buffer is evaluated from Fundamental or other to Lisp Interaction.


  • We can evaluate expression by placing cursor at end of expression to be evaluated and using C-j key
 5

evaluates to

 5


Basic elisp types are

<yambe:breadcrumb self="Elisp notes">Emacs|Emacs</yambe:breadcrumb>

Value Type
5 Number
5.6 Floats
"Abc" String
'sym1 Symbol
'(1 2 3) List
(list 1 2 3) List
t true
nil false
nil empty list


Functions

Functions can be defined using defun

(defun factorial (n) "Factorial function"
  (if (= n 0) 1 (* n (factorial (- n 1)))))

We can call user-defined functions just like in-built functions in elisp code.

(factorial 5)

Note for this call to work one needs to compile factorial function definition using Ctrl+j short-cut first.

If function requires more than one arguments then arguments should be separated by space

(defun add2(x y)  "Adds two numbers" (+ x y))


Constants

We can use defconst to define constants

(defconst const1 3)


Using let

let works like in scheme but has dynamic scope and not lexical scope

(let ((x 3))  (+ x 3))

would return 6


Using car and cdr

car and cdr work like first and rest of scheme

(car '(1 2 3))         ;;Returns 1
(cdr '(1 2 3))        ;; Returns list '(2 3)
(cdr nil)             ;;Could have been error, but is nil :(


Printing values

print can be used to print values

(print   "2 + 2 is 4")

Note this would print "2 + 2 is 4" twice. Once due to print statement and other as execution of elisp code prints return value in buffer.

To print just once one can use message function:

(message "hi")

Message can also be used to print variable values or constants:

(message "My age is %d" 25)

Here, %d is used to indicate integer. One can also use "%S" which can print generic values including lists.


Modifying values of variables

setq works like set! of scheme

(setq p1 3)
(setq p1 (+ 2 p1))


Using if

Basic syntax of if is

(if <condition> <statement-for-true> <statement-for-false>)

For example,

(if (< a 5) (message "a(%d) is less than 5" a) (message "a(%d) is greater than or equal to 5" a))


Interactive functions

When functions are declared as interactive then they can be executed as commands using M-x. For example:

(defun hello() "Inserts hello world at cursor position" 
  (interactive)
  (insert "Hello world"))

The above function can be compiled using C-j and then executed using "M-x hello <RET>"

Interactive functions can take one argument from user using:

(defun call-factorial (num1) "Calls factorial function"
       (interactive "nEnter number for factorial : ")
       (message "Factorial of %d is %d" num1 (factorial num1)))

Arguments can also be taken from univeral argument list built with "C-u <argument>" using:

(defun factorial (x) "FActorial function"
   (if (= x 0) 1
       (* x (factorial (- x 1)))))
(defun univ-factorail (N) "Factorial using universal argument"
       (interactive "p")
       (message "Factorial of %d is %d" N (factorial N)))


More information can be learned from http://ergoemacs.org/emacs/elisp.html


Home > CentOS > CentOS 6.x > Text editors > Emacs > Elisp notes