From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Add file-dwim. Date: Sun, 18 Oct 2015 21:46:37 +0200 Message-ID: <87lhb0ot7m.fsf@T420.taylan> References: <87d1wcrzuu.fsf@T420.taylan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1445197617 4478 80.91.229.3 (18 Oct 2015 19:46:57 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 18 Oct 2015 19:46:57 +0000 (UTC) Cc: emacs-devel To: Artur Malabarba Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Oct 18 21:46:52 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zntut-0000qZ-I7 for ged-emacs-devel@m.gmane.org; Sun, 18 Oct 2015 21:46:51 +0200 Original-Received: from localhost ([::1]:35359 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zntus-0001xg-HO for ged-emacs-devel@m.gmane.org; Sun, 18 Oct 2015 15:46:50 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52148) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zntuj-0001xV-Sk for emacs-devel@gnu.org; Sun, 18 Oct 2015 15:46:43 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zntui-0000Xz-A6 for emacs-devel@gnu.org; Sun, 18 Oct 2015 15:46:41 -0400 Original-Received: from mail-wi0-x22c.google.com ([2a00:1450:400c:c05::22c]:36601) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zntuh-0000X2-VY for emacs-devel@gnu.org; Sun, 18 Oct 2015 15:46:40 -0400 Original-Received: by wicfx6 with SMTP id fx6so23826779wic.1 for ; Sun, 18 Oct 2015 12:46:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=pcgiVLo/djhKsdMb8rQ/ZrHRzRLXqoMx1MhU9GfjWXI=; b=RaxXS9VyL5om2v9/+0USTToMKAPr9ZjvpNuU8FX7xgPy6RCe7n8AL7BsQTU0gOZoxO P3Ftk/ijGQn9talunmK1cOFDhbfE9y81Fimi/8yFU7iw0uasURfy/RiRXIFEduf2Z4ZX VqwyQWS9Hbqg1etlXuxIQ8GOn7oNfOtgD47uwCyXskhBOrBMafk1b1aYVzlIFTYwtohQ Fqy+0UJqea/kMA6tHGKZYV38RbwNYyEzw7AYm6OjFv46EQORTILm6/p8A/2QDERRzon9 G0U3CnZpEAC82y7tJYPxiQ5B9a+6WMco0DVsEVv36HPtlAjGjQeott1uZdPjF7BFLrFx 2ggQ== X-Received: by 10.181.29.102 with SMTP id jv6mr16107240wid.59.1445197599454; Sun, 18 Oct 2015 12:46:39 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c32:4740:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id lb10sm35716532wjc.9.2015.10.18.12.46.38 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Oct 2015 12:46:38 -0700 (PDT) In-Reply-To: (Artur Malabarba's message of "Sun, 18 Oct 2015 20:12:27 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::22c X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:192009 Archived-At: --=-=-= Content-Type: text/plain Artur Malabarba writes: >> + :group 'file-dwim) > > These :group entries are redundant. It is a (poorly documented) > feature that all defcustoms following a defgroup belong to that > defgroup. Thanks, didn't know that. >> +;; (defun play-video-file (file) >> +;; (shell-command (concat "my-video-player " (shell-quote-argument file)))) > > Why not say that the cdr of an entry in `file-dwim-action-list' can > also be a list like ("command" "arg1" "arg2"), in which case it used > in `start-process' with the file-name at the end? > For instance: > > (add-to-list 'file-dwim-action-list > (list (rx "." (or "mp3" "flac" "aac" "wav") eos) "mplayer")) > > I think that would be very valuable, as this sounds like the common > use for this package. Hmm, while that's indeed *my* most common use-case, I'm not sure if it should be baked into the library. People should be using shell-quasiquote to easily construct shell commands anyway. ;-) That leaves the freedom to choose between a synchronous and asynchronous process, whether the output goes into a buffer, etc.. I guess I could have a cross reference to shell-quasiquote since that will become an ELPA package too. Here's an updated patch. --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=0001-Add-file-dwim.patch Content-Transfer-Encoding: quoted-printable >From 210db5bd46496e6c404dfaa590a31cf79a5aef16 Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?Taylan=3D20Ulrich=3D20Bay=3DC4=3DB1rl=3DC4=3DB1/Kammer?=3D Date: Sun, 18 Oct 2015 16:53:51 +0200 Subject: [PATCH 1/2] Add file-dwim. --- packages/file-dwim/file-dwim.el | 129 ++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 129 insertions(+) create mode 100644 packages/file-dwim/file-dwim.el diff --git a/packages/file-dwim/file-dwim.el b/packages/file-dwim/file-dwim= .el new file mode 100644 index 0000000..9343ce5 --- /dev/null +++ b/packages/file-dwim/file-dwim.el @@ -0,0 +1,129 @@ +;;; file-dwim.el --- Do What I Mean for files. + +;; Copyright (C) 2015 Free Software Foundation, Inc. + +;; Author: Taylan Ulrich Bay=C4=B1rl=C4=B1/Kammer +;; Keywords: extensions, files +;; URL: https://github.com/TaylanUB/file-dwim + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Do any action on a file, according to a dispatch-table. E.g. specify t= hat +;; media files should be played, directories opened in Dired, etc.. This = could +;; be bound to RET in Dired, or used to replace `find-file'. +;; +;; Example configuration (using `shqq' from the shell-quasiquote package): +;; +;; (define-key dired-mode-map (kbd "RET") 'file-dwim-dired) +;; +;; (defun file-has-extension (file exts) +;; (string-match-p +;; (rx-to-string `(: "." (or ,@exts) eos)) +;; file)) +;; +;; (defun audio-file-p (file) +;; (file-has-extension file '("mp3" "flac" "aac" "wav"))) +;; +;; (defun video-file-p (file) +;; (file-has-extension +;; file '("mkv" "mp4" "wmv" "webm" "avi" "mpg" "mov" "flv" "mts"))) +;; +;; (defun guitarpro-file-p (file) +;; (file-has-extension file '("gp3" "gp4"))) +;; +;; (defun play-audio-file (file) +;; (shell-command (shqq (my-audio-player ,file)))) +;; +;; (defun play-video-file (file) +;; (shell-command (shqq (my-video-player ,file)))) +;; +;; (defun open-guitarpro-file (file) +;; (async-shell-command (shqq (tuxguitar ,file)))) +;; +;; (dolist (entry +;; '((audio-file-p . play-audio-file) +;; (video-file-p . play-video-file) +;; (guitarpro-file-p . open-guitarpro-file) +;; )) +;; (add-to-list 'file-dwim-action-list entry)) + +;;; Code: + +(eval-when-compile + (require 'cl) + (declare-function dired-get-file-for-visit "dired.el" ())) + +(defgroup file-dwim nil + "Do What I Mean for files.") + +(defcustom file-dwim-action-list nil + "A list of action-specifiers for file-types. + +Each entry must be either a cons cell or a unary function. + +If it's a cons cell, the car must either be a regexp or a +predicate, and the cdr an unary function that will be called with +a FILE argument. + +If it's a function, it will be called with a FILE argument and +should return t to indicate that it matched, and can do its +action at the same time. + +A function-entry could act on a file but return nil anyway to +allow further actions." + :type '(repeat (or (cons (or string function) + function) + function))) + +(defcustom file-dwim-default-action 'find-file + "The default action for when none of `file-dwim-action-list' +are applicable." + :type 'function) + +;;;###autoload +(defun file-dwim (file) + "Act on a file according to `file-dwim-action-list'." + (interactive "FFile: ") + (cl-block actions + (dolist (action file-dwim-action-list) + (cond + ((consp action) + (let ((test (car action)) + (function (cdr action))) + (cond + ((stringp test) + (when (string-match-p test file) + (return-from actions (funcall function file)))) + ((functionp test) + (when (funcall test file) + (return-from actions (funcall function file)))) + (t + (error "Bad test in `file-dwim-action-list': %S" action))))) + ((functionp action) + (if (funcall action file) + (return-from actions))) + (t + (error "Bad action in `file-dwim-action-list': %S" action)))) + (funcall file-dwim-default-action file))) + +;;;###autoload +(defun file-dwim-dired () + "In Dired, use `file-dwim' on a file" + (interactive) + (file-dwim (dired-get-file-for-visit))) + +(provide 'file-dwim) +;;; file-dwim.el ends here --=20 2.5.0 --=-=-=--