gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Create a directory when saving a file


From: Kevin Rodgers
Subject: Re: Create a directory when saving a file
Date: Thu, 28 Oct 2004 15:31:34 -0600
User-agent: Mozilla Thunderbird 0.8 (X11/20040916)

Juan-Leon wrote:
> Often (specially when saving mail attachments) I decide to create a
> new directory for a file I want to save. I did not know how to make
> this quickly from emacs, so I wrote this piece of code that maybe
> somebody could find useful:
>
> ;; Use C-RET to create a directory from minibuffer while you are
> ;; writing the name (it does not matter if the name portion has been
> ;; already typed)
> (add-hook 'minibuffer-setup-hook 'leon-add-keybinding-for-create-dir)
>
> (defun leon-add-keybinding-for-create-dir()
>   (interactive)
>   (local-set-key [(control return)] 'leon-create-dir-from-minibuffer))

That function isn't very useful, and you might want to restrict the
keybinding to commands that are reading file names:

(add-hook 'minibuffer-setup-hook
          (lambda ()
            (when (eq minibuffer-completion-table 'read-file-name-internal)
              (local-set-key [(control return)]
                             'leon-create-dir-from-minibuffer))))

> (defun leon-create-dir-from-minibuffer()
>   (interactive)
>   (save-excursion
>     (beginning-of-line)
>     (make-directory
>      (file-name-directory
>       (buffer-substring-no-properties (point) (point-max)))
>      t)))

That's a useful command.  But I think since it's got "minibuffer" in its
name you may as well use the utility function provided to get the
minibuffer's text, and I don't know that you need to discard any text
properties from the string before you pass it to file-name-directory or
make-directory (so I've left the -no-properties part off):

(defun leon-create-dir-from-minibuffer()
  (interactive)
  (make-directory (file-name-directory (minibuffer-contents)) t))

--
Kevin Rodgers


reply via email to

[Prev in Thread] Current Thread [Next in Thread]