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

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

renaming current file and buffer


From: Dieter Wilhelm
Subject: renaming current file and buffer
Date: Mon, 02 Jan 2006 23:29:33 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Hi

Every so often I'm working on a file whose name I've not chosen
appropriately.  So far I couldn't find an elegant way in Emacs to
rename the file and the buffer at the same time.  I found the function
definitions below in the net, which serve my needs perfectly, but I'm
wondering whether I've overlooked something equally fitting from standard
emacs packages? What are you doing in this situation? Yes I know:
Thinking in advance, but ...

(defun rename-file-and-buffer (new-name)
  "Renames both current buffer and file it's visiting to NEW-NAME."
  (interactive "sNew name: ")
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (if (not filename)
        (message "Buffer '%s' is not visiting a file!" name)
      (if (get-buffer new-name)
          (message "A buffer named '%s' already exists!" new-name)
        (progn
          (rename-file name new-name 1)
          (rename-buffer new-name)
          (set-visited-file-name new-name)
          (set-buffer-modified-p nil))))))

(defun move-file-and-buffer (dir)
  "Moves both current buffer and file it's visiting to DIR."
  (interactive "DNew directory: ")
  (let* ((name (buffer-name))
         (filename (buffer-file-name))
         (dir
          (if (string-match dir "\\(?:/\\|\\\\)$")
              (substring dir 0 -1) dir))
         (newname (concat dir "/" name)))

    (if (not filename)
        (message "Buffer '%s' is not visiting a file!" name)
      (progn
        (copy-file filename newname 1)
        (delete-file filename)
        (set-visited-file-name newname)
        (set-buffer-modified-p nil)
        t))))

-- 
Best wishes

     Dieter Wilhelm





reply via email to

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