From 860ca7a7a262feeef05658440ad299281981fcbc Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Fri, 19 Feb 2016 13:57:40 -0800 Subject: [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. --- emacs.d/core/core-bindings.el | 2 ++ emacs.d/core/core-defun.el | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'emacs.d/core') 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) -- cgit 1.4.1