Skip to content

Commit

Permalink
Merge pull request #10 from cxxxr/add-error-handling
Browse files Browse the repository at this point in the history
Add error handling
  • Loading branch information
snmsts committed Feb 19, 2021
2 parents afcd374 + 13ee88f commit 8a580cb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
11 changes: 11 additions & 0 deletions src/conditions.lisp
@@ -0,0 +1,11 @@
(in-package :trivial-clipboard)

(define-condition trivial-clipboard-error (simple-error) ())

(define-condition not-installed (trivial-clipboard-error)
((programs
:initarg :programs
:reader not-installed-programs))
(:report (lambda (c s)
(format s "None of the commands are installed: ~S"
(not-installed-programs c)))))
88 changes: 49 additions & 39 deletions src/text.lisp
Expand Up @@ -9,43 +9,57 @@ Return nil if COMMAND is not found anywhere."
:output '(:string :stripped t)))
path))

(defvar *clipboard-in-command*
(defparameter *clipboard-commands*
#+(or darwin macosx)
"pbcopy"
#+(and :unix (:not :darwin))
(or (executable-find "wl-copy")
(executable-find "xclip")
(executable-find "xsel")
""))
'((:mac ("pbcopy") ("pbpaste")))
#-(or darwin macosx)
'((:wayland ("wl-copy") ("wl-paste"))
(:xclip ("xclip" "-in" "-selection" "clipboard") ("xclip" "-out" "-selection" "clipboard"))
(:xsel ("xsel" "--input" "--clipboard") ("xsel" "--output" "--clipboard"))))

(defvar *clipboard-out-command*
#+(or darwin macosx)
"pbpaste"
#+(and :unix (:not :darwin))
(or (executable-find "wl-paste")
*clipboard-in-command*))
(defun clipboard-programs (fn)
(loop :for elt :in *clipboard-commands*
:collect (first (funcall fn elt))))

(defun get-paste-command (elt)
(third elt))

(defun get-copy-command (elt)
(second elt))

(defun find-command (fn)
(loop :for elt :in *clipboard-commands*
:for command := (funcall fn elt)
:when (executable-find (first command))
:return command))

(let ((command nil))
(defun find-paste-command ()
(or command
(setf command (find-command #'get-paste-command)))))

(let ((command nil))
(defun find-copy-command ()
(or command
(setf command (find-command #'get-copy-command)))))

(defvar *clipboard-in-args*
(progn
'()
#+ (and :unix (:not :darwin))
(or (and (string= (pathname-name *clipboard-in-command*) "wl-copy")
'())
(and (string= (pathname-name *clipboard-in-command*) "xclip")
'("-in" "-selection" "clipboard"))
(and (string= (pathname-name *clipboard-in-command*) "xsel")
'("--input" "--clipboard")))))
(defun paste ()
(let ((command (find-paste-command)))
(if command
(with-output-to-string (output)
(uiop:run-program command
:output output))
(error 'not-installed
:programs (clipboard-programs #'get-paste-command)))))

(defvar *clipboard-out-args*
(progn
'()
#+ (and :unix (:not :darwin))
(or (and (string= (pathname-name *clipboard-in-command*) "wl-paste")
'())
(and (string= (pathname-name *clipboard-in-command*) "xclip")
'("-out" "-selection" "clipboard"))
(and (string= (pathname-name *clipboard-in-command*) "xsel")
'("--output" "--clipboard")))))
(defun copy (text)
(let ((command (find-copy-command)))
(if command
(with-input-from-string (input text)
(uiop:run-program command
:input input))
(error 'not-installed
:programs (clipboard-programs #'get-copy-command)))))

(defun text (&optional data)
"If DATA is STRING, it is set to the clipboard. An ERROR is
Expand All @@ -58,15 +72,11 @@ copy failed, it returns NIL instead."
#+os-windows
(set-text-on-win32 data)
#+(not os-windows)
(with-input-from-string (input data)
(uiop:run-program (cons *clipboard-in-command* *clipboard-in-args*)
:input input))
(copy data)
data)
(null
(or
#+os-windows
(get-text-on-win32)
#+(not os-windows)
(with-output-to-string (output)
(uiop:run-program (cons *clipboard-out-command* *clipboard-out-args*)
:output output))))))
(paste)))))
1 change: 1 addition & 0 deletions trivial-clipboard.asd
Expand Up @@ -9,6 +9,7 @@
:components ((:module "src"
:components
((:file "package")
(:file "conditions")
#+os-windows (:file "windows")
(:file "text"))))
:in-order-to ((test-op (test-op trivial-clipboard-test))))

0 comments on commit 8a580cb

Please sign in to comment.