emacs org-mode用の画像貼り付るための設定

クリップボード画像を貼付けするための設定
利用環境は、windows10 + emacs 27.1

Take Screenshots Straight into Org Files in Emacs on Win10

上記を参考にして、若干手直し。
- 保存ファイル名の temp-filename は、\のエスケープを追加
- 保存ファイル名、パスに日本語が含まれるとエラーで画像の保存がNGとなる

;;
;; My Function for Screenshots
;;
(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
same directory as the org-buffer and insert a link to this file."
  (interactive)

  (setq temp-filename
    (concat
      (make-temp-name
        (concat "images\\"
          (file-name-sans-extension (buffer-name))
          "_"
          (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))

;;  (shell-command "snippingtool /clip") ;; 有効にするとツールで範囲指定した箇所を取り込みできる
  (shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms;if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {$image = [System.Windows.Forms.Clipboard]::GetImage();[System.Drawing.Bitmap]$image.Save('"temp-filename"',[System.Drawing.Imaging.ImageFormat]::Png); Write-Output 'clipboard content saved as file'} else {Write-Output 'clipboard does not contain image data'}\""))
  (insert (concat "[[file:" temp-filename "]]"))
  (org-display-inline-images))

(global-set-key "\C-cs" 'my-org-screenshot)

2022/6 までは、以下を利用。参考にしたリンクがあったはずだが見つからない。

;;
;; org-screenshot
;; orgファイルの保存場所にある images フォルダに orgファイル名+タイムスタンプ を保存
(defun org-screenshot ()
  (interactive)
  (setq tilde-buffer-filename
        (replace-regexp-in-string "/" "\\" (file-name-directory buffer-file-name) t t))
  (setq temp-filename
        (concat
         (make-temp-name
          (concat "images\\"
                  (file-name-sans-extension (buffer-name))
                  "_"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".jpg"))
  (setq full-filename
        (concat tilde-buffer-filename temp-filename))

  ;; Windows: Irfanview
  (call-process "c:\\Program Files\\IrfanView\\i_view64.exe" nil nil nil (encode-coding-string (concat "/clippaste /convert=" full-filename ) 'sjis))
  (insert (concat "[[file:"  temp-filename "]]"))
  (org-display-inline-images))
(define-key ctl-x-map "p" 'org-screenshot)