help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I debug a macro?


From: Phillip Lord
Subject: Re: How can I debug a macro?
Date: Wed, 16 Dec 2015 21:21:28 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> On 2015-12-16 10:43, phillip.lord@russet.org.uk (Phillip Lord) writes:
>
>> If you want to debug a *usage* of the macro, then the macro itself needs
>> to declare to edebug how it should be evaled. In most cases this is
>> quite easy. For `org-babel-comint-with-output' it's slightly tricker
>> because AFAICT the `meta' argument is not evaluated.
>
> I want to debug the usage of the macro. It returns the wrong value and
> I want to understand why.
>
>>
>> (def-edebug-spec org-babel-comint-with-output
>>   (form body))
>>
>> should do the trick, although probably you want to send a patch in for
>> org-comint.el (using the declare version) so that it works for everyone.
>
> I'm trying this, but I'm confused at to what I need to do after I add
> the declare form.

Sorry, I should have given a complete example. But I didn't know how to
call org-babel-comint-with-output, so I was guessing.


>
> This is what I tried:
> - add the declare form to org-babel-comint-with-output

So, on closer insepction I realise that there is an edebug spec
immediately after the macro.

(def-edebug-spec org-babel-comint-with-output (sexp body))


> - C-u C-M-x on org-babel-comint-with-output

Yeah, don't do that. Otherwise, you will be debugging the macro
expansion.

> - evaluate a block in org-mode
>
> I then see a message:
> edebug: Symbol's value as variable is void: edebug-def-mark

Yep, sorry by debug form was a bit wrong.

> I then tried to debug the function calling the macro:
> - C-u C-M-x on org-babel-execute:ocaml
> - strangely, a debugging start even though I have not called the
> function yet (I have not evaluated the source block)

That's correct. You've instrumented org-babel-comint-with-output, so it
gets evaluated at the time you eval org-babel-execute:ocaml. The macro
returns some form, of course, and this will be evaluated when the
calling function is evaled.


> I feel like I'm missing something here. Am I using this correctly?

Try the debug statement given here. You should now get step through
debugging of the arguments to org-babel-comint-with-output (or the
second one anyway -- the first one is not evaluated).

If this still doesn't help, and you need to debug the *macro* not the
usage, then put point after this after do M-x pp-macroexpand-last-sexp

(org-babel-comint-with-output
                  (session org-babel-ocaml-eoe-output t full-body)
                (insert
                 (concat
                  (org-babel-chomp
                  full-body)";;\n"org-babel-ocaml-eoe-indicator))

which will give you this.


(org-babel-comint-in-buffer session
  (let
      ((string-buffer "")
       dangling-text raw)
    (setq comint-output-filter-functions
          (cons
           (lambda
             (text)
             (setq string-buffer
                   (concat string-buffer text)))
           comint-output-filter-functions))
    (unwind-protect
        (progn
          (goto-char
           (process-mark
            (get-buffer-process
             (current-buffer))))
          (let
              ((start
                (point))
               (end
                (point-max)))
            (setq dangling-text
                  (buffer-substring start end))
            (delete-region start end))
          (insert
           (concat
            (org-babel-chomp full-body)
            ";;\n" org-babel-ocaml-eoe-indicator))
          (tuareg-interactive-send-input)
          (while
              (progn
                (goto-char comint-last-input-end)
                (not
                 (save-excursion
                   (and
                    (re-search-forward
                     (regexp-quote org-babel-ocaml-eoe-output)
                     nil t)
                    (re-search-forward comint-prompt-regexp nil t)))))
            (accept-process-output
             (get-buffer-process
              (current-buffer))))
          (goto-char
           (process-mark
            (get-buffer-process
             (current-buffer))))
          (insert dangling-text))
      (setq comint-output-filter-functions
            (cdr comint-output-filter-functions)))
    (if
        (and t full-body
             (string-match
              (replace-regexp-in-string "\n" "[\n]+"
                                        (regexp-quote
                                         (or full-body "")))
              string-buffer))
        (setq raw
              (substring string-buffer
                         (match-end 0))))
    (split-string string-buffer comint-prompt-regexp)))


Unfortunately, it's got another macro inside. If you *really* need to
debug this, then, the best you can do is put org-babel-execute:ocaml
into a file, and replace the org-babel-comint-with-output form with this
expansion. Then you can run edebug directly on that.

I'd rather not have a macro with such a large expansion myself, because
it is hard to debug, but sometimes it's necessary.

Phil



reply via email to

[Prev in Thread] Current Thread [Next in Thread]