Paste an image on clipboard to Emacs Org mode file

2020-05-13 20:27发布

As I'm using the Emacs Org mode as a research log, sometime I want to keep track of something via screenshot images, and I definitely don't want to save them. So I'm wondering is there any way to insert those figures into my org mode file, like with word coping them from clipboard?

8条回答
Ridiculous、
2楼-- · 2020-05-13 21:11

(If of use), I used @assem's code (thank you v.much btw) and created a version that instead creates a sub-folder (if it doesn't exist) like : ${filename}IMG/ Then puts an image like img_date.png into that folder and then inserts a relative path into the buffer like:
[[ ./${filename}IMG/img_2015...png ]]

Here is code:

(defun my-org-screenshot ()
"Take a screenshot into a time stamped unique-named file in the
sub-directory (%filenameIMG) as the org-buffer and insert a link to this file."
(interactive)

(setq foldername (concat (buffer-file-name) "IMG/"))
(if (not (file-exists-p foldername))
  (mkdir foldername))

(setq imgName (concat "img_" (format-time-string "%Y_%m_%d__%H_%M_%S") ".png"))
(setq imgPath (concat (buffer-file-name) "IMG/" imgName))

(call-process "import" nil nil nil imgPath)

(setq relativeFilename (concat "./"
    (buffer-name) "IMG/" imgName))

(insert (concat "[[" relativeFilename "]]"))
(org-display-inline-images)
)

[Edit 2015.04.09]

In the mean time, I found the above doesn't work well. Technically it works, but it generates a lot of folder names that match the file names, making it tedious to select files as there are folder that have a similar name.

Instead I settled with a solution where I keep all my org files in the same folder, and have an 'img' sub folder' with all my images.

I use code like this to capture images:

(defun my/img-maker ()
 "Make folder if not exist, define image name based on time/date" 
  (setq myvar/img-folder-path (concat default-directory "img/"))

  ; Make img folder if it doesn't exist.
  (if (not (file-exists-p myvar/img-folder-path)) ;[ ] refactor thir and screenshot code.
       (mkdir myvar/img-folder-path))

  (setq myvar/img-name (concat "img_" (format-time-string "%Y_%m_%d__%H_%M_%S") ".png"))
  (setq myvar/img-Abs-Path (concat myvar/img-folder-path myvar/img-name)) ;Relative to workspace.

  (setq myvar/relative-filename (concat "./img/" myvar/img-name))
  (insert "[[" myvar/relative-filename "]]" "\n")
)

(defun my/org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
 sub-directory (%filenameIMG) as the org-buffer and insert a link to this file."
  (interactive)
  (my/img-maker)
  ;(make-frame-invisible)
  (lower-frame)
  (call-process "import" nil nil nil myvar/img-Abs-Path)

  (raise-frame)
  ;(make-frame-visible)
  (org-display-inline-images)
)

and a little bash script in my org/img folder to archive unreferenced images:

#!/bin/sh 

cd ~/git/LeoUfimtsev.github.io/org/img/
mkdir ~/git/LeoUfimtsev.github.io/org/img/archive
FILES=~/git/LeoUfimtsev.github.io/org/img/*.png
for f in $FILES
do
  var_grep_out=$(grep --directories=skip $(basename $f) ../*)
  if [ -z "$var_grep_out" ];
  then 
     echo "Archiving: $f"
     mv $f archive/
  fi
done
查看更多
放我归山
3楼-- · 2020-05-13 21:12

I like the images and other material that belongs together with an org file to be located in a clearly associated directory. I am using the org-attachment mechanism for this and wrote a few years back a package org-attachment-screenshot that is also available from MELPA. It allows providing a function for defining the directory name (e.g. document name + some postfix) for the attachments, and will also support the standard attachment functionalities, e.g. if you want to define attachments based on entries, and inheriting within entries. Also provides customizable variable for the snapshot command to be executed. Please have a look.

查看更多
登录 后发表回答