emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tim Cross <theophilusx@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: From macros to elisp programming?
Date: Tue, 07 Dec 2021 09:10:40 +1100	[thread overview]
Message-ID: <875ys1ba9n.fsf@gmail.com> (raw)
In-Reply-To: <5b93e27d-df22-3799-7b35-3078472a5154@gmail.com>

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:
> (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"))
> Using that code, from these headlines:
> *** Sanz Aparicio et al. (2019) "5 Los Motivos Adquiridos, Menéndez Balaña" (pp. 95-118)
> 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

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. 

  parent reply	other threads:[~2021-12-06 23:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-06 13:42 From macros to elisp programming? Ypo
2021-12-06 18:08 ` András Simonyi
2021-12-06 18:58   ` Ypo
2021-12-06 22:10 ` Tim Cross [this message]
2021-12-07  9:30   ` Marcin Borkowski
2021-12-07  2:11 ` Eduardo Ochs
2021-12-07 12:55   ` Ypo
2021-12-07 15:16     ` Eduardo Ochs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ys1ba9n.fsf@gmail.com \
    --to=theophilusx@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).