Spacemacs Org 粘贴剪贴板上的图片


Spacemacs Org 粘贴剪贴板上的图片


粘贴剪贴板上的图片

1. mac

安装pngpate

brew install pngpaste

在.spacemacs中添加org-insert-image函数

(defun org-insert-image ()
  (interactive)
  (let* ((path (concat default-directory "img/"))
    (image-file (concat path (buffer-name)
        (format-time-string "_%Y%m%d_%H%M%S.png"))))
    (if (not (file-exists-p path))
      (mkdir path))
    (shell-command (concat "pngpaste " image-file))
      (org-insert-link nil (concat "file:" image-file) ""))
  ;; (org-display-inline-images) ;; inline显示图片
)
2. centos

pngpaste.py

#!/usr/bin/python
"""
Save image from clipboard to file
"""

import sys
import glob
from optparse import OptionParser

def def_file():
    """
    Return default file name
    """
    files = glob.glob("image.png")
    if len(files) < 1:
        return 'img001.png'
    maxf = 0
    for f in files:
        try:
            n = int(f[3:6])
            maxf = max(n, maxf)
        except ValueError:
            pass
    return 'img{:03d}.png'.format(maxf+1)

usage = """%prog [option] [filename]
Save image from clipboard to file in PNG format."""

op = OptionParser(usage=usage)
op.add_option("-o", "--open", action="store_true", dest="open", default=False,
        help="Open saved file with default program (using xdg-open)")
(options, args) = op.parse_args()

if len(args) > 1:
    parser.error("Only one argument expected")
    sys.exit(1)
elif len(args) == 1:
    fname = args[0]
else:
    fname = def_file()

import gtk
clipboard = gtk.clipboard_get()
image = clipboard.wait_for_image()
if image is not None:
    image.save(fname, "png")
    print "PNG image saved to file", fname
    if options.open:
        import subprocess
        subprocess.call(["xdg-open", fname])
else:
    print "No image in clipboard found"

在.spacemacs中添加org-insert-image函数

(defun org-insert-image ()
  (interactive)
  (let* ((path (concat default-directory "img/"))
    (image-file (concat path (buffer-name)
        (format-time-string "_%Y%m%d_%H%M%S.png"))))
    (if (not (file-exists-p path))
      (mkdir path))
    (shell-command (concat "{YOUR PATH}/pngpaste.py " image-file))
      (org-insert-link nil (concat "file:" image-file) ""))
  ;; (org-display-inline-images) ;; inline显示图片
)