abo-abo / hydra

make Emacs bindings that stick around
1.84k stars 112 forks source link

Confusing about the remapping rule in hydra #293

Open BooAA opened 6 years ago

BooAA commented 6 years ago

Generally, I have "E" bind to eval-last-sexp so that I can quickly eval elisp code.


(defhydra hydra-vi (:color pink :hint nil)
  "hydra-vi"
  ("E" eval-last-sexp)
  ("q" nil))

But when editing scheme code, I want "E" to be remapped by another function customized for scheme-mode. Here's how I do it.

(require 'cmuscheme)

(defun BooAA/get-scheme-proc-create ()
  "Create one scheme process if no one is created."
  (unless (and scheme-buffer
               (get-buffer scheme-buffer)
               (comint-check-proc scheme-buffer))
    (save-window-excursion
      (run-scheme scheme-program-name))))

(defun BooAA/scheme-send-last-sexp ()
  "A replacement of original `scheme-send-last-sexp':
1. check if scheme process exists, otherwise create one
2. make sure the frame is splitted into two windows, current one is the scheme
   source code window, the other one is the scheme process window
3. run `scheme-send-last-sexp'"

  (interactive)
  (BooAA/get-scheme-proc-create)
  (cond ((= 2 (count-windows))
         (other-window 1)
         (unless (string= (buffer-name)
                          scheme-buffer)
           (switch-to-buffer scheme-buffer))
         (other-window 1))
        (t
         (delete-other-windows)
         (split-window-vertically (floor (* 0.68 (window-height))))
         (other-window 1)
         (switch-to-buffer scheme-buffer)
         (other-window 1)))
  (scheme-send-last-sexp))

And that I saw this post #175 , so I think I must remap hydra-vi/eval-last-sexp

(define-key scheme-mode-map [remap hydra-vi/eval-last-sexp] 'BooAA/scheme-send-last-sexp)

but not

(define-key scheme-mode-map [remap eval-last-sexp] 'BooAA/scheme-send-last-sexp)

However , the weird thing is that the first one doesn't work but the second one does.

The first one didn't split the window and only open the REPL without eval my code. And I was in the buffer with REPL opening.

The second one splits window and eval the code. And I was stayed at the origin buffer.

The reason I know whether the code is evaluated is by making the expression before cursor with some bugs and the first one doesn't report but the second one does.

I haved tried directly M-x and call BooAA/scheme-send-last-sexp , the result is correct. So I think my customized function should work independently.

I doubt that the last line of BooAA/scheme-send-last-sexp (e.g. scheme-send-last-sexp) was not evaluated, and maybe the function stuck after running BooAA/get-scheme-proc-create so that it doesn't split window nor eval scheme code as well.

abo-abo commented 6 years ago

This is expected, we are simply following the usual keymap rules here: [remap eval-last-sexp] doesn't work since eval-last-sexp isn't bound anywhere in the keymap at that point, but hydra-vi/eval-last-sexp is, that's why [remap hydra-vi/eval-last-sexp] works.

BooAA commented 6 years ago

Oh..., sorry for my poor English expression.

I mean that [remap eval-last-sexp] works but [remap hydra-vi/eval-last-sexp] doesn't work, which confusing me.

[remap hydra-vi/eval-last-sexp] doesn't split window and eval the expression, which is very strange.