From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#24150: 26.0.50; New command: dired-create-empty-file Date: Mon, 23 Jul 2018 12:57:09 +0900 Message-ID: <87va9661uy.fsf@calancha-pc.dy.bbexcite.jp> References: <87mv8m3yya.fsf@calancha-pc> <83wp7mn3go.fsf@gnu.org> <97b67d5a-fc1e-47ea-b6b1-4154206ed5f9@default> <87bmbfwp4x.fsf@calancha-pc.dy.bbexcite.jp> <879bc31155c8718745f44321382e9ecb@webmail.orcon.net.nz> <87lgaa9uq8.fsf@calancha-pc.dy.bbexcite.jp> <83pnzi1dpy.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1532318168 15351 195.159.176.226 (23 Jul 2018 03:56:08 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 23 Jul 2018 03:56:08 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: bug-gnu-emacs-bounces+psainty=orcon.net.nz@gnu.org, psainty@orcon.net.nz, tzz@lifelogs.com, clement.pit@gmail.com, michael.albinus@gmx.de, 24150@debbugs.gnu.org, sdl.web@gmail.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Jul 23 05:56:03 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fhRx4-0003s5-Sx for geb-bug-gnu-emacs@m.gmane.org; Mon, 23 Jul 2018 05:56:03 +0200 Original-Received: from localhost ([::1]:60801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhRzB-0004Xt-Qs for geb-bug-gnu-emacs@m.gmane.org; Sun, 22 Jul 2018 23:58:13 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33168) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhRz4-0004Xj-Dq for bug-gnu-emacs@gnu.org; Sun, 22 Jul 2018 23:58:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fhRz0-0005SO-CU for bug-gnu-emacs@gnu.org; Sun, 22 Jul 2018 23:58:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:47942) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fhRz0-0005SK-80 for bug-gnu-emacs@gnu.org; Sun, 22 Jul 2018 23:58:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fhRyz-0002nq-UV for bug-gnu-emacs@gnu.org; Sun, 22 Jul 2018 23:58:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 23 Jul 2018 03:58:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24150 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 24150-submit@debbugs.gnu.org id=B24150.153231824710730 (code B ref 24150); Mon, 23 Jul 2018 03:58:01 +0000 Original-Received: (at 24150) by debbugs.gnu.org; 23 Jul 2018 03:57:27 +0000 Original-Received: from localhost ([127.0.0.1]:52960 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fhRyP-0002my-LF for submit@debbugs.gnu.org; Sun, 22 Jul 2018 23:57:27 -0400 Original-Received: from mail-pf1-f195.google.com ([209.85.210.195]:34325) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fhRyO-0002ml-5g for 24150@debbugs.gnu.org; Sun, 22 Jul 2018 23:57:24 -0400 Original-Received: by mail-pf1-f195.google.com with SMTP id k19-v6so625664pfi.1 for <24150@debbugs.gnu.org>; Sun, 22 Jul 2018 20:57:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=w6DEVbdPLZ3BXw0fZVLmQf+XnAvWRiHqIk+jgQ0uCC0=; b=gZL6GFx7zsZI/2snkSUO1pEiXd/GeNA2FkleJo3lm34IP19NzHB3jFQNaNYE+yjDIo AK5NAQBC620Q2fLKj2NamaDU83YkmHhAZwpiYJQaQTzobvWJAiiLw5F41Kqrc8S2g/uH Lkx8szlkLy08StrPZQIn5uNmf1gJdNzGaLCQGQigMLQMc86XoaV76hLLLhvwVpVUKuZX A+/Vnp4XY5Nli95dMpV8QGjir8/25pXR0UjjE9ibkMhfdJXY2f/D5DZn5YNkUoqT6m3b UN16+YSKojtl79tyhpVwm18nfL7tr2THPQ3sSfHComxasxY4AfCMOOQQ+D0DpYeWbsFP Nbvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=w6DEVbdPLZ3BXw0fZVLmQf+XnAvWRiHqIk+jgQ0uCC0=; b=a46/5kXXzhJ5OnQcFdPSwt7Bu4xbFmYg6T66Lexr0lfonCvSU/lUmT84mf6FHx2b/4 vxKKBV/kJq3o9CoF01IJ8rDWRNIkPysXAISMC5VrEhb+AjKNC+19dlB4hS8To6NHap87 2jigQIt0WULFVtUFlTtajULdGpgydpCLQqO5t1V97rFtWX1tASFxQqsSuSF5ARvHkOPW +G4EAlpx5HxA+uhLgohnbzQuWVgKR5IyQP6MBdWEpaHEPa+czpeZ9VicWoUyr2QKvVIZ +f8SM+X5y19/yZAwOIKVVIAsH93lcetuYgbLayn1RVodarq8P9G9MBncLcPcSOJItgy4 zypw== X-Gm-Message-State: AOUpUlEL1vFA/MPk4bv+KuFayrxipai/0ECLUIabFLltd2V+/hUOhRL/ 86zMia0O+Xl9aVfFFBF+llQ= X-Google-Smtp-Source: AAOMgpdeSOfhSiend6NtTCZs9GquB16INOAHeDNj44ravEPyT0KryXBsmRWPhrW9pn5yIGsp1P1W/A== X-Received: by 2002:a63:5a5e:: with SMTP id k30-v6mr10842493pgm.123.1532318238145; Sun, 22 Jul 2018 20:57:18 -0700 (PDT) Original-Received: from calancha-pc.dy.bbexcite.jp (195.139.137.133.dy.bbexcite.jp. [133.137.139.195]) by smtp.gmail.com with ESMTPSA id 77-v6sm10271199pga.40.2018.07.22.20.57.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 22 Jul 2018 20:57:17 -0700 (PDT) In-Reply-To: <83pnzi1dpy.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 20 Jul 2018 12:03:05 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:148837 Archived-At: Eli Zaretskii writes: > I was somewhat surprised to see how much > code you needed. We have the capability of creating parent > directories in 'make-directory', so I thought all we'd need for > creating a new file is this two-step dance: > > . call make-directory to maybe create the file's parent directory > . call write-region to create the file itself > > What did I miss that needs so many lines of code? Right. Too much dance. Updated the patch to follow your recomendation: --8<-----------------------------cut here---------------start------------->8--- commit 700087e10f26359ed75b1cf2f23d72971eaafbca Author: Tino Calancha Date: Mon Jul 23 12:49:47 2018 +0900 New commands to create an empty file Similarly as `create-directory', `dired-create-directory', the new commands create the parent dirs as needed (Bug#24150). * lisp/files.el (make-empty-file): New command. * lisp/dired-aux.el (dired-create-empty-file): New command. (dired--find-topmost-parent-dir): New function extracted from `dired-create-directory'. (dired-create-directory, dired-create-empty-file): Use it. * lisp/dired.el (dired-mode-map): Add menu entry for `dired-create-empty-file'. * doc/emacs/dired.texi (Misc Dired Features) * doc/lispref/files.texi (Create/Delete Dirs): Update manual. ; * etc/NEWS: Announce the changes. diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi index 007a943714..1b03a3967a 100644 --- a/doc/emacs/dired.texi +++ b/doc/emacs/dired.texi @@ -1468,6 +1468,11 @@ Misc Dired Features directory's name, and creates that directory. It signals an error if the directory already exists. +@findex dired-create-empty-file + The command (@code{dired-create-empty-file}) reads a +file name, and creates that file. It signals an error if +the file already exists. + @cindex searching multiple files via Dired @kindex M-s a C-s @r{(Dired)} @kindex M-s a M-C-s @r{(Dired)} diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index 068cf05443..c891349c59 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -3006,6 +3006,12 @@ Create/Delete Dirs interactive call, that means to create the parent directories first, if they don't already exist. +@deffn Command make-empty-file filename &optional parents +This command creates an empty file named @var{filename}. +As @code{make-directory}, this command creates parent directories +if @var{parents} is non-@code{nil}. +If @var{filename} already exists, then this command signal an error. + @code{mkdir} is an alias for this. @end deffn diff --git a/etc/NEWS b/etc/NEWS index 57b51f61b6..78fd7fd80d 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -180,6 +180,9 @@ This triggers to search the program on the remote host as indicated by * Editing Changes in Emacs 27.1 ++++ +** New command 'make-empty-file'. + --- ** New variable 'x-wait-for-event-timeout'. This controls how long Emacs will wait for updates to the graphical @@ -217,6 +220,11 @@ navigation and editing of large files. * Changes in Specialized Modes and Packages in Emacs 27.1 ++++ +** Dired + +*** New command 'dired-create-empty-file'. + ** Change Logs and VC *** Recording ChangeLog entries doesn't require an actual file. diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 925a7d50d6..8fd6dcdea0 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -1989,6 +1989,16 @@ dired-dwim-target-defaults dired-dirs))) + +(defun dired--find-topmost-parent-dir (filename) + "Return the topmost nonexistent parent dir of FILENAME. +FILENAME is a full file name." + (let ((try filename) new) + (while (and try (not (file-exists-p try)) (not (equal new try))) + (setq new try + try (directory-file-name (file-name-directory try)))) + new)) + ;;;###autoload (defun dired-create-directory (directory) "Create a directory called DIRECTORY. @@ -1997,18 +2007,31 @@ dired-create-directory (interactive (list (read-file-name "Create directory: " (dired-current-directory)))) (let* ((expanded (directory-file-name (expand-file-name directory))) - (try expanded) new) + new) (if (file-exists-p expanded) (error "Cannot create directory %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)))) + (setq new (dired--find-topmost-parent-dir expanded)) (make-directory expanded t) (when new (dired-add-file new) (dired-move-to-filename)))) +;;;###autoload +(defun dired-create-empty-file (file) + "Create an empty file called FILE. +Parent directories of FILE are created as needed. +If FILE already exists, signal an error." + (interactive (list (read-file-name "Create empty file: "))) + (let* ((expanded (expand-file-name file)) + new) + (if (file-exists-p expanded) + (error "Cannot create file %s: file exists" expanded)) + (setq new (dired--find-topmost-parent-dir expanded)) + (make-empty-file file 'parents) + (when new + (dired-add-file new) + (dired-move-to-filename)))) + (defun dired-into-dir-with-symlinks (target) (and (file-directory-p target) (not (file-symlink-p target)))) diff --git a/lisp/dired.el b/lisp/dired.el index 1348df6934..26a7449e03 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -1802,6 +1802,9 @@ dired-mode-map (define-key map [menu-bar immediate create-directory] '(menu-item "Create Directory..." dired-create-directory :help "Create a directory")) + (define-key map [menu-bar immediate create-empty-file] + '(menu-item "Create Empty file..." dired-create-empty-file + :help "Create an empty file")) (define-key map [menu-bar immediate wdired-mode] '(menu-item "Edit File Names" wdired-change-to-wdired-mode :help "Put a Dired buffer in a mode in which filenames are editable" diff --git a/lisp/files.el b/lisp/files.el index eabb3c0e06..0880ab8dc0 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5519,6 +5519,20 @@ make-directory (dolist (dir create-list) (files--ensure-directory dir))))))) +(defun make-empty-file (filename &optional parents) + "Create an empty file FILENAME. +Optional arg PARENTS, if non-nil then creates parent dirs as needed. + +If called interactively, then PARENTS is non-nil." + (interactive + (let ((filename (read-file-name "Create empty file: "))) + (list filename t))) + (let ((paren-dir (file-name-directory filename))) + (when paren-dir (make-directory paren-dir parents))) + (if (and (file-exists-p filename) (null parents)) + (signal 'file-already-exists `("File exists" ,filename) ) + (write-region "" nil filename nil 0))) + (defconst directory-files-no-dot-files-regexp "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*" "Regexp matching any file name except \".\" and \"..\".") --8<-----------------------------cut here---------------end--------------->8--- In GNU Emacs 27.0.50 (build 20, x86_64-pc-linux-gnu, GTK+ Version 3.22.11) of 2018-07-23 Repository revision: 8f3bca3ad513549af552b321aaca81e9e635857b Windowing system distributor 'The X.Org Foundation', version 11.0.11902000 System Description: Debian GNU/Linux 9 (stretch)