unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* renaming current file and buffer
@ 2006-01-02 22:29 Dieter Wilhelm
  2006-01-03  3:37 ` Ian Zimmerman
  0 siblings, 1 reply; 13+ messages in thread
From: Dieter Wilhelm @ 2006-01-02 22:29 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-02-11 12:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-02 22:29 renaming current file and buffer Dieter Wilhelm
2006-01-03  3:37 ` Ian Zimmerman
2006-01-03 10:16   ` Dieter Wilhelm
2006-01-03 20:39     ` Peter Dyballa
2006-01-03 23:51       ` Dieter Wilhelm
2006-01-03 23:58         ` Peter Dyballa
2006-01-04  1:03           ` Dieter Wilhelm
2006-01-04 11:23             ` Peter Dyballa
     [not found]             ` <mailman.21367.1136381051.20277.help-gnu-emacs@gnu.org>
2006-02-01  1:11               ` David Combs
2006-02-01  9:20                 ` Peter Dyballa
2006-02-11 12:32                   ` Dieter Wilhelm
     [not found]                 ` <mailman.488.1138786073.3044.help-gnu-emacs@gnu.org>
2006-02-09  6:40                   ` David Combs
2006-01-04  0:03     ` Ian Zimmerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).