From 4a62144606f358864203aca4901e2b153b32ebf1 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Thu, 7 Nov 2013 17:20:18 +0100 Subject: Add sort-imports command --- .emacs.d/init.org | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to '.emacs.d') diff --git a/.emacs.d/init.org b/.emacs.d/init.org index 368ae0e..ef4b381 100644 --- a/.emacs.d/init.org +++ b/.emacs.d/init.org @@ -276,3 +276,33 @@ :size 12.4 :weight 'normal)) #+END_SRC + +* A function to sort python includes + + At work I use Python. Our project is getting bigger and bigger, + import statements are growing. We use the following style: + + #+BEGIN_SRC python :tangle no + from somemodule import ( + Class1, + Class2, + Class3, + ) + #+END_SRC + + Which we found to be one of the better-looking ways to handle + multi-line import statements. To sort these lines I wrote a very + simplistic (and error-prone, but I'll handle it when I get to it) + command. + + All it does is save the current point, searches backwards for a ~(~, + then forwards for a ~)~ and sorts the region in between (not + including) the ~()~. + + #+BEGIN_SRC emacs-lisp + (defun sort-imports () + (interactive) + (save-excursion + (sort-lines nil (1+ (search-backward "(")) + (1- (search-forward ")"))))) + #+END_SRC -- cgit v1.2.3-54-g00ecf