From 727f7f5e929f6b520b9c2b45670f6ca9b9c26ff7 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 21 Apr 2014 17:24:56 +0200 Subject: Show python package name in mode-line --- .emacs.d/init.org | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/.emacs.d/init.org b/.emacs.d/init.org index 8421150..41360bd 100644 --- a/.emacs.d/init.org +++ b/.emacs.d/init.org @@ -683,6 +683,68 @@ (setq elnode-do-init nil) #+END_SRC +* Show python package name instead of file name + + When working with python, knowing which package I'm in usually tells + me more about what I'm doing than the file name, especially when + working with django where almost every app will have a ~tests.py~ and + a ~models.py~. Of course =uniquify= fixes this pretty well too, though + in this case, it's less cool (imo). + + First we define two functions that help us determine the package + name of the current file and the parent package name of the current + file. Finally we define a third function which determines the full + package name of the current buffer. + + #+BEGIN_SRC emacs-lisp + (defun oni:python--get-current-module-name () + "Get the name of the current python module. + + This is very simply just the filename of the current buffer with + the extension and pyath removed." + (file-name-sans-extension + (file-name-nondirectory (buffer-file-name)))) + + (defun oni:python--get-parent-module-name (&optional dir) + "This gets the currend file's parent module. + + This function recursively gathers the parent package name of + either DIR or the current buffer's file name. Any directory where + an `__init__.py' file is found is considered to be a package. + + This function returns either the parent package, with its + parents, or nil if the current directory isn't a python + package.." + (let* ((base (directory-file-name + (file-name-directory (or dir (buffer-file-name))))) + (package (file-name-nondirectory base))) + (if (file-exists-p (concat base "/__init__.py")) + (let ((parent (oni:python--get-parent-module-name base))) + (if parent + (concat parent "." package) + package)) + nil))) + + (defun oni:python-package-name () + (let ((current-module (oni:python--get-current-module-name))) + (if (file-exists-p "__init__.py") + (concat (oni:python--get-parent-module-name) + "." current-module) + current-module))) + #+END_SRC + + After all this we make Emacs show the package name rather than the + file name in the mode-line. + + #+BEGIN_SRC emacs-lisp + (defun oni:python-package-buffer-identification () + "Have `mode-line-buffer-identification' show the python package name." + (setq mode-line-buffer-identification + '(:eval (oni:python-package-name)))) + + (add-hook 'python-mode-hook #'oni:python-package-buffer-identification) + #+END_SRC + * Load custom file I don't really use the Emacs customization interface much, but I -- cgit v1.2.3-54-g00ecf