POC for python code manipulation with a Full Syntax Tree. That’s better than regexps.
RedBaron: https://redbaron.readthedocs.org/en/latest/tuto.html
This library is based on RedBaron:
pip install redbaron
You need hydra.el (in Melpa, that’s optionnal but real handy) and
helm (for helm-imenu).
For the moment, clone this repository and load red4emacs.el. Doing
so will set the path to the python code that lives alongside it (see
red4emacs-path).
The one command to know to see all options:
M-x red4e-hydra/body
Another example, renaming an argument and its occurences inside the method body:
Inside a function, call M-x red4e-add-arg RET whatever RET. It
replaces the method definition with a new one.
It respects args on many lines (well it isn’t that clever, it’s all in one line or each on its line).
There is a shortcut to add those two special args quickly: * on the
hydra and otherwise M-x red4e-add-args-kwargs.
M-x red4e-rename-arg
Renaming an argument will ask you to search and replace it inside the method body.
The actions for the current method are grouped under the hydra
red4e-hydra-method/body, accessible from the main hydra.
We can rename the current methode, just once or in the current project (using projectile), and kill, comment or copy it.
M-x red4e-rename-method
or
M-x red4e-rename-method-in-project
The second choice will ask you to search and replace this method in
the current project (based on projectile’s projectile-replace).
M-x red4e-args-multi-line-toggle
or l on the hydra (“lines”).
Note: see also multi-line and fillcode.
The hydra has the i binding to call imenu, so you can jump to
another def and carry on using red4e.
As an imenu frontend, you can stick with the default and very basic
prompt (completing-read), or choose between helm and ivy:
(setq red4e-selection-method 'red4e-selection-ivy) (setq red4e-selection-method 'red4e-selection-helm)
S in the hydra. It’s a simple search and replace inside the method body.
You can add a decorator to the current method:
M-x red4e-decorator-add
Or @ with the hydra.
It will suggest a list of decorators used in the current buffer. As
usual, you can bypass the ido prompt with C-f and type another
one. In that case, you can omit the “@”.
If we choose “@staticmethod”, then it removes “self” from the method signature.
This function doesn’t use the redbaron python library.
Thus the remove (D with the hydra) only works with one-line decorators.
take the string def foo(first, **kwargs, second=foo): pass, where
the arguments are in a bad order, and sort them:
def foo(first, second=foo, **kwargs): pass
Call that from within emacs, use with my snippet to add an argument to
a function, and it becomes a bit simpler (no more movements around the
code) and smarter (we manipulate code, not text with clunky regexps)
to add an argument to a method.
- emacs-traad for real refactoring (using Rope) and straightforward installation.

