An Emacs package which lets you evaluate (run) Elisp code using the right mouse button, inspired by the “Execute text” command in Rob Pike’s Acme editor.
eval-region-or-word
is the function that does the evaluation.
By default, it’s bound to <mouse-3>
, which corresponds to the right mouse button in Emacs, as opposed to <mouse-2>
(middle mouse) like it is in Acme because… I wanted it like that?
To be fair, nowadays, more computers have only <mouse-1>
and <mouse-3>
, for example laptops, which makes all the sense. I suppose three-button mice were more common back then.
If you want, you can bind this function to a custom keyboard shortcut.
For example, to bind it to the key combination C-c e
, you can add the following code to your init.el:
(global-set-key (kbd "C-c e") 'eval-region-or-word)
With this, in addition to the right mouse button, you can now use a keyboard shortcut to use the function.
Just keep in mind acme-mouse-3 was designed specifically for the mouse, and might break if the mouse isn’t in an Emacs pane.
- First of all, we define
eval-region-or-word
.
(defun eval-region-or-word ()
"Evaluate the current region or word under the mouse cursor."
(interactive)
- Next, we create a let-binding and define
region
, whose value depends on whether or not an active region (selection) is present. - A let-binding, in the context of Elisp, allows you to create temporary variables with specific values within a local block of code.
- If there’s an active region, we set the
region
variable to that.
(let ((region (if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
- Otherwise, we set the text cursor to where the mouse pointer is and temporarily modify the syntax table to include hyphens as part of words.
- After that,
region
is set to the word the text cursor was moved to/is under, effectively settingregion
to the word under the mouse pointer.
(progn
(mouse-set-point last-nonmenu-event)
(with-syntax-table (copy-syntax-table (syntax-table))
(modify-syntax-entry ?- "w")
(thing-at-point 'word))))))
- After
region
is set, we check if(
and)
are the prefix and suffix ofregion
respectively. - If not, we add that, so that it’s a legitimate Lisp expression.
- Finally, we read the value of
region
and evaluate it, which ends the function.
(when region
(unless (string-prefix-p "(" region)
(setq region (concat "(" region)))
(unless (string-suffix-p ")" region)
(setq region (concat region ")")))
(eval (read region))
)
Okay, I don’t quite know how to say this tactfully, but it’s possible that I slightly exaggerated the useful nature of the package.
Is it a very, very neat package?
To be certain it is! It’s an extremely neat package!
I don’t want anyone to say that it isn’t an astonishingly neat package - it is!
Is it useful?
Well that sort of depends on your definition of usefulness.
From one perspective the useful is merely philosophical in nature. It’s more of a-
Okay, well good for you, you found the more popular and useful Acme Emacs mouse package.
You found me out, user.
I’m a liar and a cheat and you’re so clever.
Look, I think the issue here is just that you’re unusually fascinated by tried and tested packages.
What normal person actually wants to use a program entirely for its usefulness?
I figured the package was as useful as anyone would actually need!
Don’t you put this on me! Maybe you’re the problem!
sigh Look… things got a little heated there.
I think we both said some things we didn’t mean.
Why don’t we just put all this behind us and agree to just call the package mostly-useful.
If that works for you, then go ahead and download the package and add it to your init.el, and we can move on.
I’ll just be over here when you are ready.
This package is licensed under the GNU General Public License v3 or later.
Free Software, hell yeah!