help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need help writing file-visiting macro


From: rgb
Subject: Re: Need help writing file-visiting macro
Date: 26 Jul 2005 15:49:10 -0700
User-agent: G2/0.2

drkm wrote:
> rgb writes:
>
> > (defun my-open-complementary-file ()
> >   "If buffer-file-name ends in .h open .mdl and vise versa."
> >   (interactive)
> >   (if buffer-file-name
> >      (if (string-match "\\(\\`.*\\.\\)mdl\\'" (buffer-file-name))
> >          (find-file (concat (match-string 1 (buffer-file-name))"h"))
> >        (if (string-match ".*\\.h\\'" (buffer-file-name))
> >            (find-file (concat (match-string 1
> > (buffer-file-name))"mdl"))
> >          (message "Buffer's filename doesn't end in .mdl or .h")))
> >     (message "Buffer is not visiting a file")))
>
>   Or more precisely like this (to use '_Impl.c' instead of '.h'):
>
>     (defun my-open-complementary-file ()
>       "If buffer-file-name ends in _Impl.c open .mdl and vise versa."
>       (interactive)
>       (let ((buf    (buffer-file-name))
>             (mdl-re "\\`\\(.+\\)\\.mdl\\'")
>             (c-re   "\\`\\(.+\\)_Impl\\.c\\'"))
>         (if buf
>             (if (string-match mdl-re buf)
>                 (find-file (concat (match-string 1 buf) "_Impl.c"))
>               (if (string-match c-re buf)
>                   (find-file (concat (match-string 1 buf) ".mdl"))
>                 (error "Filename doesn't end in .mdl or _Impl.c: %s" buf)))
>           (error "Buffer is not visiting a file"))))
>
> --drkm

I've really been throwing out some slop lately.  It's embarrassing.

(defun my-open-complementary-file ()
  "If buffer-file-name ends in _Impl.c open .mdl and vise versa."
  (interactive)
  (cond
   ((not buffer-file-name)
    (message "Buffer is not visiting a file"))
   ((string-match "\\(\\`.*\\)\\.mdl\\'" buffer-file-name)
    (find-file (concat (match-string 1 buffer-file-name)"_Impl.c")))
   ((string-match "\\(\\`.*\\)_Impl\\.c\\'" buffer-file-name)
    (find-file (concat (match-string 1 buffer-file-name)".mdl")))
   (t (message "Buffer's filename doesn't end in .mdl or _Impl.c"))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]