I'd do it this way:
(defun add-default-extension (retval)
(let ((filename (car retval))) ; retval is (filename t)
(if (or (file-exists-p filename)
(file-name-extension filename))
retval
(message "Adding default .org extension")
(list (concat filename ".org") t))))
(advice-add 'find-file-read-args :filter-return #'add-default-extension)
(Well, it would be more accurate to say "I wouldn't do it, but if I had to, I'd do it this way:" :P)
EDIT: The above answer only add .org
to file names typed interactively at the find-file
prompt, but I read in the comments to the question that you are actually not using find-file
but this function instead:
(defun find-file-region()
(interactive)
(if (region-active-p)
(let ((str (buffer-substring (region-beginning) (region-end))))
(find-file str))
(message "No region active")))
In that case, just add the extension there:
(defun find-file-region ()
(interactive)
(if (region-active-p)
(let ((str (buffer-substring (region-beginning) (region-end))))
(find-file (if (or (file-exists-p str)
(file-name-extension str))
str
(concat str ".org"))))
(message "No region active")))
C-xC-f
or are you using your own command that makes use offind-file
? If the latter, then you can use this in theinteractive
spec, to read the file name:(read-file-name "File: " nil (expand-file-name ".org") nil)
. That will insert the directory/.org
, and put your cursor before the dot so you can type the file name.find-file
to be honest.(defun my-find-file (filename &optional wildcards) "..." (interactive (list (read-file-name "File: " nil (expand-file-name ".org") nil) t)) (find-file filename wildcards))