jerrypnz / major-mode-hydra.el

Spacemacs-esque major mode leader key powered by Hydra
297 stars 9 forks source link

Checking duplicate letter #44

Open Sasanidas opened 3 years ago

Sasanidas commented 3 years ago

Sometimes when I have like a big hydra, I mess up the configuration and duplicate some symbols.

This can be easily solve with a simple check of the major-mode/MODE/heads, but I think it may be interesting to implement it into the macro major-mode-hydra-define (or somewhere else), I can develop that feature if you think is useful for the package.

Thank you.

jerrypnz commented 3 years ago

Sorry for the late response. This would be a really useful feature and a PR is more than welcome! Have you thought about how it's gonna work? It simply fails when there are duplicate heads?

Sasanidas commented 3 years ago

Sorry for the late response. This would be a really useful feature and a PR is more than welcome! Have you thought about how it's gonna work? It simply fails when there are duplicate heads?

Yes, I implement a simple example in the function major-mode-hydra-generate

(defun major-mode-hydra--generate (mode body heads-plist &optional overwrite-p) "Generate a major mode hydra for given MODE with given BODY and HEADS-PLIST. Overwrite existing hydra if OVERWRITE-P is t, otherwise add new heads to it." (let* ((hydra-name (major-mode-hydra--name-for mode)) (title (when (functionp major-mode-hydra-title-generator) (funcall major-mode-hydra-title-generator mode))) (letter-sequence (seq-mapcat (lambda (x) (seq-map #'car x)) (seq-filter #'listp heads-plist))) (body (-> body (lax-plist-put :hint nil) (major-mode-hydra--put-if-absent :color 'teal) (major-mode-hydra--put-if-absent :title title) (major-mode-hydra--put-if-absent :separator major-mode-hydra-separator) (major-mode-hydra--put-if-absent :quit-key major-mode-hydra-invisible-quit-key))) (df (if overwrite-p 'pretty-hydra-define 'pretty-hydra-define+))) (if (equal (-distinct letter-sequence) letter-sequence) `(,df ,hydra-name ,body ,heads-plist) (error "There are letters duplicated in the hydra"))))

It extracts the letters from the heads-plist variable and check if they are duplicates. This seems to work, but there is probably a faster way to get the letters.

What do you think?

Regards

jerrypnz commented 3 years ago

Hi @Sasanidas,

Thanks for posting your code snippet. Yes I think it should work. I do have a couple of suggestions though:

  1. Perhaps you can implement this check in pretty-hydra-generate so that it's more generic. You'll find that it's also easier to get all the keys as there is a heads binding which you can extract keys from.
  2. Could you stick to -map and -filter functions provided by dash.el to keep it consistent? That's the library I use for sequence manipulation
  3. Would be awesome if the error message mentions the duplicate key(s)

If you're keen, feel free to submit a PR!