The wise commenters below pointed out that my function above is a bit dangerous, since the variable name
that I use has global scope so could modify another variable called name
elsewhere. This can be solved by using (let ...)
to give the variable local scope, as Kaushal Modi points out, but I ended up going with the compact version suggested by Noam Postavsky:
;;advise deft-new-file-named to replace spaces in file names with -
(defun bjm-deft-strip-spaces (args)
"Replace spaces with - in the string contained in the first element of the list args. Used to advise deft's file naming function."
(list (replace-regexp-in-string " " "-" (car args)))
)
(advice-add 'deft-new-file-named :filter-args #'bjm-deft-strip-spaces)
Which uses filter-args in the advice function to specify that my bjm-quit-strip-spaces
function is applied to the arguments to deft-new-file-named
before they are passed to the latter function.