3

I am looking for an alternative to file-name-extension that will include an ending ~ if there is one. For example, if there is a file named foo.el~, the result should be el~.

Rather than using the generic split-string, I was hoping there might be something more specific geared towards file extensions.

lawlist
  • 19,106
  • 5
  • 38
  • 120

1 Answers1

3

The built-in function file-name-extension uses file-name-sans-versions, which does us the favor of removing the tilde. http://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Components.html Removing file-name-sans-versions from said function achieves the desired effect.

(defun my-file-name-extension (filename &optional period)
"This function is a modification of `file-name-extension`, in that the function
`file-name-sans-extensions` has been removed -- thus, the tilde (if any) remains."
  (save-match-data
    (let ((file (file-name-nondirectory filename)))
      (if (and (string-match "\\.[^.]*\\'" file)
        (not (eq 0 (match-beginning 0))))
          (substring file (+ (match-beginning 0) (if period 0 1)))
        (if period
            "")))))
lawlist
  • 19,106
  • 5
  • 38
  • 120