all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Why is defun not executed during load-file?
@ 2021-05-30  9:02 Jean Louis
  2021-05-30 13:31 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-30 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 15+ messages in thread
From: Jean Louis @ 2021-05-30  9:02 UTC (permalink / raw)
  To: Help GNU Emacs

I have this in the hyperscope.el and upon load-file the last function
is not executed, it does not generate the functions. But if I evaluate
it specifically, it generates functions.

Is there any reason for that?

(defun hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
  (eval `(defun ,(intern (symbol-name function-name)) ()
	   ,(format "Add new `%s' hyperdocument to Hyperscope." type-name)
	   (interactive)
	   (let* ((parent (hyperscope-select-set))
		  (prompt ,(format "New `%s' hyperdocument name: " type-name))
		  (name (read-from-minibuffer prompt)))
	     (hlink-add-generic name "" ,id parent nil)))))

(defun hyperscope-generate-add-functions-by-hyperdocument-types ()
  (let ((types (hyperscope-hyperdocument-types)))
    (while types
      (let* ((type (pop types))
	     (id (nth 0 type))
	     (type (nth 1 type))
	     (type-name type)
	     (type (downcase (string-replace " " "-" type)))
	     (function-name (intern (format "hyperscope-add-new-%s-hyperdocument" type))))
	(hyperscope-generate-the-add-function-by-hyperdocument-type type-name id function-name)))))

(hyperscope-generate-add-functions-by-hyperdocument-types)



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30  9:02 Why is defun not executed during load-file? Jean Louis
@ 2021-05-30 13:31 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-30 15:36   ` Jean Louis
  2021-05-30 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 15+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-05-30 13:31 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis [2021-05-30 12:02:25] wrote:
> I have this in the hyperscope.el and upon load-file the last function
> is not executed, it does not generate the functions. But if I evaluate
> it specifically, it generates functions.
>
> Is there any reason for that?
>
> (defun hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
>   (eval `(defun ,(intern (symbol-name function-name)) ()

My crystal ball says it's punishment for using `eval` and recommends
using macro(s) instead.  I wouldn't trust its judgment, but the
recommendation sounds useful anyway.

BTW, `intern + symbol-name` ends up a no-op if `function-name` is
already an interned symbol.


        Stefan




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 13:31 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-05-30 15:36   ` Jean Louis
  2021-05-30 16:32     ` Stefan Monnier
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2021-05-30 15:36 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

* Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2021-05-30 16:32]:
> Jean Louis [2021-05-30 12:02:25] wrote:
> > I have this in the hyperscope.el and upon load-file the last function
> > is not executed, it does not generate the functions. But if I evaluate
> > it specifically, it generates functions.
> >
> > Is there any reason for that?
> >
> > (defun hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
> >   (eval `(defun ,(intern (symbol-name function-name)) ()
> 
> My crystal ball says it's punishment for using `eval` and recommends
> using macro(s) instead.  I wouldn't trust its judgment, but the
> recommendation sounds useful anyway.
> 
> BTW, `intern + symbol-name` ends up a no-op if `function-name` is
> already an interned symbol.

I just did not find a way to make it working. Now I have put more care
and I got this to work:

(defmacro hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
  (list 'defun function-name '()
	(format "Add new `%s' hyperdocument to Hyperscope." type-name)
	'(interactive)
	'(let* ((parent (hyperscope-select-set))
		(prompt ,(format "New `%s' hyperdocument name: " type-name))
		(name (read-from-minibuffer prompt)))
	   (hlink-add-generic name "" ,id parent nil))))

(defun hyperscope-generate-add-functions-by-hyperdocument-types ()
  (let ((types (hyperscope-hyperdocument-types)))
    (while types
      (let* ((type (pop types))
	     (id (nth 0 type))
	     (type (nth 1 type))
	     (type-name type)
	     (type (downcase (string-replace " " "-" type)))
	     (function-name (format "hyperscope-add-new-%s-hyperdocument" type)))
	(insert "\n" type-name " " id " " function-name)
	(hyperscope-generate-the-add-function-by-hyperdocument-type type-name id function-name)))))

(hyperscope-generate-add-functions-by-hyperdocument-types)

Now, this does not use `eval' and generates functions when I evaluate
it.

But again it will not generate functions when I load the file.

Is that supposed to be so?

This is smallest example:

(defmacro my-generate-function (name)
  (list 'defun (intern name) '()
	"My function"
	'(interactive)
	'(message "hello")))

(my-generate-function "my-hello3")

and that example works well, it does generate function `my-hello3' but
my above doesn't.

On the other hand I have observed if I do just one time:

(fmakunbound 'hyperscope-add-new-markdown-hyperdocument)

then this function will never be generated again in the same
instance. I have to run another instance to be able to generate that
function.

Do you know why is that?


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 15:36   ` Jean Louis
@ 2021-05-30 16:32     ` Stefan Monnier
  2021-05-30 19:56       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier @ 2021-05-30 16:32 UTC (permalink / raw)
  To: help-gnu-emacs

> I just did not find a way to make it working. Now I have put more care
> and I got this to work:
>
> (defmacro hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
>   (list 'defun function-name '()
> 	(format "Add new `%s' hyperdocument to Hyperscope." type-name)
> 	'(interactive)
> 	'(let* ((parent (hyperscope-select-set))
> 		(prompt ,(format "New `%s' hyperdocument name: " type-name))
> 		(name (read-from-minibuffer prompt)))
> 	   (hlink-add-generic name "" ,id parent nil))))
>
> (defun hyperscope-generate-add-functions-by-hyperdocument-types ()
>   (let ((types (hyperscope-hyperdocument-types)))
>     (while types
>       (let* ((type (pop types))
> 	     (id (nth 0 type))
> 	     (type (nth 1 type))
> 	     (type-name type)
> 	     (type (downcase (string-replace " " "-" type)))
> 	     (function-name (format "hyperscope-add-new-%s-hyperdocument" type)))
> 	(insert "\n" type-name " " id " " function-name)
> 	(hyperscope-generate-the-add-function-by-hyperdocument-type type-name id function-name)))))
>
> (hyperscope-generate-add-functions-by-hyperdocument-types)

Try to `M-: macroexpand-all '(defun hyperscope-generate-add-functions-by-hyperdocument-types ...))
to see that your code doesn't do what you think:
`hyperscope-generate-add-functions-by-hyperdocument-types` needs to be
a macro.


        Stefan




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30  9:02 Why is defun not executed during load-file? Jean Louis
  2021-05-30 13:31 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-05-30 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-30 21:56   ` Jean Louis
  1 sibling, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-30 19:44 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>   (eval `(defun ,(intern (symbol-name function-name)) ()

One-liner of the year award :)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 16:32     ` Stefan Monnier
@ 2021-05-30 19:56       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-30 21:46         ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-30 19:56 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier wrote:

> Try to `M-: macroexpand-all '(defun
> hyperscope-generate-add-functions-by-hyperdocument-types
> ...)) to see that your code doesn't do what you think:
> `hyperscope-generate-add-functions-by-hyperdocument-types`
> needs to be a macro.

OK, in this case you mean?

Because in general you don't need a macro to generate
a function?

I wrote this:

(defmacro file-to-variable (file var)
  `(setq ,var ,(file-to-string file)) )

(defmacro file-to-variable-integer (file var)
  `(setq ,var ,(string-to-number (file-to-string file))) )

I wrote that to get a function? Don't remember...

https://dataswamp.org/~incal/emacs-init/file-write-to.el

Also from the unofficial code:

(defun syms-status (sym-list)
  (mapcar (lambda (s) (list s (symbol-value s))) sym-list))

(defmacro vars-status (&rest sym-list)
  `(syms-status ',sym-list))

Also don't remember :)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 19:56       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-30 21:46         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-05-30 21:55           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-05-30 21:46 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor [2021-05-30 21:56:05] wrote:
> Stefan Monnier wrote:
>> Try to `M-: macroexpand-all '(defun
>> hyperscope-generate-add-functions-by-hyperdocument-types
>> ...)) to see that your code doesn't do what you think:
>> `hyperscope-generate-add-functions-by-hyperdocument-types`
>> needs to be a macro.
> OK, in this case you mean?

Everything you quoted from me above was written specifically about your code.
So very much "this case", yes.


        Stefan




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 21:46         ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-05-30 21:55           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-30 21:55 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

>> OK, in this case you mean?
>
> Everything you quoted from me above was written specifically
> about your code. So very much "this case", yes.

OK...

But actually, how do you put it, it wasn't me :)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-30 21:56   ` Jean Louis
  2021-05-30 22:07     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2021-05-30 21:56 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2021-05-30 22:47]:
> Jean Louis wrote:
> 
> >   (eval `(defun ,(intern (symbol-name function-name)) ()
> 
> One-liner of the year award :)

I still did not make it. It works when evaluated with hand, but not
with load-file.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 21:56   ` Jean Louis
@ 2021-05-30 22:07     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-30 22:39       ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-30 22:07 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>>> (eval `(defun ,(intern (symbol-name function-name)) ()
>> 
>> One-liner of the year award :)
>
> I still did not make it. It works when evaluated with hand,
> but not with load-file.

Macros are much more difficult to debug...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 22:07     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-30 22:39       ` Jean Louis
  2021-05-30 23:26         ` Michael Heerdegen
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2021-05-30 22:39 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2021-05-31 01:11]:
> Jean Louis wrote:
> 
> >>> (eval `(defun ,(intern (symbol-name function-name)) ()
> >> 
> >> One-liner of the year award :)
> >
> > I still did not make it. It works when evaluated with hand,
> > but not with load-file.
> 
> Macros are much more difficult to debug...

I am fine to generate functions without macros, just tell me how. I
need names of functions dynamically generated.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
@ 2021-05-30 22:48 Drew Adams
  2021-05-30 22:57 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 15+ messages in thread
From: Drew Adams @ 2021-05-30 22:48 UTC (permalink / raw)
  To: Help-Gnu-Emacs (help-gnu-emacs@gnu.org); +Cc: Emanuel Berg

> Macros are much more difficult to debug...

`macroexpand` and `macroexpand-1` are your friends.

Separate that first step, which is just reduction
(aka rewriting / reduction semantics), from the
subsequent step of Lisp-evaluating the sexp that
results from it.

So debug the macro-expansion step, to ensure you
get the sexp you want.  And debug evaluation of
that sexp - separately.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 22:48 Drew Adams
@ 2021-05-30 22:57 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-30 22:57 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

>> Macros are much more difficult to debug...
>
> `macroexpand` and `macroexpand-1` are your friends.

Also, writing macros compared to writing functions typically
involves more bugs...

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 22:39       ` Jean Louis
@ 2021-05-30 23:26         ` Michael Heerdegen
  2021-05-30 23:37           ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: Michael Heerdegen @ 2021-05-30 23:26 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> I am fine to generate functions without macros, just tell me how. I
> need names of functions dynamically generated.

I think you can use `defalias', a simple function.  That would require
to use lexical binding so that variables like `prompt' can get captured
in a closure.  That would look like this:

#+begin_src emacs-lisp
;;;; Your first file line   -*- lexical-binding: t -*-
;; [...]
(defun hyperscope-generate-the-add-function-by-hyperdocument-type (type-name id function-name)
  (defalias function-name
    (lambda ()
      (interactive)
      (let* ((parent (hyperscope-select-set))
	     (prompt (format "New `%s' hyperdocument name: " type-name))
	     (name (read-from-minibuffer prompt)))
        (hlink-add-generic name "" id parent nil)))
    ;; Docstring gets an argument of `defalias':
    (format "Add new `%s' hyperdocument to Hyperscope." type-name)))
#+end_src


Michael.




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Why is defun not executed during load-file?
  2021-05-30 23:26         ` Michael Heerdegen
@ 2021-05-30 23:37           ` Jean Louis
  0 siblings, 0 replies; 15+ messages in thread
From: Jean Louis @ 2021-05-30 23:37 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

* Michael Heerdegen <michael_heerdegen@web.de> [2021-05-31 02:27]:
> Jean Louis <bugs@gnu.support> writes:
> 
> > I am fine to generate functions without macros, just tell me how. I
> > need names of functions dynamically generated.
> 
> I think you can use `defalias', a simple function.  That would require
> to use lexical binding so that variables like `prompt' can get captured
> in a closure.  That would look like this:


That I must try. Thanks.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2021-05-30 23:37 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-30  9:02 Why is defun not executed during load-file? Jean Louis
2021-05-30 13:31 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-30 15:36   ` Jean Louis
2021-05-30 16:32     ` Stefan Monnier
2021-05-30 19:56       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-30 21:46         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-30 21:55           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-30 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-30 21:56   ` Jean Louis
2021-05-30 22:07     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-30 22:39       ` Jean Louis
2021-05-30 23:26         ` Michael Heerdegen
2021-05-30 23:37           ` Jean Louis
  -- strict thread matches above, loose matches on Subject: below --
2021-05-30 22:48 Drew Adams
2021-05-30 22:57 ` Emanuel Berg via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.