summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franckcuny@gmail.com>2016-02-19 13:57:40 -0800
committerFranck Cuny <franckcuny@gmail.com>2016-02-19 13:57:40 -0800
commit860ca7a7a262feeef05658440ad299281981fcbc (patch)
tree6faa3bc3182aac8a9a26338f0b0dd21f36361456
parent[emacs] Fix regexp to set python-mode for aurora (diff)
downloademacs.d-860ca7a7a262feeef05658440ad299281981fcbc.tar.gz
[emacs] Add a function to jump to a BUILD file.
When working on code at Twitter, I regularly need to jump to the
associated BUILD file. The function `fcuny/jump-to-build-file` open the
related BUILD file if it exists in a new buffer.

Bind this function to "C-c t" for now.
Diffstat (limited to '')
-rw-r--r--emacs.d/core/core-bindings.el2
-rw-r--r--emacs.d/core/core-defun.el29
2 files changed, 31 insertions, 0 deletions
diff --git a/emacs.d/core/core-bindings.el b/emacs.d/core/core-bindings.el
index 377b89c..a9d1b4d 100644
--- a/emacs.d/core/core-bindings.el
+++ b/emacs.d/core/core-bindings.el
@@ -8,4 +8,6 @@
 
 (global-set-key (kbd "s-N") 'fcuny/switch-to-scratch)
 
+(global-set-key (kbd "C-c t") 'fcuny/jump-to-build-file)
+
 (provide 'core-bindings)
diff --git a/emacs.d/core/core-defun.el b/emacs.d/core/core-defun.el
index 5180e68..be7899f 100644
--- a/emacs.d/core/core-defun.el
+++ b/emacs.d/core/core-defun.el
@@ -39,4 +39,33 @@
         (ansi-term (getenv "SHELL")))
     (switch-to-buffer-other-window "*ansi-term*")))
 
+;; jump to the BUILD file
+(defvar fcuny/build-file "BUILD"
+  "Name of the file containing our build targets")
+
+(defun fcuny/find-root-directory-for-build-file (file)
+  "Find the root."
+  (let ((root nil)
+        try)
+    (while (not (or root
+                    (null file)
+                    (string-match locate-dominating-stop-dir-regexp file)))
+      (setq try (if (stringp fcuny/build-file)
+                    (file-exists-p (expand-file-name fcuny/build-file file))
+                  (message "found file exists -> %s" file)))
+      (cond (try (setq root file))
+            ((equal file (setq file (file-name-directory
+                                     (directory-file-name file))))
+             (setq file nil))))
+    (and root (expand-file-name (file-name-as-directory root)))))
+
+(defun fcuny/jump-to-build-file ()
+  "Open the BUILD file in a buffer if it exists."
+  (interactive)
+  (let ((build-file (fcuny/find-root-directory-for-build-file (file-name-directory (buffer-file-name)))))
+    (message "found a file %s" build-file)
+    (if build-file
+        (find-file (concat build-file fcuny/build-file))
+      (error "Could not find %s" fcuny/build-file))))
+
 (provide 'core-defun)