emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: From macros to elisp programming?


From: Tim Cross
Subject: Re: From macros to elisp programming?
Date: Tue, 07 Dec 2021 09:10:40 +1100
User-agent: mu4e 1.7.5; emacs 28.0.90

Ypo <ypuntot@gmail.com> writes:

> Hi
>
> I am able to make macros, but I think I am far away from Lisp programming.
>
> Is there a path to go from macros to elisp programming? For example, the last 
> macro I've made is for transforming the name of some
> headlines, adding in front of them a part from the previous headline. This is 
> the elisp code of the macro:
>
> #+BEGIN_SRC
> (fset 'SanzTema5
>    (kmacro-lambda-form [?\C-a ?\M-f ?\M-b ?\C-  ?\M-f ?\M-f ?\M-f ?\M-f ?\M-f 
> ?\M-f ?\M-f ?\M-f ?\M-f ?\M-w ?\C-c ?\C-n ?\C-a ?\M-f ?\M-b ?\C-y
> ?  ?- ?  ?\C-e ?\M-b ?\M-f ?\"] 0 "%d"))
> #+END_SRC
>
> Using that code, from these headlines:
>
> *** Sanz Aparicio et al. (2019) "5 Los Motivos Adquiridos, Menéndez Balaña" 
> (pp. 95-118)
> **** INTRODUCCIÓN
>
> I get modified the second headline:
>
> *** Sanz Aparicio et al. (2019) "5 Los Motivos Adquiridos, Menéndez Balaña" 
> (pp. 95-118)
> **** Sanz Aparicio et al. (2019) "5 Los Motivos Adquiridos - INTRODUCCIÓN"
>
> Are macros near to elisp programming or they are two different worlds?
>

Unfortunately, the term 'macro' is somewhat overloaded and can mean
different things depending on the language. For example, C has
pre-processor 'macros' which are essentially simple string replacement
mechanisms and very different from macros as used in lisp languages like
elisp, common lisp or clojure. In a similar vain, what you are referring
to above is a keyboard macro, which is not an elisp macro. A keyboard
macro is essentially a simple 'replay' facility where you can capture a
set of keyboard input actions, give them a name and then repeat those
actions by running the macro with that name again. This is very
different from an elisp macro. An elisp macro (or a common lisp macro,
or a clojure macro etc) is a facility which allows you to manipulate
source code and can be very useful in defining new low level language
constructs which are largely indistinguishable from the built-in
constructs of the language. For example, if elisp did not have a 'when'
statement, you could define it as a macro and it would operate at the
same level as other built in functions. For this reason, macros are
often used a lot when defining a 'domain specific language' (DSL-)  a type of
language dialect which has operators specific to a specialised domain. 

While others would likely disagree, I would not consider using keyboard
macros as programming in emacs lisp. Keyboard macros are an end user
facility which can be used to automate repetitive tasks and which can be
used by anyone using Emacs - you don't need to know any emacs lisp to
create and use a keyboard macro. 

Macros as used in lisp languages are a wonderful, extremely powerful
facility that allow the programmer to do things which are near
impossible in other languages. However, they should be used very
judiciously. There is an old adage which states that if you can do it
with a function, then use a function, not a macro. A common error made
by those new to macros is that they are so amazed at their power, they
now think it is the solution to all problems.

It is difficult to recommend resources as a lot depends on your own
experience, what level you want to dive into and what you want to be
able to do. There is a big difference in knowing enough emacs lisp to
tweak your Emacs configuration and knowing enough emacs lisp to write
the next great package. The good news is that there are some great free
resources out there, so the main investment will be in time.

If you have not yet read it, a good starting point is Mastering Emacs by
Mickey Petersen  https://www.masteringemacs.org/

There is also an Introduction to Emacs Lisp, which is part of the GNU
Emacs distribution.

For getting to understand lisp programming at a much deeper level, there
are some excellent resources out there, many of which are free. The
trick is to not limit yourself to searching just for Emacs lisp. Emacs
lisp is a dialect of lisp - a domain specific language for an editor if
you like. Most of the concepts associated with lisp are applicable to
emacs lisp. almost any resource teaching lisp will teach you things
applicable to emacs lisp. Therefore, if you really want to learn how to
program in emacs lisp, search for resources on learning to program in
lisp.

There is a somewhat famous book called 'The Structure and Interpretation
of Computer Programs" (SCIP), which is based on a course in computer
science taught at MIT. It is an excellent book. Even better, videos from
the course are also available on-line. While the focus is on
programming, it uses a lisp dialect to demonstrate the core principals,
which it makes very clear. A must watch for anyone really keen to
understand programming.

Another great book about lisp which is also freely available is Paul
Graham's 'On Lisp', which I think gives a pretty good overview of the
power and flexibility of lisp and functional programming. 

A book which I really like and which I think really shows off the power
of lisp macros is Doug Hoyte's "Let over Lambda". Not only does this book
show the wonderful power and possibilities of macros, it also shows some
of the dangers and pitfalls. However, it is pretty hard core computer
science focused and can take some effort to really digest. 

If you really want to learn emacs lisp, there is really only one route
you can take - sit down and start using it to write simple programs to
solve some basic problems. Maybe you would like to automate some of what
you do or maybe you would like to modify how Emacs does something or
maybe you want to create a new command to do something. Open a new
window and start trying to implement your idea. You will run into road
blocks and you will often be frustrated, but eventually the penny will
drop. The other big advantage is that when you run into problems, you
will have concrete issues which you can take to a forum for help. It is
much easier for people to help with something concrete than something
more vague or conceptual. The only real way to learn is to do - if you
want to learn emacs lisp, you need to use emacs lisp to solve problems. 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]