unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Changing name format for backup files
@ 2016-01-11 22:21 Nick Helm
  2016-01-11 23:26 ` Óscar Fuentes
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Helm @ 2016-01-11 22:21 UTC (permalink / raw)
  To: help-gnu-emacs

I like to keep my emacs backup files in the same directory as the file I'm
editing, but I want the backups to be hidden in the GUI by default. I achieve
this by adding a prefix dot to the backup filenames by tweaking
`make-backup-file-name-1' like this:

 (defun make-backup-file-name-1 (file)
   "Subroutine of `make-backup-file-name--default-function'.
 The function `find-backup-file-name' also uses this."
   (let ((alist backup-directory-alist)
 	elt backup-directory abs-backup-directory)
     (while alist
       (setq elt (pop alist))
       (if (string-match (car elt) file)
 	  (setq backup-directory (cdr elt)
 		alist nil)))
     ;; If backup-directory is relative, it should be relative to the
     ;; file's directory.  By expanding explicitly here, we avoid
     ;; depending on default-directory.
     (if backup-directory
 	(setq abs-backup-directory
 	      (expand-file-name backup-directory
 				(file-name-directory file))))
     (if (and abs-backup-directory (not (file-exists-p abs-backup-directory)))
 	(condition-case nil
 	    (make-directory abs-backup-directory 'parents)
 	  (file-error (setq backup-directory nil
 			    abs-backup-directory nil))))
     (if (null backup-directory)
 ;; -----------8<------------------CHANGE HERE-----------------8<-------------
 ;;    file
                        (let* ((file-path (file-name-directory file))
                               (file-name (file-name-nondirectory file)))
                            (concat file-path "." file-name))
 ;; -----------8<------------------CHANGE HERE-----------------8<-------------
       (if (file-name-absolute-p backup-directory)
 	  (progn
 	    (when (memq system-type '(windows-nt ms-dos cygwin))
 	      ;; Normalize DOSish file names: downcase the drive
 	      ;; letter, if any, and replace the leading "x:" with
 	      ;; "/drive_x".
 	      (or (file-name-absolute-p file)
 		  (setq file (expand-file-name file))) ; make defaults explicit
 	      ;; Replace any invalid file-name characters (for the
 	      ;; case of backing up remote files).
 	      (setq file (expand-file-name (convert-standard-filename file)))
 	      (if (eq (aref file 1) ?:)
 		  (setq file (concat "/"
 				     "drive_"
 				     (char-to-string (downcase (aref file 0)))
 				     (if (eq (aref file 2) ?/)
 					 ""
 				       "/")
 				     (substring file 2)))))
 	    ;; Make the name unique by substituting directory
 	    ;; separators.  It may not really be worth bothering about
 	    ;; doubling `!'s in the original name...
 	    (expand-file-name
 	     (subst-char-in-string
 	      ?/ ?!
 	      (replace-regexp-in-string "!" "!!" file))
 	     backup-directory))
 	(expand-file-name (file-name-nondirectory file)
 			  (file-name-as-directory abs-backup-directory))))))

I do something similar to `make-auto-save-file-name' to achieve the same result
for auto save files.

Redefining core functions seems like a pretty heavy handed approach to the
problem though. Is there better way to achieve the same result? Or perhaps
there's a user option I missed in the manual?

Thanks, 
Nick



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

* Re: Changing name format for backup files
  2016-01-11 22:21 Changing name format for backup files Nick Helm
@ 2016-01-11 23:26 ` Óscar Fuentes
  2016-01-12 22:32   ` Nick Helm
  0 siblings, 1 reply; 6+ messages in thread
From: Óscar Fuentes @ 2016-01-11 23:26 UTC (permalink / raw)
  To: help-gnu-emacs

Nick Helm <nick@tenpoint.co.nz> writes:

> I do something similar to `make-auto-save-file-name' to achieve the
> same result for auto save files.
>
> Redefining core functions seems like a pretty heavy handed approach to the
> problem though. Is there better way to achieve the same result? Or perhaps
> there's a user option I missed in the manual?

For overriding how a function work, see defadvice.

However, often there are variables that control how some feature works.
For your case, those seems to apply:

C-h v make-backup-file-name-function
C-h v auto-save-file-name-transforms




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

* Re: Changing name format for backup files
  2016-01-11 23:26 ` Óscar Fuentes
@ 2016-01-12 22:32   ` Nick Helm
  2016-01-13 14:34     ` Alex Kost
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Helm @ 2016-01-12 22:32 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Óscar Fuentes

> However, often there are variables that control how some feature works.
> For your case, those seems to apply:
>
> C-h v make-backup-file-name-function

Thanks,

The trouble is, I also use numbered backups and in this case, when I set
make-backup-file-name-function to a custom function (as described in help), it
has no effect. For example, if I eval this:

  (defun nick-backup-file-naming-function (file)
     "Return a backup file name with prefix dot and suffix tilde."
     (concat (file-name-directory file) "." (file-name-nondirectory file) "~"))

  (setq make-backup-file-name-function 'nick-backup-file-naming-function)

  (setq version-control 'never) ;; or nil

  (make-backup-file-name "/Users/nick/Desktop/sample.txt")

it returns "/Users/nick/Desktop/.sample.txt~" as expected. However, with

  (setq version-control t)

it returns "/Users/nick/Desktop/sample.txt.~1~" , that is, backup names are
correctly versioned, but the name is missing the prefix dot. It appears as if
`make-backup-file-name' is not being called in this case.

Looking at `files.el' it seems like `find-backup-file-name' calls
`make-backup-file-name-1' directly, bypassing the custom fuction.

Changing that line to call `make-backup-file-name' still doesn't give the
desired behaviour though, returning backup names with the format
"/Users/nick/Desktop/.sample.txt~.~1~" instead.

Any ideas what I'm doing wrong? 





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

* Re: Changing name format for backup files
  2016-01-12 22:32   ` Nick Helm
@ 2016-01-13 14:34     ` Alex Kost
  2016-01-15  4:53       ` Nick Helm
  0 siblings, 1 reply; 6+ messages in thread
From: Alex Kost @ 2016-01-13 14:34 UTC (permalink / raw)
  To: Nick Helm; +Cc: Óscar Fuentes, help-gnu-emacs

Nick Helm (2016-01-13 01:32 +0300) wrote:

>> However, often there are variables that control how some feature works.
>> For your case, those seems to apply:
>>
>> C-h v make-backup-file-name-function
>
> Thanks,
>
> The trouble is, I also use numbered backups and in this case, when I set
> make-backup-file-name-function to a custom function (as described in help), it
> has no effect. For example, if I eval this:
>
>   (defun nick-backup-file-naming-function (file)
>      "Return a backup file name with prefix dot and suffix tilde."
>      (concat (file-name-directory file) "." (file-name-nondirectory file) "~"))
>
>   (setq make-backup-file-name-function 'nick-backup-file-naming-function)
>
>   (setq version-control 'never) ;; or nil
>
>   (make-backup-file-name "/Users/nick/Desktop/sample.txt")
>
> it returns "/Users/nick/Desktop/.sample.txt~" as expected. However, with
>
>   (setq version-control t)
>
> it returns "/Users/nick/Desktop/sample.txt.~1~" , that is, backup names are
> correctly versioned, but the name is missing the prefix dot. It appears as if
> `make-backup-file-name' is not being called in this case.
>
> Looking at `files.el' it seems like `find-backup-file-name' calls
> `make-backup-file-name-1' directly, bypassing the custom fuction.

When I was messing with backing up, I also noticed this thing.  So what
I did is: I just replaced `make-backup-file-name-1' with my own
`utl-make-backup-file-name-1' function¹ that does what I need:

  (advice-add 'make-backup-file-name-1
    :override 'utl-make-backup-file-name-1)

I didn't follow this thread closely, so I'm sorry if this reply is useless.

¹ https://github.com/alezost/emacs-utils/blob/master/utl-file.el#L76-L102

-- 
Alex



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

* Re: Changing name format for backup files
  2016-01-13 14:34     ` Alex Kost
@ 2016-01-15  4:53       ` Nick Helm
  2016-01-15  9:53         ` Alex Kost
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Helm @ 2016-01-15  4:53 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Alex Kost


> When I was messing with backing up, I also noticed this thing.  So what
> I did is: I just replaced `make-backup-file-name-1' with my own
> `utl-make-backup-file-name-1' function¹ that does what I need:
>
>   (advice-add 'make-backup-file-name-1
>     :override 'utl-make-backup-file-name-1)
>
> I didn't follow this thread closely, so I'm sorry if this reply is useless.

Ok, this is a good excuse to learn to use advice. This is what I tried:

   (defun nick-add-prefix-dot (file)
      "Inject a prefix dot into file name FILE."
      (concat (file-name-directory file) "." (file-name-nondirectory file)))

   (defun nick-auto-save-file-name-p (file)
       "Return non-nil if FILE is an auto-save file name."
       (string-match "\\`\\.#.*#\\'" file))

   (advice-add 'make-backup-file-name-1 :filter-return #'nick-add-prefix-dot)
   (advice-add 'make-auto-save-file-name :filter-return #'nick-add-prefix-dot)
   (advice-add 'auto-save-file-name-p :override #'nick-auto-save-file-name-p)

Which seems to work well. I don't think `backup-file-name-p' doesn't
need override advice in this case as it only checks that files end with ~.




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

* Re: Changing name format for backup files
  2016-01-15  4:53       ` Nick Helm
@ 2016-01-15  9:53         ` Alex Kost
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Kost @ 2016-01-15  9:53 UTC (permalink / raw)
  To: Nick Helm; +Cc: help-gnu-emacs

Nick Helm (2016-01-15 07:53 +0300) wrote:

>> When I was messing with backing up, I also noticed this thing.  So what
>> I did is: I just replaced `make-backup-file-name-1' with my own
>> `utl-make-backup-file-name-1' function¹ that does what I need:
>>
>>   (advice-add 'make-backup-file-name-1
>>     :override 'utl-make-backup-file-name-1)
>>
>> I didn't follow this thread closely, so I'm sorry if this reply is useless.
>
> Ok, this is a good excuse to learn to use advice. This is what I tried:
>
>    (defun nick-add-prefix-dot (file)
>       "Inject a prefix dot into file name FILE."
>       (concat (file-name-directory file) "." (file-name-nondirectory file)))
>
>    (defun nick-auto-save-file-name-p (file)
>        "Return non-nil if FILE is an auto-save file name."
>        (string-match "\\`\\.#.*#\\'" file))
>
>    (advice-add 'make-backup-file-name-1 :filter-return #'nick-add-prefix-dot)
>    (advice-add 'make-auto-save-file-name :filter-return #'nick-add-prefix-dot)
>    (advice-add 'auto-save-file-name-p :override #'nick-auto-save-file-name-p)
>
> Which seems to work well. I don't think `backup-file-name-p' doesn't
> need override advice in this case as it only checks that files end with ~.

Oh great, I didn't notice :filter-return before.  It can be very useful, thanks!

-- 
Alex



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

end of thread, other threads:[~2016-01-15  9:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-11 22:21 Changing name format for backup files Nick Helm
2016-01-11 23:26 ` Óscar Fuentes
2016-01-12 22:32   ` Nick Helm
2016-01-13 14:34     ` Alex Kost
2016-01-15  4:53       ` Nick Helm
2016-01-15  9:53         ` Alex Kost

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).