DilithiumMatrix
DilithiumMatrix

Reputation: 18637

Lisp if-statement - emacs initialization file ".emacs"

I modified my .emacs file to make auto-backups hidden files via the following code:

(defun make-backup-file-name (filename)
  (expand-file-name
   (concat "." (file-name-nondirectory filename) "~")
   (file-name-directory filename)))

It works great except that backups of hidden-files go from ".hidden-file.xxx" to "..no-longer-hidden-file.xxx'

I know zero Lisp, can someone give me a quick work-around like:

(if (filename) doesn't-start-with "."
  (concat 
  (else do-nothing))

Upvotes: 2

Views: 736

Answers (3)

Stefan
Stefan

Reputation: 151

you can also test (string-match "\`\." (file-name-nondirectory filename)), but to tell you the truth I wonder why you think "..foo" is considered as "not hidden": in my tests, `ls' and * both ignored my "..test.txt" file.

Upvotes: 0

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68152

You could use (equal (string-to-char filename) ?.). This turns the filename string into its first character and compares it to ?., which is the character notation for a ..

By the looks of it, you want to check (file-name-nondirectory filename) rather than just filename, so the whole statement would be something like:

(if (equal (string-to-char (file-name-nondirectory filename)) ?.)
    (concat (file-name-nondirectory filename) "~")
    (concat "." (file-name-nondirectory filename) "~"))

So the whole function should look something like:

(defun make-backup-file-name (filename)
  (expand-file-name
  (if (equal (string-to-char (file-name-nondirectory filename)) ?.)
    (concat (file-name-nondirectory filename) "~")
    (concat "." (file-name-nondirectory filename) "~"))
  (file-name-directory filename)))

You need to do a concat in both branches because you always want to append a ~.

Upvotes: 3

snim2
snim2

Reputation: 4079

Like this:

(if (not (string-equal (substring "abcdefg" 0 1) "."))
 (message "foo") 
 (message "bar")
)

Since you're in emacs, open a scratch buffer and M-x eval-buffer what you're doing to check that it has the right semantics. message is useful in debugging as it prints a string to the mini-buffer.

Upvotes: 2

Related Questions