The interactive
special form provides the easiest way to get input from a user.
(defun td (variable)
(interactive "sVariable:")
(insert (format "std::cout << \"%s is: \" << %s << std::endl;" variable variable)))
Here "sVariable:"
consists of the "s"
code character (read a string) and the prompt. (See Using interactive in the Emacs Lisp Manual for more.)
In addition to using a string with with code characters, the interactive
special form can use a lisp form as its argument descriptor; this form should evaluate to a list of arguments.
This lets an interactive command compute its arguments from the context, record argument history, and so on.
read-from-minibuffer
supports both setting the initial value and recording history; with its help we can make td
remember what was entered and offer it upon next invocation.
(defun td (stream variable)
(interactive
(list
(read-from-minibuffer "Stream: "
(when (boundp 'td-history) (car td-history))
nil nil 'td-history)
(read-from-minibuffer "Variable: ")))
(insert (format "%s << \"%s is: \" << %s << std::endl;" stream variable variable)))
PS: Since you are debugging C++ code, you can add __FILE__
and __LINE__
macros, to get something similar to
(defun td (variable)
(interactive "sVariable:")
(insert (format "std::cout << \"file \" << __FILE__ << \" line \" << __LINE__ << \" %s is: \" << %s << std::endl;" variable variable)))
goto-line
to see if you can figure it out. UseC-h f goto-line RET
and click on the link that sayssimple.el
to see the source.read-minibuffer
andformat
in the manual. (And, for good measure here's the intro to elisp you can reference as well.)yasnippet
DBG(foo)
) from the normal output, and disable the debug code in production.