From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id GLoJFeuQImVGTgEA9RJhRA:P1 (envelope-from ) for ; Sun, 08 Oct 2023 13:22:19 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id GLoJFeuQImVGTgEA9RJhRA (envelope-from ) for ; Sun, 08 Oct 2023 13:22:19 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C3F23581FD for ; Sun, 8 Oct 2023 13:22:18 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=A4qRsXUC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1696764139; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=fmzB4a4a+OA/bIB+gOu/3mvmJBwfUAEYGvb6BgWBMRw=; b=MSte/hJTcwZpcN2mbWZ00TFn8E/fnz0slo6pl67ZV7LxSLAsGst14HiuQCzv0IBJqzXTO9 4SzGyuYP6A6jdaEZLXBJYTr5t701n3JO8WBnln5liLNenbMBKTtx7+kVOUU/iee0Br+7lH MaiICsRiknd/8RXbp/t6QnaR5MymIlYT+lm3hzrueOyJb217smn9ftiUR+UVTECkzyA1VC XXFeGCO4wFitOOMcLrAG59UM70bvca8R8HqPoLo/EE9a66wWbUHdH4HfTVxVZRgWHpfSzR 2TZCrbS1ORm90nieKN6yKzok77Lh/c+wLoFOlp3vo5aAl60M4OZIpE3nh0tnhQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=A4qRsXUC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1696764139; a=rsa-sha256; cv=none; b=eI6W6I/+skT4TZFFQbNz7Fw1lxiIs/GTPYZDU57K24pW19xCdYB6FEaqLjwnHC2EiLQTso 3SeCbW1MxTORCt4cpcUQ4E3fLeWpyVK+woKiqQ35Bu9zJl8RnuP3IRimkLjoDMunAusAZo OXNBOtPJDU53OMWOZLIWbLIh4aQ0pGdNM5VMJ61RxfzYQqx8SsbSixpSdNx3gqiihfjBw0 sowtpU3VuP4xO152Ue5/EJl6ZXt0xGo0fI6xJwzFbFgRONptqkGepeoihSHlQAgzAeua8x +UA36nSyYGQFp1hTDpW9j+K32yGqATQNVOiQGvN9Dk23ele0BAZuzvLFUJpQuw== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qpRqa-0004QP-Su; Sun, 08 Oct 2023 07:21:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qpRqZ-0004QG-E0 for emacs-orgmode@gnu.org; Sun, 08 Oct 2023 07:21:19 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qpRqW-0007aP-Ud for emacs-orgmode@gnu.org; Sun, 08 Oct 2023 07:21:19 -0400 Received: by mail-pg1-x541.google.com with SMTP id 41be03b00d2f7-578d0dcd4e1so2279654a12.2 for ; Sun, 08 Oct 2023 04:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696764075; x=1697368875; darn=gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=fmzB4a4a+OA/bIB+gOu/3mvmJBwfUAEYGvb6BgWBMRw=; b=A4qRsXUCv7RDpP9h0YyL3kJ4Qa/0UtFQDIBbVLt0GkdOwBZGM033IdrlHI/SlbwBHR eLlYRGz3F/yg3GaLkHcwgzY0/XfH+QjTGSvuGVP249OepJ4pgHCKbEzDeY0L6vWMEdQo YUAfza7z5Y12WJWZDiFcEcRW3N4iwI9EGzfeH+JRfRWAYHVG9NKbvhNLj4P7Q4C+Kpim zW3nxgS47moby0kT40BXvgDwDUgk616cU5tjQDEV9EvmoHD+bKQY9gX4GTvGEiFZBkfY 8ru7AdJTU6vK3qww3GleFyZgHu9Dph3zBwq+1vOEOaPgP/+0+31W1QIhHkWv/CF2Vnyd /EyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696764075; x=1697368875; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fmzB4a4a+OA/bIB+gOu/3mvmJBwfUAEYGvb6BgWBMRw=; b=XLnVbcpYW0LF21uAIKFm+OIvnCBGYdW95Kwy6s1lk8rUoVEzCJPM/73WySLTkyPuEy 4MS3Wi/FkNj+fggRqGHYTcd+cr+J+ge46thNRz1jbXYa+ZpquCriLeyOszM1zc4D635t CC0AX8jUmg4xJopJY3ArsipUgKym6rAegnaQZE5VlDnqknKtRuaj+VQ6SK9J61GDgRcE s23qh4/oXq2RFLaxc9fpQBi62htUsOA5ogZLL11VZb+HT1cunajDAI/hfTwbIJVuQ19u qi1wYKB3y9Tv3OQN53YjtQJZx+KCnsDPdxux9CTgKyAl2CIfi1kunnnYvW4p6H//P6Pt qHrA== X-Gm-Message-State: AOJu0YzGGpQhGsR2qtlZPzJ0CyfYENoQghNBW5OOA9or6SY4KkLPNSdN 0YZ+DblTCM8PB849xMfBQfjOjm3POp7HUg== X-Google-Smtp-Source: AGHT+IEEnMDFlSA1awh3ughGXMIZ/U7iRbvujj5yg8C9F5ily9jNUf7YEfXYM6B2N1FpVqFvjoZXIw== X-Received: by 2002:a05:6a20:948c:b0:160:db54:64b1 with SMTP id hs12-20020a056a20948c00b00160db5464b1mr7419361pzb.21.1696764075418; Sun, 08 Oct 2023 04:21:15 -0700 (PDT) Received: from localhost ([118.185.152.162]) by smtp.gmail.com with ESMTPSA id je14-20020a170903264e00b001c74d166467sm7388897plb.97.2023.10.08.04.21.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 04:21:14 -0700 (PDT) From: Visuwesh To: Ihor Radchenko Cc: emacs-orgmode@gnu.org Subject: Re: [BUG] [PATCH] Add yank-media and DND handler [9.6.7 (9.6.7-g6eb773 @ /home/viz/lib/emacs/straight/build/org/)] In-Reply-To: <87ttr1pild.fsf@localhost> (Ihor Radchenko's message of "Sun, 08 Oct 2023 09:30:06 +0000") References: <87jzsintv0.fsf@gmail.com> <87lecx2nff.fsf@localhost> <87bkdsomm2.fsf@gmail.com> <87a5tamm3j.fsf@gmail.com> <877cnyzlda.fsf@localhost> <87a5sur51r.fsf@gmail.com> <875y3ir0lu.fsf@gmail.com> <87ttr1pild.fsf@localhost> Date: Sun, 08 Oct 2023 16:51:11 +0530 Message-ID: <87v8bhpdg8.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::541; envelope-from=visuweshm@gmail.com; helo=mail-pg1-x541.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Queue-Id: C3F23581FD X-Spam-Score: -6.56 X-Migadu-Spam-Score: -6.56 X-Migadu-Scanner: mx1.migadu.com X-TUID: eRT60jKQZI5a --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable [=E0=AE=9E=E0=AE=BE=E0=AE=AF=E0=AE=BF=E0=AE=B1=E0=AF=81 =E0=AE=85=E0=AE=95= =E0=AF=8D=E0=AE=9F=E0=AF=8B=E0=AE=AA=E0=AE=B0=E0=AF=8D 08, 2023] Ihor Radch= enko wrote: > Visuwesh writes: > >> This was far easier than I initially thought. Patch attached. > > Thanks! > I tried to install the patch and I have two more comments. > >> +(defun org-yank-image-autogen-filename () >> + "Autogenerate filename for image in clipboard." >> + (format-time-string "clipboard-%Y-%m-%d-%H:%M")) > > This is too coarse - no more than a single unique image name per minute. > What about the default value of `org-id-ts-format' - "%Y%m%dT%H%M%S.%6N"? > This is guaranteed to be unique. OK, now done. >> +(defun org--image-yank-media-handler (mimetype data) >> + "Save image DATA of mime-type MIMETYPE and insert link at point. >> +It is saved as per `org-yank-image-save-type'. The name for the >> +image is prompted and the extension is automatically added to the >> +end." >> ... >> + (if (null (eq org-yank-image-save-type 'attach)) >> + (setq link (org-link-make-string >> + (concat "file:" (file-relative-name absname)) >> + filename)) > > I do not like that the inserted image link is with description. Images > with description cannot be previewed by default. I think that no > description would be more handy as one could then M-x yank-media > followed by M-x org-display-inline-images to see the inserted image > immediately. I didn't know about this. Now adjusted in the attached patch. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-support-for-yank-media-and-DND.patch >From f2d13548b970ebde90ea0a04be7951ef92d220a9 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Fri, 22 Sep 2023 20:11:41 +0530 Subject: [PATCH] Add support for yank-media and DND * lisp/org.el (org-mode): Call the setup function for yank-media and DND. (org-setup-yank-dnd-handlers): Register yank-media-handler and DND handler. (org-yank-image-save-type, org-yank-image-file-name-function) (org-dnd-default-attach-method, org-dnd-method): New defcustoms. (org--image-yank-media-handler, org--copied-files-yank-media-handler) (org--dnd-attach-file, org--dnd-local-file-handler, org--dnd-xds-method) (org--dnd-xds-function): Add yank-media and DND handlers. * etc/ORG-NEWS: Advertise the new features. --- etc/ORG-NEWS | 20 +++++ lisp/org.el | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 243 insertions(+), 1 deletion(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 252c5a9f9..c4a58dd4d 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -596,6 +596,26 @@ return a matplotlib Figure object to plot. For output results, the current figure (as returned by =pyplot.gcf()=) is cleared before evaluation, and then plotted afterwards. +*** Images and files in clipboard can be attached + +Org can now attach images in clipboard and files copied/cut to the +clipboard from file managers using the ~yank-media~ command which also +inserts a link to the attached file. This command was added in Emacs 29. + +Images can be saved to a separate directory instead of being attached, +customize ~org-yank-image-save-type~. + +Image filename chosen can be customized by setting +~org-yank-image-file-name-function~ which by default autogenerates a +filename based on the current time. + +*** Files and images can be attached by dropping onto Emacs + +Attachment method other than ~org-attach-method~ for dropped files can +be specified using ~org-dnd-default-attach-method~. + +Images dropped also respect the value of ~org-yank-image-save-type~. + ** New functions and changes in function arguments *** =TYPES= argument in ~org-element-lineage~ can now be a symbol diff --git a/lisp/org.el b/lisp/org.el index d0b2355ea..a7ec90f08 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -4999,7 +4999,10 @@ The following commands are available: (org--set-faces-extend '(org-block-begin-line org-block-end-line) org-fontify-whole-block-delimiter-line) (org--set-faces-extend org-level-faces org-fontify-whole-heading-line) - (setq-local org-mode-loading nil)) + (setq-local org-mode-loading nil) + + ;; `yank-media' handler and DND support. + (org-setup-yank-dnd-handlers)) ;; Update `customize-package-emacs-version-alist' (add-to-list 'customize-package-emacs-version-alist @@ -20254,6 +20257,225 @@ it has a `diary' type." (org-format-timestamp timestamp fmt t)) (org-format-timestamp timestamp fmt (eq boundary 'end))))))) +;;; Yank media handler and DND +(defun org-setup-yank-dnd-handlers () + "Setup the `yank-media' and DND handlers for buffer." + (setq-local dnd-protocol-alist + (cons '("^file:///" . org--dnd-local-file-handler) + dnd-protocol-alist)) + (when (fboundp 'yank-media-handler) + (yank-media-handler "image/.*" #'org--image-yank-media-handler) + ;; Looks like different DEs go for different handler names, + ;; https://larsee.com/blog/2019/05/clipboard-files/. + (yank-media-handler "x/special-\\(?:gnome\|KDE\|mate\\)-files" + #'org--copied-files-yank-media-handler)) + (when (boundp 'x-dnd-direct-save-function) + (setq-local x-dnd-direct-save-function #'org--dnd-xds-function))) + +(defcustom org-yank-image-save-type 'attach + "Method to save images yanked from clipboard and dropped to Emacs. +It can be the symbol `attach' to add it as an attachment, or a +directory name to copy/cut the image to that directory." + :group 'org + :package-version '(Org . "9.7") + :type '(choice (const :tag "Add it as attachment" attach) + (directory :tag "Save it in directory")) + :safe (lambda (x) (eq x 'attach))) + +(defcustom org-yank-image-file-name-function #'org-yank-image-autogen-filename + "Function to generate filename for image yanked from clipboard. +By default, this autogenerates a filename based on the current +time. +It is called with no arguments and should return a string without +any extension which is used as the filename." + :group 'org + :package-version '(Org . "9.7") + :type '(radio (function-item :doc "Autogenerate filename" + org-yank-image-autogen-filename) + (function-item :doc "Ask for filename" + org-yank-image-read-filename) + function)) + +(defun org-yank-image-autogen-filename () + "Autogenerate filename for image in clipboard." + (format-time-string "clipboard-%Y%m%dT%H%M%S.%6N")) + +(defun org-yank-image-read-filename () + "Read filename for image in clipboard." + (read-string "Basename for image file without extension: ")) + +(declare-function org-attach-attach "org-attach" (file &optional visit-dir method)) + +(defun org--image-yank-media-handler (mimetype data) + "Save image DATA of mime-type MIMETYPE and insert link at point. +It is saved as per `org-yank-image-save-type'. The name for the +image is prompted and the extension is automatically added to the +end." + (let* ((ext (symbol-name (mailcap-mime-type-to-extension mimetype))) + (iname (funcall org-yank-image-file-name-function)) + (filename (file-name-with-extension iname ext)) + (absname (expand-file-name + filename + (if (eq org-yank-image-save-type 'attach) + temporary-file-directory + org-yank-image-save-type))) + link) + (when (and (not (eq org-yank-image-save-type 'attach)) + (not (file-directory-p org-yank-image-save-type))) + (make-directory org-yank-image-save-type t)) + (with-temp-file absname + (insert data)) + (if (null (eq org-yank-image-save-type 'attach)) + (setq link (org-link-make-string (concat "file:" (file-relative-name absname)))) + (require 'org-attach) + (org-attach-attach absname nil 'mv) + (setq link (org-link-make-string (concat "attachment:" filename)))) + (insert link))) + +;; I cannot find a spec for this but +;; https://indigo.re/posts/2021-12-21-clipboard-data.html and pcmanfm +;; suggests that this is the format. +(defun org--copied-files-yank-media-handler (_mimetype data) + "Attach copied or cut files from file manager. +If the files were cut from the file manager, then the `mv' attach +method is used; `cp' otherwise. + +DATA is a string where the first line is the operation to +perform: copy or cut. Rest of the lines are file: links to the +concerned files." + (require 'org-attach) + ;; pcmanfm adds a null byte at the end for some reason. + (let* ((data (split-string data "[\0\n\r]" t "^file://")) + (files (cdr data)) + (method (if (equal (car data) "cut") + 'mv + 'cp))) + (dolist (f files) + (setq f (url-unhex-string f)) + (if (file-readable-p f) + (org-attach-attach f nil method) + (message "File `%s' is not readable, skipping" f))))) + +(defcustom org-dnd-method 'ask + "Action to perform on the dropped file. +When the value is the symbol, + . `attach' -- attach the dropped file + . `open' -- visit/open the dropped file in Emacs + . `file-link' -- insert file: link to the dropped file + . `ask' -- ask what to do out of the above." + :group 'org + :package-version '(Org . "9.7") + :type '(choice (const :tag "Attach" attach) + (const :tag "Open/Visit file" open) + (const :tag "Insert file: link" file-link) + (const :tag "Ask what to do" ask))) + +(defcustom org-dnd-default-attach-method nil + "Default attach method to use when DND action is unspecified. +This attach method is used when the DND action is `private'. +This is also used when `org-yank-image-save-type' is nil. +When nil, use `org-attach-method'." + :group 'org + :package-version '(Org . "9.7") + :type '(choice (const :tag "Default attach method" nil) + (const :tag "Copy" cp) + (const :tag "Move" mv) + (const :tag "Hard link" ln) + (const :tag "Symbolic link" lns))) + +(declare-function mailcap-file-name-to-mime-type "mailcap" (file-name)) +(defvar org-attach-method) + +(defun org--dnd-local-file-handler (url action) + (let ((method (if (eq org-dnd-method 'ask) + (caddr (read-multiple-choice + "What to do with dropped file?" + '((?a "attach" attach) + (?o "open" open) + (?f "insert file: link" file-link)))) + org-dnd-method))) + (pcase method + (`attach (org--dnd-attach-file url action)) + (`open (dnd-open-local-file url action)) + (`file-link + (let ((filename (dnd-get-local-file-name url))) + (insert (org-link-make-string (concat "file:" filename)))))))) + +(defun org--dnd-attach-file (url action) + "Attach filename given by URL using method pertaining to ACTION. +If ACTION is `move', use `mv' attach method. +If `copy', use `cp' attach method. +If `ask', ask the user. +If `private', use the method denoted in `org-dnd-default-attach-action'. +The action `private' is always returned." + (require 'mailcap) + (let* ((filename (dnd-get-local-file-name url)) + (mimetype (mailcap-file-name-to-mime-type filename)) + (separatep (and (string-prefix-p "image/" mimetype) + (not (eq 'attach org-yank-image-save-type)))) + (method (pcase action + ('copy 'cp) + ('move 'mv) + ('ask (caddr (read-multiple-choice + "Attach using method" + '((?c "copy" cp) + (?m "move" mv) + (?l "hard link" ln) + (?s "symbolic link" lns))))) + ('private (or org-dnd-default-attach-method + org-attach-method))))) + (if separatep + (funcall + (pcase method + ('cp #'copy-file) + ('mv #'rename-file) + ('ln #'add-name-to-file) + ('lns #'make-symbolic-link)) + filename + (expand-file-name (file-name-nondirectory filename) + org-yank-image-save-type)) + (org-attach-attach filename nil method)) + (insert + (org-link-make-string + (concat (if separatep + "file:" + "attachment:") + (if separatep + (expand-file-name (file-name-nondirectory filename) + org-yank-image-save-type) + (file-name-nondirectory filename)))) + "\n") + 'private)) + +(defvar-local org--dnd-xds-method nil + "The method to use for dropped file.") +(defun org--dnd-xds-function (need-name filename) + "Handle file with FILENAME dropped via XDS protocol. +When NEED-NAME is t, FILNAME is the base name of the file to be +saved. +When NEED-NAME is nil, the drop is complete." + (if need-name + (let ((method (if (eq org-dnd-method 'ask) + (caddr (read-multiple-choice + "What to do with dropped file?" + '((?a "attach" attach) + (?o "open" open) + (?f "insert file: link" file-link)))) + org-dnd-method))) + (setq-local org--dnd-xds-method method) + (pcase method + (`attach (expand-file-name filename (org-attach-dir 'create))) + (`open (expand-file-name (make-temp-name "emacs.") temporary-file-directory)) + (`file-link (read-file-name "Write file to: " nil nil nil filename)))) + (pcase org--dnd-xds-method + (`attach (insert (org-link-make-string + (concat "attachment:" (file-name-nondirectory filename))) + "\n")) + (`file-link (insert (org-link-make-string (concat "file:" filename)) + "\n")) + (`open (find-file filename))) + (setq-local org--dnd-xds-method nil))) + ;;; Other stuff (defvar reftex-docstruct-symbol) -- 2.40.1 --=-=-=--