[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: naming convention based file toggling facility
From: |
Masatake YAMATO |
Subject: |
Re: naming convention based file toggling facility |
Date: |
Thu, 25 Sep 2003 16:20:42 +0900 (JST) |
> I've had a hackish little elisp function for years that always struck me
> as a slightly surprising omission from emacs: the ability to toggle
> between files based on a naming convention like: <source>.c and
> <source>.h.
M-x ff-find-other-file may be useful.
I have another implementation.
Masatake YAMATO
;; c-alt.el --- Find header file or implementation file
;; Copyright (C) 2000 Masatake YAMATO
;; Author Masatake YAMATO <address@hidden>
;; Created: Tue Jun 4 17:00:22 1996
;; Time-stamp: <00/10/12 20:30:48 masata-y>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;; Commentary:
;;
;; Put .emacs
;;
;; (add-hook 'c-mode-hook (function (lambda ()
;; (define-key c-mode-map "\C-c\C-v"
'c-find-alternative-file))))
;; (autoload 'c-find-alternative-file "c-alt" "Find .c or .h file" t)
;; Histroy
;; * Thu Oct 12 20:06:36 2000
;; Added c++ supports
(defun c-find-alternative-file (arg)
"TODO"
(interactive "P")
(let* ((fname (buffer-file-name))
(basename (file-name-sans-extension fname))
(afname nil)
(baselen (length basename))
(ext (substring fname (1+ baselen))))
(cond
((or (string= ext "c") (string= ext "m") (string= ext "cpp"))
(setq afname (concat basename ".h"))
(if (file-exists-p afname)
(find-file afname)
(if arg
(find-file afname)
(if (y-or-n-p (format "Cannot find! Create %s?: " afname))
(find-file afname)
))
))
((string= ext "h")
(if (or
(progn (setq afname (concat basename ".m")) (file-exists-p afname))
(progn (setq afname (concat basename ".c")) (file-exists-p afname))
(progn (setq afname (concat basename ".cpp")) (file-exists-p
afname)))
(find-file afname)
(let ((newext (call-interactively 'c-get-alternative-file-name)))
(setq afname (concat basename (cond
((eq newext ?+)
".cpp")
((eq newext ?c)
".c")
((eq newext ?m)
".m")
((eq newext ?n)
"")
((eq newext ?y)
".c")
(t
"")
)))
(if (not (eq newext ?n))
(if (not (eq baselen (length afname)))
(find-file afname)
(error "Wrong extension: %c" newext))))))
(t
(error "No alternative file for: %c" fname)))))
(defun c-get-alternative-file-name (ext)
(interactive "cCannot find! Create?(type n(No) c(C), m(ObjC) or +(C++)): ")
ext)
(provide 'c-alt)
;; c-alt.el ends here.