From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: numbered backups Date: Wed, 21 May 2014 06:14:40 +0200 Organization: Informatimago Message-ID: <87y4xvg43z.fsf@kuiper.lan.informatimago.com> References: <877g5fhn75.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1400646024 9236 80.91.229.3 (21 May 2014 04:20:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 21 May 2014 04:20:24 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed May 21 06:20:19 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Wmy0n-0007s9-WD for geh-help-gnu-emacs@m.gmane.org; Wed, 21 May 2014 06:20:18 +0200 Original-Received: from localhost ([::1]:56815 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wmy0n-0002w3-DR for geh-help-gnu-emacs@m.gmane.org; Wed, 21 May 2014 00:20:17 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 115 Original-X-Trace: individual.net dU2OZqDDkLAD0Yb0c3Yj1AsXbsBBH7FNxZlDajpr22hg6xDIrW Cancel-Lock: sha1:NTA0ZGJlM2JhY2Q2ZTk0NmI1NTczZGRhMDUxM2I1ZThmM2QyYmRkNw== sha1:xXxF3UWnRu7UJd6Fm0dlt6PI8j0= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:205503 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:97772 Archived-At: Steven Arntson writes: > "Pascal J. Bourguignon" writes: > >> Steven Arntson writes: >> >>> I know I read how to do this somewhere, but can't find it now. I'd like >>> emacs to name my backup files not as >>> >>> file.org.~1~ >>> file.org.~2~ >>> >>> but rather as >>> >>> file.org.~001~ >>> file.org.~002~ >>> >>> is there a variable I could call up from M-x customize to influence this >>> behavior? >> >> See the variable make-backup-file-name-function >> and the function make-backup-file-name > > This looks promising, but I'm too much of a beginner to accomplish much > with it! Under "value menu" for "Make Backup File Name Function" it asks > me, "Your function: ______". I don't know what to put there to achieve > my desired result. Reading the source of make-backup-file-name we can see that it's actually the function find-backup-file-name that formats the backup file names. This function has to be changed to allow for smooth migration from .~%d~ to .~%03d~ version numbers, and to allow parameterisation of the version number format. Here is the new find-backup-file-name function, with the additions. (defcustom backup-file-version-format "%d" "format specifier for version numbers in backup files." :group 'backup :type 'string) (defun format-backup-file-name (name &optional version) (if version (concat name ".~" (format backup-file-version-format version) "~") (concat name ".~"))) (defun find-backup-file-name (fn) "Find a file name for a backup file FN, and suggestions for deletions. Value is a list whose car is the name for the backup file and whose cdr is a list of old versions to consider deleting now. If the value is nil, don't make a backup. Uses `backup-directory-alist' in the same way as does `make-backup-file-name'." (let ((handler (find-file-name-handler fn 'find-backup-file-name))) ;; Run a handler for this function so that ange-ftp can refuse to do it. (if handler (funcall handler 'find-backup-file-name fn) (if (or (eq version-control 'never) ;; We don't support numbered backups on plain MS-DOS ;; when long file names are unavailable. (and (eq system-type 'ms-dos) (not (msdos-long-file-names)))) (list (make-backup-file-name fn)) (let* ((basic-name (make-backup-file-name-1 fn)) (base-versions (format-backup-file-name (file-name-nondirectory basic-name))) (backup-extract-version-start (length base-versions)) (high-water-mark 0) (number-to-delete 0) possibilities deserve-versions-p versions) (condition-case () (setq possibilities (file-name-all-completions base-versions (file-name-directory basic-name)) versions (sort (mapcar (lambda (file) (cons (backup-extract-version file) file)) possibilities) (lambda (a b) (< (car a) (car b)))) high-water-mark (apply (function max) 0 (mapcar (function car) versions)) deserve-versions-p (or version-control (> high-water-mark 0)) number-to-delete (- (length versions) kept-old-versions kept-new-versions -1)) (file-error (setq possibilities nil))) (if (not deserve-versions-p) (list (make-backup-file-name fn)) (cons (format-backup-file-name basic-name (1+ high-water-mark)) (if (and (> number-to-delete 0) ;; Delete nothing if there is overflow ;; in the number of versions to keep. (>= (+ kept-new-versions kept-old-versions -1) 0)) (let ((new-versions (let ((v (nthcdr kept-old-versions versions))) (rplacd (nthcdr (1- number-to-delete) v) ()) v))) (mapcar (function cdr) new-versions)))))))))) So now you can change the format: (setf backup-file-version-format "%03d") (find-backup-file-name "/home/pjb/scratch.txt") --> ("/home/pjb/scratch.txt.~013~" "scratch.txt.~4~") -- __Pascal Bourguignon__ http://www.informatimago.com/ "Le mercure monte ? C'est le moment d'acheter !"