diff --git a/lisp/files.el b/lisp/files.el index 62e1702fdf..ab8be6ad9e 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -4889,6 +4889,21 @@ extension, the value is \"\"." (if period ""))))) +(defun file-name-set-extension (filename extension) + "Set the extension of a FILENAME to EXTENSION. +Consolidates leading/trailing dots so that either `foo' or `.foo' +can be passed as an EXTENSION. + +Returns nil if either of the FILENAME or EXTENSION are nil before +dot consolidation, or empty afterwards." + (when (and filename extension) + (let* ((patt "[ .]+") + (filename (string-trim-right filename patt)) + (extension (string-trim-left extension patt))) + (unless (or (string-empty-p filename) + (string-empty-p extension)) + (concat (file-name-sans-extension filename) "." extension))))) + (defun file-name-base (&optional filename) "Return the base name of the FILENAME: no directory, no extension." (declare (advertised-calling-convention (filename) "27.1"))