cmpitg / wand

Execute actions based on text patterns, for Emacs
GNU General Public License v3.0
87 stars 4 forks source link

Wand

Wand is an extension that allows users perform actions on a region based on predefined patterns. Wand is inspired by Xiki[1] and Acme[2].

Requirements

Installation

Thanks to @yasuyk Wand is available in MELPA. Installation process is now as simple as M-x package-install RET wand RET.

If you don't have MELPA setup, the following migh suffice:

(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
(package-initialize)
(package-install 'wand)
(require 'wand)

If you use Emacs 26+, it's recommend to use Wand with use-package:

(use-package wand
 :ensure t
 :config (...))

Usage

Wand works by going through a list of rules, stored in the wand:*rules* variable (which should be customized to be useful). Each rule has the meaning of "if satisfied, perform an action; otherwise pass to the next rule" and is a cons of (rule-check-fn . action-fn):

After setting the rules, we simply call wand:execute with the corresponding string.

Manually creating all the rules from ground up is possible but usually tedious. Hence, Wand provides the wand:create-rule helper to facilitate the creation of a rule. wand:create-rule takes several arguments describing the process of matching and extracting a string and the action performed upon. Essentially, wand:create-rule cares about the following questions:

Here is an example of how it would look like in practice:

(setq wand:*rules*
      (list (wand:create-rule :match "\\$ "
                              :capture :after
                              :action #'popup-shell-command)
            (wand:create-rule :match "https?://"
                              :capture :whole
                              :action #'open-url-in-firefox)
            (wand:create-rule :match "file:"
                              :capture :after
                              :action #'find-file)
            (wand:create-rule :match "#> "
                              :capture :after
                              :action #'(lambda (string)
                                          (eval (read string))))))

When calling wand:execute <a-string>, the following would happen:

The string could span through multiple lines. If skip-comment is t, comments are stripped from all the lines.

It's recommended to bind wand:execute to a key stroke and/or mouse for quick command execution.

(global-set-key (kbd "<C-return>")       'wand:execute)
(global-set-key (kbd "<C-mouse-1>")      'wand:execute)
(global-set-key (kbd "<C-down-mouse-1>")  nil)

Thanks

Special thanks to:

License

This project along with its source code and all materials are released under the terms of the GNU General Public License 3.0 (GPLv3). See COPYING for more details.

Copyright (C) 2014-2019 Ha-Duong Nguyen