Last active
August 29, 2015 14:03
-
-
Save vermiculus/64110ca96f09a0d5f306 to your computer and use it in GitHub Desktop.
CJump: Jump to or search for a corresponding file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun get-word-at-point () | |
"Get the word at point." | |
(interactive) | |
(let (pos1 pos2 meat) | |
(if (and transient-mark-mode mark-active) | |
(setq pos1 (region-beginning) | |
pos2 (region-end)) | |
(setq pos1 (car (bounds-of-thing-at-point 'symbol)) | |
pos2 (cdr (bounds-of-thing-at-point 'symbol)))) | |
(buffer-substring-no-properties pos1 pos2))) | |
(defun c-jump (&optional use-thing-at-point prompt-for-extensions) | |
"Jumps to the corresponding file of the current one. If | |
visiting an implementation file, find its declaration file. If | |
visiting a declaration, find its implementation. | |
With a prefix argument, use the thing at point as the base file | |
name. | |
If the file does not exist in the current directory, ask for a | |
base directory and call \\[find-dired] with that directory and | |
the computed filename." | |
(interactive "P") | |
(when use-thing-at-point | |
(if (= (car use-thing-at-point) 16) | |
(setq prompt-for-extensions t)) | |
(setq use-thing-at-point t)) | |
(let* ((decl-ext (simple-get-minibuf prompt-for-extensions "Declaration extension" "hh")) | |
(impl-ext (simple-get-minibuf prompt-for-extensions "Implementation extension" "cc")) | |
(my-ext (file-name-extension (buffer-file-name))) | |
(my-cls (file-name-sans-extension (buffer-file-name))) | |
(base-ext (if use-thing-at-point decl-ext (if (string-equal my-ext decl-ext) impl-ext decl-ext))) | |
(base-cls (if use-thing-at-point (get-word-at-point) my-cls)) | |
(full-file (concat base-cls "." base-ext))) | |
(if (file-exists-p full-file) | |
(find-file full-file) | |
(if (y-or-n-p (format "File \"%s\" does not exist in the current directory! Would you like to find it?" full-file)) | |
(find-name-dired (let ((response (read-file-name "Base directory: " default-directory "" t nil (function file-directory-p)))) | |
(if (string-equal response (buffer-file-name)) "" response)) | |
full-file))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any easier way to do L25?