Monday, February 2, 2009

e-blog: Blogging from Emacs

This is sent from emacs using e-blog.

Friday, November 28, 2008

Emacs: Go to first Python import statement

I was looking at PerlySense briefly because I occasionally have to do some Perl at work, along w/ Python, shell, Java & C++. The latter 2 are fairly infrequent, but still, Emacs makes such a heterogeneous work place alot easier. PerlySense has a keyboard shortcut that will take you to the "use module" section of a script/module. I wanted to do something similar in Python that would take you to the first "import module" section in a Python script/module. I love the dynamic programming environment that Emacs provides. I tested out the following in a *scratch* buffer, created a local key mapping and viola. Just save it in your prefered Emacs init environment and you are done.


(defun back-to-indentation-or-beginning ()
(interactive)
(if (= (point)
(progn
(back-to-indentation)
(point)))
(beginning-of-line)))

(defun killer/python-import-beginning ()
"Go to the first import statement at the beginning of the buffer.
TODO: Skip any triple-quoted sections at the start of the script/module."
(interactive)
(widen) ; turn off narrowing, just in case.
(goto-char (point-min))
(if (re-search-forward "^import" nil t)
(progn
(goto-char (point))
(back-to-indentation-or-beginning))
(message "No python import statement")))

(define-key py-mode-map (kbd "C-; in") 'killer/python-import-beginning)