emacs alphabetize methods

J

John Hunter

Using emacs python-mode with a class

class C:
def method1(self):
pass

def anothermethod(self):
pass

def method2(self):
pass

is there any way to select all the methods and have emacs order them
alphabetically? If not, is there a good too to do so?

Thanks,
John Hunter
 
P

Peter Milliken

Not that I am aware of - but this wouldn't be an impossible job to tackle
yourself and provide the results for benefit of the rest of us who uses
Emacs :)

My suggestion for how to do this (off the top of my head :)) is to write a
support (Python) program to call from your emacs using Pymacs
(http://pymacs.progiciels-bpi.ca/manual/The--Pymacs--buffer.html).

Then you could probably locate the points where your methods start and
finish using services available from the Python Language Services section in
the Python Library Reference (Chapter 18), re-arrange the source code and
then re-insert it back into your Python buffer.

Probably not a very big job at all - if you don't do it then I might well do
it myself one day (but not in the foreseeable future due to other work loads
:)).

Regards,
Peter
 
S

Skip Montanaro

John> Using emacs python-mode with a class
John>
John> class C:
John> def method1(self):
John> pass
John>
John> def anothermethod(self):
John> pass
John>
John> def method2(self):
John> pass
John>
John> is there any way to select all the methods and have emacs order
John> them alphabetically? If not, is there a good too to do so?

Sort of, but there are problems. Emacs does have a sort-paragraphs
function. Python-mode has a py-mark-block command. If you place the
cursor at the start of the "class" line and hit C-c C-k you'll mark the
class. Hit C-n C-x C-x to highlight the block except for the class line
itself then M-x sort-paragraphs RET.

This has a gotcha. The notion of paragraph separator is basically a blank
line. To make this more effective you'd have to modify the
paragraph-separate variable. If you come up with a reasonable value for
Python code, send it along to (e-mail address removed) and we'll update
python-mode.el.

Skip
 
?

=?iso-8859-1?q?Johan_Bockg=E5rd?=

Peter Milliken said:
My suggestion for how to do this (off the top of my head :)) is to write a
support (Python) program to call from your emacs using Pymacs
(http://pymacs.progiciels-bpi.ca/manual/The--Pymacs--buffer.html).

You can just call the program directly and use stdin/stdout:

(defun my-alphabize-region (beg end)
"Sort methods in region alphabetically."
(interactive "r")
;; run command with region as input, and replace it with the output
(shell-command-on-region
beg end
"myCommand" ;<<< Your command here
t t "*errors*"))


(You could do this entirely inside emacs of course.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top