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: drkm
Subject: Re: Need help writing file-visiting macro
Date: Mon, 25 Jul 2005 23:02:45 +0200
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/22.0.50 (windows-nt)

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


reply via email to

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