Re: From macros to elisp programming?

2021-12-07 Thread Eduardo Ochs
Hi Ypo!

You may find this useful too. I often use it when I want to put
editing commands in my functions and I'm in a hurry and not in a mood
to find their names - so for example I use (eek "C-l") instead of
(recenter-top-bottom), or (eek " C-a C-SPC  M-w C-y") to
duplicate the line above the point...

  Cheers & happy hacking =),
E.
http://angg.twu.net/#eev


;; See:
;; http://angg.twu.net/eev-intros/find-eev-quick-intro.html#3
;; (find-eev-quick-intro "3. Elisp hyperlinks")
;;
(defun eek (str)
  "Execute STR as a keyboard macro. See `edmacro-mode' for the exact format.\n
An example: (eek \"C-x 4 C-h\")"
  (interactive "sKeys: ")
  (execute-kbd-macro (read-kbd-macro str)))

On Tue, 7 Dec 2021 at 09:55, Ypo  wrote:
>
> Obrigado, Eduardo!
>
> I didn't know that command: "setq last-kbd-macro" and probably it will be 
> useful with elmacro too.
>
> (outline-next-visible-heading) ;; C-c C-n
>
> I save your instructions to my notes to use them :-)
>
> Saúde!
>
>
> El 07/12/2021 a las 3:11, Eduardo Ochs escribió:
>
> On Mon, 6 Dec 2021 at 10:43, Ypo  wrote:
>
> 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?
>
> Ypo
>
> Hi Ypo,
>
> here's a suggestion. If you run this
>
>   (setq last-kbd-macro [?\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 ?\"])
>
> and then type `C-x C-k C-e' (`kmacro-edit-macro-repeat') you will get
> a temporary buffer that contains this:
>
> C-a ;; move-beginning-of-line
> M-f ;; forward-word
> M-b ;; backward-word
> C-SPC   ;; set-mark-command
> 9*M-f   ;; forward-word
> M-w ;; kill-ring-save
> C-c C-n
> C-a ;; move-beginning-of-line
> M-f ;; forward-word
> M-b ;; backward-word
> C-y ;; yank
> SPC ;; self-insert-command
> -   ;; self-insert-command
> SPC ;; self-insert-command
> C-e ;; move-end-of-line
> M-b ;; backward-word
> M-f ;; forward-word
> "   ;; self-insert-command
>
> If you convert that by hand - suggestion: use keyboard macros for
> that! =) - to a defun like this one,
>
>   (defun SanzTema5 ()
> (interactive)
> (move-beginning-of-line)   ; C-a
> (forward-word) ; M-f
> (backward-word); M-b
> (set-mark-command) ; C-SPC
> (dotimes 9 (forward-word)) ; 9*M-f
> ;; ^ or: (forward-word 9)
> (kill-ring-save)   ; M-w
> ;; What here? I don' have a:   ; C-c C-n
> (move-beginning-of-line)   ; C-a
> (forward-word) ; M-f
> (backward-word); M-b
> (yank) ; C-y
> (insert " - ") ; SPC - SPC
> (move-end-of-line) ; C-e
> (backward-word); M-b
> (forward-word) ; M-f
> (insert "\"")  ; "
> )
>
> Then you will get a defun that does something that is clearly useful
> to you... you can start by creating and debugging a handful of defuns
> like that one and then learn what most people consider as "real
> Elisp"...
>
>   Cheers =),
> Eduardo Ochs
> http://angg.twu.net/#eev



Re: From macros to elisp programming?

2021-12-07 Thread Ypo

Obrigado, Eduardo!

I didn't know that command: "setq last-kbd-macro" and probably it will 
be useful with elmacro too.


(outline-next-visible-heading) ;; C-c C-n

I save your instructions to my notes to use them :-)

Saúde!


El 07/12/2021 a las 3:11, Eduardo Ochs escribió:

On Mon, 6 Dec 2021 at 10:43, Ypo  wrote:

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?

Ypo


Hi Ypo,

here's a suggestion. If you run this

   (setq last-kbd-macro [?\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 ?\"])

and then type `C-x C-k C-e' (`kmacro-edit-macro-repeat') you will get
a temporary buffer that contains this:

 C-a ;; move-beginning-of-line
 M-f ;; forward-word
 M-b ;; backward-word
 C-SPC   ;; set-mark-command
 9*M-f   ;; forward-word
 M-w ;; kill-ring-save
 C-c C-n
 C-a ;; move-beginning-of-line
 M-f ;; forward-word
 M-b ;; backward-word
 C-y ;; yank
 SPC ;; self-insert-command
 -   ;; self-insert-command
 SPC ;; self-insert-command
 C-e ;; move-end-of-line
 M-b ;; backward-word
 M-f ;; forward-word
 "   ;; self-insert-command

If you convert that by hand - suggestion: use keyboard macros for
that! =) - to a defun like this one,

   (defun SanzTema5 ()
 (interactive)
 (move-beginning-of-line)   ; C-a
 (forward-word) ; M-f
 (backward-word); M-b
 (set-mark-command) ; C-SPC
 (dotimes 9 (forward-word)) ; 9*M-f
 ;; ^ or: (forward-word 9)
 (kill-ring-save)   ; M-w
 ;; What here? I don' have a:   ; C-c C-n
 (move-beginning-of-line)   ; C-a
 (forward-word) ; M-f
 (backward-word); M-b
 (yank) ; C-y
 (insert " - ") ; SPC - SPC
 (move-end-of-line) ; C-e
 (backward-word); M-b
 (forward-word) ; M-f
 (insert "\"")  ; "
 )

Then you will get a defun that does something that is clearly useful
to you... you can start by creating and debugging a handful of defuns
like that one and then learn what most people consider as "real
Elisp"...

   Cheers =),
 Eduardo Ochs
 http://angg.twu.net/#eev

Re: From macros to elisp programming?

2021-12-07 Thread Marcin Borkowski


On 2021-12-06, at 23:10, Tim Cross  wrote:

> 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.

+1

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

Agreed, though AFAIR it doesn't talk about coding in Elisp, only /using/
Emacs.

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

I second that.  It is a great book for beginners.

> 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.

On the other hand, Elisp has quite a few quirks which make it stand
out.  These mare mainly related to the fact that Emacs is a text editor
and not an interpreter for a general-purpose language.  Things like
point, windows, buffers, using buffers to process text (as opposed to
strings in 99% langauges), buffer-local variables, advice system...

> 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.

+100.  Those videos are great (the book is more in-depth, though).

> 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. 

Yes, though it it way more advanced - I probably wouldn't give it to
a beginner.

> 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. 

Also, one can go a really long way /without/ macros.  I'd argue that the
main reason a beginning Elisper should learn the basics of (Elisp)
macros is to be able to (roughly) understand what is going on when s/he
sees them in other people's code.

> 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.

Again, very solid advice.  Though for some people at least some guidance
may be valuable.  One example would be "Writing GNU Emacs Extensions" by
Bob Glickstein.  It is fairly old, but from what I heard most of it
should be still relevant.  Word of caution: I'm not sure I'd agree with
Glickstein's selection of topics.  For example, he writes about writing
major modes or autoloading.  The former is IMO not very useful unless
you are doing something /extremely/ atypical.  The latter is very
advanced (and not really essential for 99.99% or even 100% use-cases),
and by the time the learner /needs/ it, s/he will be advanced enough to
learn it from the GNU Emacs Lisp Reference Manual anyway.

Speaking of which, it also an excellent resource not to be overlooked -
and it comes with Emacs itself.

Another thing I'd like to add here is that asking on this list is
usually a good idea.  I learned a lot (and I'm still learning a lot)
just by 

Re: From macros to elisp programming?

2021-12-06 Thread Eduardo Ochs
On Mon, 6 Dec 2021 at 10:43, Ypo  wrote:
>
> 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?
>
> Ypo


Hi Ypo,

here's a suggestion. If you run this

  (setq last-kbd-macro [?\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 ?\"])

and then type `C-x C-k C-e' (`kmacro-edit-macro-repeat') you will get
a temporary buffer that contains this:

C-a ;; move-beginning-of-line
M-f ;; forward-word
M-b ;; backward-word
C-SPC   ;; set-mark-command
9*M-f   ;; forward-word
M-w ;; kill-ring-save
C-c C-n
C-a ;; move-beginning-of-line
M-f ;; forward-word
M-b ;; backward-word
C-y ;; yank
SPC ;; self-insert-command
-   ;; self-insert-command
SPC ;; self-insert-command
C-e ;; move-end-of-line
M-b ;; backward-word
M-f ;; forward-word
"   ;; self-insert-command

If you convert that by hand - suggestion: use keyboard macros for
that! =) - to a defun like this one,

  (defun SanzTema5 ()
(interactive)
(move-beginning-of-line)   ; C-a
(forward-word) ; M-f
(backward-word); M-b
(set-mark-command) ; C-SPC
(dotimes 9 (forward-word)) ; 9*M-f
;; ^ or: (forward-word 9)
(kill-ring-save)   ; M-w
;; What here? I don' have a:   ; C-c C-n
(move-beginning-of-line)   ; C-a
(forward-word) ; M-f
(backward-word); M-b
(yank) ; C-y
(insert " - ") ; SPC - SPC
(move-end-of-line) ; C-e
(backward-word); M-b
(forward-word) ; M-f
(insert "\"")  ; "
)

Then you will get a defun that does something that is clearly useful
to you... you can start by creating and debugging a handful of defuns
like that one and then learn what most people consider as "real
Elisp"...

  Cheers =),
Eduardo Ochs
http://angg.twu.net/#eev



Re: From macros to elisp programming?

2021-12-06 Thread Tim Cross


Ypo  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 

Re: From macros to elisp programming?

2021-12-06 Thread Ypo

Lovable!!

Thanks!
Ypo

El 06/12/2021 a las 19:08, András Simonyi escribió:

Dear All,

On Mon, 6 Dec 2021 at 14:43, Ypo  wrote:


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?

there is a package called 'elmacro' (https://github.com/Silex/elmacro)
which you can look into. Apparently, it can convert  (most?) macros
into interactive Elisp functions, and might even provide a good
starting point  for learning Elisp.

best wishes,
András

Re: From macros to elisp programming?

2021-12-06 Thread András Simonyi
Dear All,

On Mon, 6 Dec 2021 at 14:43, Ypo  wrote:

> 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?

there is a package called 'elmacro' (https://github.com/Silex/elmacro)
which you can look into. Apparently, it can convert  (most?) macros
into interactive Elisp functions, and might even provide a good
starting point  for learning Elisp.

best wishes,
András



From macros to elisp programming?

2021-12-06 Thread Ypo

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?

Ypo