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

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

bug#24150: 25.1.50; New command: dired-create-empty-file


From: Tino Calancha
Subject: bug#24150: 25.1.50; New command: dired-create-empty-file
Date: Fri, 5 Aug 2016 15:03:10 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)


On Thu, 4 Aug 2016, Clément Pit--Claudel wrote:

Ah, so it's different from touch. Is there already a command in dired
that sets the access and modification time of a file? If not, maybe
this command could do it? I'm not sure it's useful to have the command
fail if the file exists. If you go that, maybe renaming the command to
eg dired-touch would be useful? Although I see the parallel with
dired-create-directory, so maybe it's fine.

'dired-do-touch' doesn't create a new file.
I use to create an empty file with
M-! touch new-file RET
Note this creates a new file in the current directory; it
desn't create non existing parens, for instance:
M-! touch new-paren/new-file RET
;; touch: cannot touch 'new-paren/new-file': No such file or directory

The command i am suggesting is a partner of 'dired-create-directory':
it also creates the paren dirs.

I think there's a slight problem with this sentence.  Maybe
*** New command 'dired-create-empty-file' (similar to
'dired-create-directory') creates a new empty file;
bound to 'M-+'.
Thanks a lot!

Do you think this could be a defun instead of a macro?
Of course, it should be a defun!
Following is the corrected patch to dired-aux.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 4732d9c..f95e74e 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -1888,25 +1888,47 @@ dired-dwim-target-defaults
       dired-dirs)))


-;;;###autoload
-(defun dired-create-directory (directory)
-  "Create a directory called DIRECTORY.
-If DIRECTORY already exists, signal an error."
-  (interactive
- (list (read-file-name "Create directory: " (dired-current-directory))))
-  (let* ((expanded (directory-file-name (expand-file-name directory)))
-        (try expanded) new)
-    (if (file-exists-p expanded)
-       (error "Cannot create directory %s: file exists" expanded))
+(defun dired--create-empty-file-or-directory (fname &optional create-file)
+  "Create an empty file or directory called FNAME.
+If FNAME already exists, signal an error.
+Optional arg CREATE-FILE if non-nil, then create a file. Otherwise create
+a directory. "
+  (let* ((expanded (directory-file-name (expand-file-name fname)))
+         (parent (directory-file-name (file-name-directory expanded)))
+         (try expanded) new)
+    (when create-file
+      (setq try parent
+            new expanded))
+    (when (file-exists-p expanded)
+      (error "Cannot create file %s: file exists" expanded))
     ;; Find the topmost nonexistent parent dir (variable `new')
     (while (and try (not (file-exists-p try)) (not (equal new try)))
       (setq new try
-           try (directory-file-name (file-name-directory try))))
-    (make-directory expanded t)
+            try (directory-file-name (file-name-directory try))))
+    (cond (create-file
+           (unless (file-exists-p parent)
+             (make-directory parent t))
+           (write-region "" nil expanded nil 0))
+          (t
+           (make-directory expanded t)))
     (when new
       (dired-add-file new)
       (dired-move-to-filename))))

+;;;###autoload
+(defun dired-create-directory (directory)
+  "Create a directory called DIRECTORY.
+If DIRECTORY already exists, signal an error."
+  (interactive (list (read-file-name "Create directory: ")))
+  (dired--create-empty-file-or-directory directory))
+
+;;;###autoload
+(defun dired-create-empty-file (file)
+  "Create an empty file called FILE.
+If FILE already exists, signal an error."
+  (interactive (list (read-file-name "Create empty file: ")))
+  (dired--create-empty-file-or-directory file 'create-file))
+
 (defun dired-into-dir-with-symlinks (target)
   (and (file-directory-p target)
        (not (file-symlink-p target))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

reply via email to

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