1

I have been trying to make auto-save compilation rules for my org files. I have tested the following functions in the interpreter:

(defun async-shell-command-no-window (command)
  (interactive)
  (let ((display-buffer-alist
         (list
          (cons
           "\\*Async Shell Command\\*.*"
           (cons #'display-buffer-no-window nil)))))
    (async-shell-command
     command)))

(defun haozeke/org-pandoc-markdown (dir &optional pargs)
  "A wrapper to generate yaml metadata markdown files. Takes the output
  directory followed by pandoc arguments"
  (if (not (file-exists-p dir)) (make-directory dir))
  (async-shell-command-no-window
   (concat "pandoc -f org -t markdown -s " pargs " " (buffer-name) " -o "
           dir "/" (file-name-sans-extension (buffer-name)) ".md")))

(haozeke/org-pandoc-markdown "./")

Where the first function has been adapted from this question.

The error I get is:

run-hooks: Wrong number of arguments: (lambda (dir &optional pargs) "A wrapper to generate yaml metadata markdown files. Takes the output
  directory followed by pandoc arguments" (if (not (file-exists-p dir)) (make-directory dir)) (async-shell-command-no-window (concat "pandoc -f org -t markdown -s " pargs " " (buffer-name) " -o " dir ".md"))), 0

The local variable setup I have is:

# Local Variables:
# after-save-hook: haozeke/org-pandoc-markdown "test"
# End:
0

haozeke/org-pandoc-markdown "test" are 2 elements, not one. So, in your local variables section, the line:

# after-save-hook: haozeke/org-pandoc-markdown "test"

is an error. I'm not sure how Emacs treats it, but ideally it should emit a warning about the syntax error (something like «extraneous text after value: "test"»). Apparently it does accept the variable setting and treats it as if you had written:

# after-save-hook: haozeke/org-pandoc-markdown

Which is why you get the error you show: after-save-hook will call this function with no arguments, whereas it expects at least one argument (the "dir").

Another problem is that this setting overrules all other functions that might have been added to your after-save-hook. Yet another problem is that you don't want to add the function haozeke/org-pandoc-markdown to your after-save-hook but instead you want to add a call (haozeke/org-pandoc-markdown "test") to that hook, but hooks don't accept "function calls", only functions.

IOW, you want to use a line like:

# eval: (add-hook 'after-save-hook (lambda () (haozeke/org-pandoc-markdown "test")) nil 'local)
  • Thanks so much! I have no idea how I would have figured that out otherwise. Just to clarify though, with the eval style, how would I manage multiple after-save-hook functions? – HaoZeke Apr 8 '19 at 12:11
  • @HaoZeke: You can add additional calls within the (lambda () ...) body, or you can use several eval: lines. – Stefan Apr 8 '19 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.