Search code examples
macroselispmessage

Message in macro printed twice


I wrote a function which can accept any kind, any number of arguments and so that it can print the name and value of the arguments. The function works as expected. But I don't like the function call requires me to pass a quote of value like this (my-message 'emacs-version 'emacs-copyright). I want to simplified to (my-message emacs-version emacs-copyright). Therefore I use macro to rewrite the function.

(defmacro my-message (&rest args)
  (if args
      (progn
        (message "This is the start of debug message.\n")
        (dolist (arg args)
          (cond
           ((stringp arg)
            (message arg))
           ((numberp arg)
            (message (number-to-string arg)))
           ((boundp arg)
            (pp arg)
            (message "")
            (pp (symbol-value arg)))
           ((not (boundp arg))
            (pp arg)
            (message "Undefined")))
          (message "\n"))
        (message "This is the end of debug message."))
    (message "This is a debug message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")))

However, the some of the message are printed twice.

(my-message emacs-version emacs-copyright 12345 "HelloWorld" foobar)

This is the start of debug message.

emacs-version
"24.5.1"
 [2 times]
emacs-copyright
"Copyright (C) 2015 Free Software Foundation, Inc."
 [2 times]
12345
 [2 times]
HelloWorld
 [2 times]
foobar
Undefined
 [2 times]
This is the end of debug message.

What is the problem?


Solution

  • I think the message [2 times] in your output refers to an extra newline character.

    This behavior can be reproduced by evaluating

    (progn
      (message "HelloWorld")
      (message "\n"))
    

    in *scratch*. The output in *messages* is

    HelloWorld
    [2 times]
    "
    "
    

    I wonder if this behavior is intended.

    Maybe I did misunderstand your question since my answer has nothing to do with macros.