0

I have a non-interactive function F to do post processing after each command. F is buffer locally hooked to post-command-hook.

Whenever F is hooked the prefix argument is eaten and the next command does not get it.

How can I preserve the prefix argument when hooking a function to post-command-hook ?

EDIT: There is no issue with the prefix argument and the function F being hooked as the selected answer shows.

My issue was that the function (let's call it A) that did not worked properly could be either called directly (which had a working behavior) or called from a push-button (in this case the prefix argument was not passed to A).

  • Can you set the universal argument inside the applicable function to a buffer-local variable value that can be picked up by the post-command-hook and then reset it to nil once that hook runs its course? – lawlist Dec 30 '14 at 22:10
  • I'm note sure it will help. More info on the use case: say I have an interactive command C. If I hook F to post-hook-commandthen C never sees the prefix argument. – syl20bnr Dec 30 '14 at 22:24
  • "Whenever F is hooked the prefix argument is eaten and the next command does not get it." What "next command"? Functions in the post-command-hook are ran after the actual command. – Malabarba Dec 30 '14 at 22:34
  • This isn't making much sense. Please post a self-contained code snippet that defines and sets this hook, tell us what series of action to perform (e.g “type C-u 3 a”), the behavior you observe, and the behavior you want to achieve instead. – Gilles Dec 30 '14 at 22:42
  • When I have a non interactive function F hooked in post-command-hook. Then pressing 1 RET (in this case the command is bound to RET) does not receive the prefix argument. There is not much to say than this. – syl20bnr Dec 30 '14 at 22:44
0

test-fn is run first, at which time the variable my-universal-arg is set (if there was a universal argument called before pressing the f5 key). Then, the post-command-hook gets triggered and it picks up the variable my-universal-arg (if that variable was set by calling a universal argument before pressing the f5key), and then the message plays displaying the argument and the variable my-universal-arg is then set back to nil.

NOTE:  If the variable my-universal-arg is made to be buffer-local, then the appropriate functions will need to either be occurring in the target buffer or something like with-current-buffer . . . would need to be used to pick up the variable bound to the corresponding buffer.

(defvar my-universal-arg nil)

(defun test-fn (arg)
(interactive "P")
  (if arg
    (setq my-universal-arg arg)
    (message "No `arg` was specified.")))

(define-key global-map [f5] 'test-fn)

(defun post-command-hook-fn ()
  (when my-universal-arg
    (message "This is `my-universal-arg`:  %s" my-universal-arg)
    (setq my-universal-arg nil)))

(add-hook 'post-command-hook 'post-command-hook-fn)
  • You example works. But the behavior I have is just non sense. – syl20bnr Dec 31 '14 at 2:27
  • My best guess is that the shelf-life of the current-prefix-arg is rather short in duration (i.e., the duration of the interactive command); whereas, you would prefer that the shelf-life extend through to the end of the post-command-hook running all functions attached to it. At some point the current-prefix-arg needs to go back to nil and it is probably happening before all of the functions attached to the post-command-hook finish running their course. – lawlist Dec 31 '14 at 2:49
  • I mark your answer as correct because it shows that the prefix argument is correctly handled with hooked functions in post-command-hook. Now my issue was that the interactive function executed was either called directly or called from a push-button! And the push-button does not pass the current prefix argument, it has to be passed explicitly with current-prefix-arg. I had no idea that push-button was used for something that did not require it. Sorry for the mess! – syl20bnr Dec 31 '14 at 3:03
  • Question updated to reflect the source of the confusion. – syl20bnr Dec 31 '14 at 3:08

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.