2

I have <f5> bound to projectile-run-project. When pressing <f5>, the minibuffer displays the command to run and you need to press RET to confirm it.

How can I add the behavior that when <f5> is pressed again, it acts as a RET to confirm and execute the command?

UPDATE: my working projectile config after the answer from @Tobias

(use-package projectile
  :init (setq
         projectile-project-search-path '("c:/repo") ;; todo make variable and declare it in paths
         projectile-enable-caching t
         projectile-completion-system 'helm
         projectile-indexing-method 'native)
         (add-hook 'ibuffer-hook
             (lambda ()
                 (ibuffer-projectile-set-filter-groups)
                 (unless (eq ibuffer-sorting-mode 'alphabetic)
                     (ibuffer-do-sort-by-alphabetic))))
  ;; projectile-globally-ignored-file-suffixes
  ;; (append '("S" "elf" "o") projectile-globally-ignored-file-suffixes))

  :bind-keymap
  ("C-c p" . projectile-command-map)

  :bind
  ("<f5>" . projectile-run-project)

  :config (projectile-mode +1))

(defun my-read-shell-command-key-wrapper (oldfun &rest args)
  "Wrapper for `projectile-run-project' that temporarily adds <f5> to the keymap."
  (let ((minibuffer-local-shell-command-map (copy-sequence minibuffer-local-shell-command-map)))
    (define-key minibuffer-local-shell-command-map (kbd "<f5>") (kbd "RET"))
    (apply oldfun args)))

(advice-add 'projectile-run-project :around #'my-read-shell-command-key-wrapper)
  • 2
    From the description of the elisp tag: "DO NOT USE it for questions about customization or how to write an application." – Tobias Mar 6 at 11:28
2

The working horse of projectile-run-project is projectile--run-project-cmd which uses indirectly read-shell-command.

I don't have projectile but I demonstrate here how you can implement the binding of f5 with a simple example that works for me in Emacs 26.3. You can just replace example-shell-command in the advice-add with projectile-run-project. Naturally, in your usecase you do not need the definition of example-shell-command anymore.

(defun my-read-shell-command-key-wrapper (oldfun &rest args)
  "Wrapper for `projectile-run-project' that temporarily adds <f5> to the keymap."
  (let ((minibuffer-local-shell-command-map (copy-sequence minibuffer-local-shell-command-map)))
    (define-key minibuffer-local-shell-command-map (kbd "<f5>") (kbd "RET"))
    (apply oldfun args)))

(defun example-shell-command ()
  "Example: Read and call shell command."
  (interactive)
  (shell-command (read-shell-command "Shell command:")))

(advice-add 'example-shell-command :around #'my-read-shell-command-key-wrapper)
| improve this answer | |
  • Could it be you made a little mistake somewhere? Pressing <f5> the first time throws me a message: let: Symbol’s function definition is void: copy-list I've updated OP with my config for projectile. Apperently I'm running 26.2. But can update to 26.3 if needed (waiting for stable 27). – Swedgin Mar 6 at 12:34
  • changing copy-list to copy-sequence did the trick. Thanks for your example! EDIT: apperently cl-copy-list also works – Swedgin Mar 6 at 12:44
  • 1
    @Swedgin I am sorry about that . That was the old-fashioned copy-list from cl.el. cl-copy-list from cl-lib.el would be better. But, the C-implemented copy-sequence also works very fine here. So I've replaced copy-list with copy-sequence. Thanks for the notice. – Tobias Mar 6 at 12:53

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.