summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2014-04-21 17:24:56 +0200
committerGravatar Tom Willemse2014-04-21 17:24:56 +0200
commit727f7f5e929f6b520b9c2b45670f6ca9b9c26ff7 (patch)
treebcae6b36ff5e8551be1fa98e755fcc778b333f57
parente071edabe36e0a11fd6ef243624060e52a51400f (diff)
downloademacs-727f7f5e929f6b520b9c2b45670f6ca9b9c26ff7.tar.gz
emacs-727f7f5e929f6b520b9c2b45670f6ca9b9c26ff7.zip
Show python package name in mode-line
-rw-r--r--.emacs.d/init.org62
1 files changed, 62 insertions, 0 deletions
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