unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* defmacro is worse than my neighbours cat
@ 2008-09-12 21:13 Lennart Borgman (gmail)
  2008-09-12 23:10 ` Alan Mackenzie
  0 siblings, 1 reply; 3+ messages in thread
From: Lennart Borgman (gmail) @ 2008-09-12 21:13 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org

defmacro is worse than my neighbour's cat. When I think I everything is
fine and I calm down they bite.

I have a defmacro where I define a function and I want to build the doc
string for this function in a certain way. This works fine - until I
byte compile it. Can someone please try to explain to me how I can do
what I want?

Below is the defmacro. Look for "fix-me".


(defmacro define-mumamo-multi-major-mode (fun-sym spec-doc chunks)
  "Define a function that turn on support for multiple major modes."
  (let* ((mumamo-describe-chunks (make-symbol "mumamo-describe-chunks"))
         (turn-on-fun (if (symbolp fun-sym)
                          (symbol-value
                           (intern
                            (symbol-name (quote fun-sym))))
                        (error "Parameter FUN-SYM must be a symbol")))
         (turn-on-fun-alias (intern
                             (concat "mumamo-alias-"
                                     (symbol-name
                                      (symbol-value
                                       (intern
                                        (symbol-name (quote fun-sym))))))))
         (turn-on-hook (intern (concat (symbol-name turn-on-fun) "-hook")))
         (turn-on-map  (intern (concat (symbol-name turn-on-fun) "-map")))
         (turn-on-hook-doc (concat "Hook run at the very end of `"
                                   (symbol-name turn-on-fun) "'."))
         (chunks2 (if (symbolp chunks)
                      (symbol-value chunks)
                    chunks))
         (docstring
          (concat
           spec-doc
           "

This function is called a multi major mode.  The main use for it
is in `auto-mode-alist' to have Emacs do this setup whenever you
open a file named in a certain way.  \(You can of course call
this function directly yourself too.)

It sets up for multiple mode in the following way:

"
           ;; Fix-me: During byte compilation the next line is not
           ;; expanded as I thought because the functions in CHUNK is
           ;; not defined. How do I fix this?
           (funcall 'mumamo-describe-chunks chunks2)
           "

At the very end this multi major mode function runs first the hook
`mumamo-turn-on-hook' and then `" (symbol-name turn-on-hook) "'.

There is a keymap specific to this multi major mode, but it is
not returned by `current-local-map' which returns the chunk's
major mode's local keymap.

The keymap is named `" (symbol-name turn-on-map) "'.

This major mode has an alias `mumamo-alias-"
(symbol-name turn-on-fun) "'.
For more information see `define-mumamo-multi-major-mode'."
           )))
    `(progn
       (add-to-list 'mumamo-defined-turn-on-functions (cons (car
',chunks2) ',turn-on-fun))
       (defvar ,turn-on-hook nil ,turn-on-hook-doc)
       (defvar ,turn-on-map (make-sparse-keymap)
         ,(concat "Keymap for multi major mode function `"
                  (symbol-name turn-on-fun) "'"))
       (defvar ,turn-on-fun nil)
       (make-variable-buffer-local ',turn-on-fun)
       (put ',turn-on-fun 'permanent-local t)
       (defun ,turn-on-fun nil ,docstring
         (interactive)
         (let ((old-major-mode (or mumamo-major-mode
                                   major-mode)))
           (kill-all-local-variables)
           (run-hooks 'change-major-mode-hook)
           (setq mumamo-multi-major-mode ',turn-on-fun)
           (setq ,turn-on-fun t)
           (mumamo-add-multi-keymap ',turn-on-fun ,turn-on-map)
           (setq mumamo-current-chunk-family (copy-tree ',chunks2))
           (mumamo-turn-on-actions old-major-mode)
           (run-hooks ',turn-on-hook)))
       (defalias ',turn-on-fun-alias ',turn-on-fun)
       )))




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

* Re: defmacro is worse than my neighbours cat
  2008-09-12 21:13 defmacro is worse than my neighbours cat Lennart Borgman (gmail)
@ 2008-09-12 23:10 ` Alan Mackenzie
  2008-09-12 23:33   ` Lennart Borgman (gmail)
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Mackenzie @ 2008-09-12 23:10 UTC (permalink / raw
  To: Lennart Borgman (gmail); +Cc: help-gnu-emacs@gnu.org

'Morning, Lennart!

On Fri, Sep 12, 2008 at 11:13:10PM +0200, Lennart Borgman (gmail) wrote:
> defmacro is worse than my neighbour's cat.

Miaow!!!

> When I think I everything is fine and I calm down they bite.

> I have a defmacro where I define a function and I want to build the doc
> string for this function in a certain way. This works fine - until I
> byte compile it. Can someone please try to explain to me how I can do
> what I want?

> Below is the defmacro. Look for "fix-me".


> (defmacro define-mumamo-multi-major-mode (fun-sym spec-doc chunks)
>   "Define a function that turn on support for multiple major modes."
>   (let* ((mumamo-describe-chunks (make-symbol "mumamo-describe-chunks"))

OK, here's a problem.  You have _two_ symbols called
"mumamo-describe-chunks" here.  One (uninterned) is the value of the
other.  Why do this?  Why not just declare the symbol directly inside the
let* form and use it?

You really don't need to use make-symbol at all, unless you deliberately
want it uninterned, or you're making up the name as you go along.  You
can simply write

   (let* mumamo-describe-chunks

>          (turn-on-fun (if (symbolp fun-sym)

>                           (symbol-value
>                            (intern
>                             (symbol-name (quote fun-sym))))

This bit seems crazy.  

    (symbol-name (quote fun-sym))

is easier to read as

    (symbol-name 'fun-sym)

which is "fun-sym".  You then "intern" it (i.e. get the (existing)
'fun-sym) and then take the value of 'fun-sym with `symbol-value'.

You don't need this.  You know that (the value of) fun-sym is a symbol,
so these three lines are entirely equivalent to

                            fun-sym

.  ;-)

>                         (error "Parameter FUN-SYM must be a symbol")))
>          (turn-on-fun-alias (intern
>                              (concat "mumamo-alias-"
>                                      (symbol-name
>                                       (symbol-value
>                                        (intern
>                                         (symbol-name (quote fun-sym))))))))

Here you've got one sensible use of intern, and one silly one.  This
should just say:

          (turn-on-fun-alias (intern (concat "mumamo-alias-" fun-sym)))

.  At least I think it should - it's a bit early at the moment.  ;-) 

I've got to get to bed, so I can't look at the rest right now.

>          (turn-on-hook (intern (concat (symbol-name turn-on-fun) "-hook")))
>          (turn-on-map  (intern (concat (symbol-name turn-on-fun) "-map")))
>          (turn-on-hook-doc (concat "Hook run at the very end of `"
>                                    (symbol-name turn-on-fun) "'."))
>          (chunks2 (if (symbolp chunks)
>                       (symbol-value chunks)
>                     chunks))
>          (docstring
>           (concat
>            spec-doc
>            "

> This function is called a multi major mode.  The main use for it
> is in `auto-mode-alist' to have Emacs do this setup whenever you
> open a file named in a certain way.  \(You can of course call
> this function directly yourself too.)

> It sets up for multiple mode in the following way:

> "
>            ;; Fix-me: During byte compilation the next line is not
>            ;; expanded as I thought because the functions in CHUNK is
>            ;; not defined. How do I fix this?
>            (funcall 'mumamo-describe-chunks chunks2)
>            "

> At the very end this multi major mode function runs first the hook
> `mumamo-turn-on-hook' and then `" (symbol-name turn-on-hook) "'.

> There is a keymap specific to this multi major mode, but it is
> not returned by `current-local-map' which returns the chunk's
> major mode's local keymap.

> The keymap is named `" (symbol-name turn-on-map) "'.

> This major mode has an alias `mumamo-alias-"
> (symbol-name turn-on-fun) "'.
> For more information see `define-mumamo-multi-major-mode'."
>            )))
>     `(progn
>        (add-to-list 'mumamo-defined-turn-on-functions (cons (car
> ',chunks2) ',turn-on-fun))
>        (defvar ,turn-on-hook nil ,turn-on-hook-doc)
>        (defvar ,turn-on-map (make-sparse-keymap)
>          ,(concat "Keymap for multi major mode function `"
>                   (symbol-name turn-on-fun) "'"))
>        (defvar ,turn-on-fun nil)
>        (make-variable-buffer-local ',turn-on-fun)
>        (put ',turn-on-fun 'permanent-local t)
>        (defun ,turn-on-fun nil ,docstring
>          (interactive)
>          (let ((old-major-mode (or mumamo-major-mode
>                                    major-mode)))
>            (kill-all-local-variables)
>            (run-hooks 'change-major-mode-hook)
>            (setq mumamo-multi-major-mode ',turn-on-fun)
>            (setq ,turn-on-fun t)
>            (mumamo-add-multi-keymap ',turn-on-fun ,turn-on-map)
>            (setq mumamo-current-chunk-family (copy-tree ',chunks2))
>            (mumamo-turn-on-actions old-major-mode)
>            (run-hooks ',turn-on-hook)))
>        (defalias ',turn-on-fun-alias ',turn-on-fun)
>        )))

-- 
Alan Mackenzie (Nuremberg, Germany).




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

* Re: defmacro is worse than my neighbours cat
  2008-09-12 23:10 ` Alan Mackenzie
@ 2008-09-12 23:33   ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 3+ messages in thread
From: Lennart Borgman (gmail) @ 2008-09-12 23:33 UTC (permalink / raw
  To: Alan Mackenzie; +Cc: help-gnu-emacs@gnu.org

Alan Mackenzie wrote:
> 'Morning, Lennart!
> 
> On Fri, Sep 12, 2008 at 11:13:10PM +0200, Lennart Borgman (gmail) wrote:
>> defmacro is worse than my neighbour's cat.
> 
> Miaow!!!

Help!

>> When I think I everything is fine and I calm down they bite.
> 
>> I have a defmacro where I define a function and I want to build the doc
>> string for this function in a certain way. This works fine - until I
>> byte compile it. Can someone please try to explain to me how I can do
>> what I want?
> 
>> Below is the defmacro. Look for "fix-me".
> 
> 
>> (defmacro define-mumamo-multi-major-mode (fun-sym spec-doc chunks)
>>   "Define a function that turn on support for multiple major modes."
>>   (let* ((mumamo-describe-chunks (make-symbol "mumamo-describe-chunks"))
> 
> OK, here's a problem.  You have _two_ symbols called
> "mumamo-describe-chunks" here.  One (uninterned) is the value of the
> other.  Why do this?

My cleaning ability. I have no idea now why I left it there. Maybe it is
just that I am scared of the byte compiler.

>>          (turn-on-fun (if (symbolp fun-sym)
> 
>>                           (symbol-value
>>                            (intern
>>                             (symbol-name (quote fun-sym))))
> 
> This bit seems crazy.  

Yes. Thanks. ;-)

> .  At least I think it should - it's a bit early at the moment.  ;-) 

Ok, I am looking forward for the rest ...

> I've got to get to bed, so I can't look at the rest right now.
> 
>>          (turn-on-hook (intern (concat (symbol-name turn-on-fun) "-hook")))
>>          (turn-on-map  (intern (concat (symbol-name turn-on-fun) "-map")))
>>          (turn-on-hook-doc (concat "Hook run at the very end of `"
>>                                    (symbol-name turn-on-fun) "'."))
>>          (chunks2 (if (symbolp chunks)
>>                       (symbol-value chunks)
>>                     chunks))
>>          (docstring
>>           (concat
>>            spec-doc
>>            "
> 
>> This function is called a multi major mode.  The main use for it
>> is in `auto-mode-alist' to have Emacs do this setup whenever you
>> open a file named in a certain way.  \(You can of course call
>> this function directly yourself too.)
> 
>> It sets up for multiple mode in the following way:
> 
>> "
>>            ;; Fix-me: During byte compilation the next line is not
>>            ;; expanded as I thought because the functions in CHUNK is
>>            ;; not defined. How do I fix this?
>>            (funcall 'mumamo-describe-chunks chunks2)
>>            "
> 
>> At the very end this multi major mode function runs first the hook
>> `mumamo-turn-on-hook' and then `" (symbol-name turn-on-hook) "'.
> 
>> There is a keymap specific to this multi major mode, but it is
>> not returned by `current-local-map' which returns the chunk's
>> major mode's local keymap.
> 
>> The keymap is named `" (symbol-name turn-on-map) "'.
> 
>> This major mode has an alias `mumamo-alias-"
>> (symbol-name turn-on-fun) "'.
>> For more information see `define-mumamo-multi-major-mode'."
>>            )))
>>     `(progn
>>        (add-to-list 'mumamo-defined-turn-on-functions (cons (car
>> ',chunks2) ',turn-on-fun))
>>        (defvar ,turn-on-hook nil ,turn-on-hook-doc)
>>        (defvar ,turn-on-map (make-sparse-keymap)
>>          ,(concat "Keymap for multi major mode function `"
>>                   (symbol-name turn-on-fun) "'"))
>>        (defvar ,turn-on-fun nil)
>>        (make-variable-buffer-local ',turn-on-fun)
>>        (put ',turn-on-fun 'permanent-local t)
>>        (defun ,turn-on-fun nil ,docstring
>>          (interactive)
>>          (let ((old-major-mode (or mumamo-major-mode
>>                                    major-mode)))
>>            (kill-all-local-variables)
>>            (run-hooks 'change-major-mode-hook)
>>            (setq mumamo-multi-major-mode ',turn-on-fun)
>>            (setq ,turn-on-fun t)
>>            (mumamo-add-multi-keymap ',turn-on-fun ,turn-on-map)
>>            (setq mumamo-current-chunk-family (copy-tree ',chunks2))
>>            (mumamo-turn-on-actions old-major-mode)
>>            (run-hooks ',turn-on-hook)))
>>        (defalias ',turn-on-fun-alias ',turn-on-fun)
>>        )))
> 




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

end of thread, other threads:[~2008-09-12 23:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-12 21:13 defmacro is worse than my neighbours cat Lennart Borgman (gmail)
2008-09-12 23:10 ` Alan Mackenzie
2008-09-12 23:33   ` Lennart Borgman (gmail)

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