36

jjzmajic / handle · GitLab

 4 years ago
source link: https://gitlab.com/jjzmajic/handle
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A handle for major-mode generic functions.

Value Proposition

handle provides generic functions that specialize on major modes and intended purpose instead of arguments. Similar functionality is needed across programming languages such as:

  • evaluating expressions
  • starting repls
  • finding documentation
  • going to definition
  • formatting code
  • compiling code
  • listing errors

But this functionality is performed by different callables across major modes and often by multiple callables in the same one. handle groups them together so that they can be bound to a single global kbd, selected based on major mode, and run in sequence until one succeeds.

Usage

handle generates handle-* functions based on (user-specified) handle-keywords that can then be bound to a single global kbd and used in any major mode.

So by running this:

(handle '(python-mode inferior-python-mode)
          :repls #'run-python
          :formatters #'lsp-format-buffer
          :docs '(lsp-describe-thing-at-point
                  python-help-for-region-or-symbol
                  python-eldoc-for-region-or-symbol
                  python-shell-print-region-or-symbol))

And then calling handle-docs in python mode, handle will run each of the functions specified under :docs in order untill one succeeds. If a function errors out (or returns nil, see handle-nice-functions) it will calmly proceed to the next one. The same function will behave differently in other modes.

handle is also aware of the Emacs mode hierarchy. If it cannot handle the task you gave it in a specific mode, it will attempt to do so with the instructions you gave for the parent mode. This also means that you can set default/global handlers by targeting modes like prog-mode and text-mode.

This package is designed to be as unopinionated as possible. I strongly recommend setting your own handle-keywords before loading the package. The appropriate handle-* functions will then be generated, and the handle function itself will be able to interpret all of them.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK