Paste an image on clipboard to Emacs Org mode file without saving it

后端 未结 8 1018
慢半拍i
慢半拍i 2021-01-30 09:21

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 wonderin

8条回答
  •  太阳男子
    2021-01-30 09:39

    (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
    

提交回复
热议问题