enricoflor / latex-table-wizard

Magic editing of LaTeX tables in GNU Emacs
GNU General Public License v3.0
19 stars 1 forks source link

How to add customized key bindings #2

Open fountainer opened 1 year ago

fountainer commented 1 year ago

Current key binding for alignment is not convenient. Call latex-table-wizard in Table 1 and press TAB, then the table is left aligned. Quit latex table wizard and repeat the above procedure on Table 2, then Table 2 is centered aligned. I noticed that we can cycle the types of alignment by repeatedly pressing TAB. However, I think it's better if the type of alignment is always left alignment when latex-table-wizard is called again in another table (can be same table as long as latx-table-wizard is called again). My workaround is adding an advice.

(advice-add #'latex-table-wizard
            :before
            (lambda () (setq latex-table-wizard--align-status '(left center right compress))))

In addition, I want to add a group of key bindings for alignments, e.g, left align (l l), centered align (l c), right align (l r). I tried to add some things to the variable 'latex-table-wizard-transient-keys, but it doesn't work.

(add-to-list 'latex-table-wizard-transient-keys (cons 'latex-table-wizard-align-left "l l"))
(add-to-list 'latex-table-wizard-transient-keys (cons 'latex-table-wizard-align-right "l r"))
enricoflor commented 1 year ago

Hey, I'm sorry this completely slipped through the cracks. I just committed a fix for the alignment cycle behavior, I agree with you that this is a much saner behavior now. Instead of using advices, I thought of just resetting the value of the list latex-table-wizard--align-status every time the function is called "for the first time". Now this resets to left alignment even if you stay in the same table, as long as the previous command you used was not an alignment command.

As for the second part of your question, the issue is that transient does not allow the prefixes to be dynamically changed this way. The whole thing with latex-table-wizard-transient-keys was the best I could come up with to let the user easily change the keybindings (and that's already not an optimal solution), but if you want to change the prefix itself (that is, what commands are made available by the transient prefix and in what order), you are better off writing your own transient prefix. This is not hard, you can just copy the standard one from the source of the package and modify it as you see fit. Otherwise, I have tried to make all the interactive commands work as intended without the transient infrastructure. One "lightweight" alternative, if you use emacs 28 or later, is presented at the end of the readme (and info page) and relies on repeat:

(require 'repeat)

(define-prefix-command 'latex-table-wizard-map)

(define-key global-map (kbd "C-c C-|") 'latex-table-wizard-map)

(dolist (c latex-table-wizard-transient-keys)
  (define-key latex-table-wizard-map (kbd (cdr c)) (car c)))

(dolist (cmd (mapcar #'car latex-table-wizard-transient-keys))
  (put cmd 'repeat-map 'latex-table-wizard-map))

This way, you just define a map like you do anywhere else in emacs, you use repeat so that you don't have to press the prefix key every time, and if you use which-key you will get suggestions as to which latex-table-wizard commands are available. A third alternative which I have never explored is to write your own hydra with the subset of commands that you use most often.