Skip to content

Commit

Permalink
clean clipboard related code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Jan 18, 2017
1 parent 6d4e39d commit 8ccc835
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 60 deletions.
2 changes: 1 addition & 1 deletion init.el
Expand Up @@ -48,8 +48,8 @@
(require 'init-modeline)
(require 'cl-lib)
(require 'init-compat)
(require 'init-utils)
(require 'init-site-lisp) ;; Must come before elpa, as it may provide package.el
(require 'init-utils)

;; Windows configuration, assuming that cygwin is installed at "c:/cygwin"
;; (condition-case nil
Expand Down
50 changes: 4 additions & 46 deletions lisp/init-clipboard.el
Expand Up @@ -5,18 +5,11 @@
;; kill-ring and clipboard are same? No, it's annoying!
;; (setq save-interprogram-paste-before-kill t)

(defun test-simpleclip ()
(simpleclip-set-contents "testsimpleclip!")
(string= "testsimpleclip!" (simpleclip-get-contents)))

(setq simpleclip-works (test-simpleclip))

;; you need install xsel under Linux
;; xclip has some problem when copying under Linux
(defun copy-yank-str (msg &optional clipboard-only)
(unless clipboard-only (kill-new msg))
(if simpleclip-works (simpleclip-set-contents msg)
(my-pclip-fallback msg)))
(my-pclip msg))

(defun cp-filename-of-current-buffer () "Copy file name (NOT full path) into the yank ring and OS clipboard."
(interactive)
Expand Down Expand Up @@ -59,48 +52,14 @@
(copy-yank-str (file-truename buffer-file-name))
(message "file full path => clipboard & yank ring")))

(defun my-gclip-fallback ()
(cond
((eq system-type 'darwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "/usr/bin/pbpaste" nil t nil "-Prefer" "txt"))))
((eq system-type 'cygwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "getclip" nil t nil))))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-output-to-string
(with-current-buffer standard-output
(call-process "xsel" nil t nil "--clipboard" "--output"))))
(t
(error "Clipboard support not available"))))

(defun my-pclip-fallback (str-val)
(cond
((eq system-type 'darwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "/usr/bin/pbcopy")))
((eq system-type 'cygwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "putclip")))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "xsel" nil nil nil "--clipboard" "--input")))
(t
(error "Clipboard support not available"))))

(defun copy-to-x-clipboard (&optional num)
"If NUM equals 1, copy the downcased string.
If NUM equals 2, copy the captalized string.
If NUM equals 3, copy the upcased string.
If NUM equals 4, kill-ring => clipboard."
(interactive "P")
(let* ((thing (my-use-selected-string-or-ask "")))
(if (region-active-p) (push-mark))
(if (region-active-p) (deactivate-mark))
(cond
((not num))
((= num 1)
Expand All @@ -114,8 +73,7 @@ If NUM equals 4, kill-ring => clipboard."
(t
(message "C-h f copy-to-x-clipboard to find right usage")))

(if simpleclip-works (simpleclip-set-contents thing)
(my-pclip-fallback thing))
(my-pclip thing)
(if (not (and num (= 4 num))) (message "kill-ring => clipboard")
(message "thing => clipboard!"))))

Expand All @@ -132,7 +90,7 @@ If N is 3, converted dashed to camelcased then paste."
(not (eolp))
(not (eobp)))
(forward-char))
(let* ((str (if simpleclip-works (simpleclip-get-contents) (my-gclip-fallback))))
(let* ((str (my-gclip)))
(cond
((not n)
;; do nothing
Expand Down
3 changes: 1 addition & 2 deletions lisp/init-emacs-w3m.el
Expand Up @@ -203,8 +203,7 @@
(t
(setq cmd (format "curl -L %s > %s.%s" url (w3mext-subject-to-target-filename) (file-name-extension url)))
(kill-new cmd)
(if (fboundp 'simpleclip-set-contents)
(simpleclip-set-contents cmd))
(my-pclip cmd)
(message "%s => clipd/kill-ring" cmd))))
))

Expand Down
2 changes: 1 addition & 1 deletion lisp/init-misc-lazy.el
Expand Up @@ -229,7 +229,7 @@ grab matched string, cssize them, and insert into kill ring"
"Make sure the full path of file exist in clipboard. This command will convert
The full path into relative path insert it as a local file link in org-mode"
(interactive)
(insert (format "[[file:%s]]" (file-relative-name (simpleclip-get-contents)))))
(insert (format "[[file:%s]]" (file-relative-name (my-gclip)))))

(defun font-file-to-base64 (file)
(let ((str "")
Expand Down
4 changes: 1 addition & 3 deletions lisp/init-misc.el
Expand Up @@ -691,9 +691,7 @@ If step is -1, go backward."
;; {{ cliphist.el
(setq cliphist-use-ivy t)
(defun my-select-cliphist-item (num str)
(unless (featurep 'simpleclip)
(require 'simpleclip))
(simpleclip-set-contents str))
(my-pclip str))
(setq cliphist-select-item-callback 'my-select-cliphist-item)
;; }}

Expand Down
44 changes: 42 additions & 2 deletions lisp/init-utils.el
Expand Up @@ -181,9 +181,49 @@
(format "rundll32.exe %SystemRoot%\\\\System32\\\\\shimgvw.dll, ImageView_Fullscreen %s &" file))))
rlt))

;; {{ simpleclip has problem on Emacs 25.1
(defun test-simpleclip ()
(simpleclip-set-contents "testsimpleclip!")
(string= "testsimpleclip!" (simpleclip-get-contents)))

(setq simpleclip-works (test-simpleclip))

(defun my-gclip ()
(if simpleclip-works (simpleclip-get-contents)
(cond
((eq system-type 'darwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "/usr/bin/pbpaste" nil t nil "-Prefer" "txt"))))
((eq system-type 'cygwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "getclip" nil t nil))))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-output-to-string
(with-current-buffer standard-output
(call-process "xsel" nil t nil "--clipboard" "--output")))))))

(defun my-pclip (str-val)
(if simpleclip-works (simpleclip-set-contents str-val)
(cond
((eq system-type 'darwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "/usr/bin/pbcopy")))
((eq system-type 'cygwin)
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "putclip")))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(with-temp-buffer
(insert str-val)
(call-process-region (point-min) (point-max) "xsel" nil nil nil "--clipboard" "--input"))))))
;; }}

(defun make-concated-string-from-clipboard (concat-char)
(let (rlt (str (replace-regexp-in-string "'" "" (upcase (simpleclip-get-contents)))))
(setq rlt (replace-regexp-in-string "[ ,-:]+" concat-char str))
(let* ((str (replace-regexp-in-string "'" "" (upcase (my-gclip))))
(rlt (replace-regexp-in-string "[ ,-:]+" concat-char str)))
rlt))

;; {{ diff region SDK
Expand Down
2 changes: 1 addition & 1 deletion snippets/css-mode/color-from-clip.yasnippet
Expand Up @@ -2,4 +2,4 @@
# name : color: ${1:clipboard}
# key: co
# --
color: ${1:`(simpleclip-get-contents)`};
color: ${1:`(my-gclip)`};
Expand Up @@ -3,4 +3,4 @@
# key: str
# contributor: Chen Bin <chenbin DOT sh AT gmail>
# --
'`(replace-regexp-in-string "\\(^[ \t]+\\|[ \t]+$\\)" "" (replace-regexp-in-string "> <" "><" (replace-regexp-in-string "'" "\\\\'" (replace-regexp-in-string "[ \t]*[\n\r]+[ \t]*" " " (simpleclip-get-contents)))))`'
'`(replace-regexp-in-string "\\(^[ \t]+\\|[ \t]+$\\)" "" (replace-regexp-in-string "> <" "><" (replace-regexp-in-string "'" "\\\\'" (replace-regexp-in-string "[ \t]*[\n\r]+[ \t]*" " " (my-gclip)))))`'
2 changes: 1 addition & 1 deletion snippets/js-mode/log-para-from-clipboard.yasnippet
Expand Up @@ -3,4 +3,4 @@
# key: lo
# contributor: Chen Bin <chenbin DOT sh AT gmail>
# --
`(mapconcat (lambda (i) (format "'%s=', %s" (my-yas-escape-string i) i)) (split-string (simpleclip-get-contents) ",[ \s\n]*") ", ")`
`(mapconcat (lambda (i) (format "'%s=', %s" (my-yas-escape-string i) i)) (split-string (my-gclip) ",[ \s\n]*") ", ")`
2 changes: 1 addition & 1 deletion snippets/org-mode/items-from-clipboard.yasnippet
Expand Up @@ -3,4 +3,4 @@
# contributor : Chen Bin <chenbin.sh AT gmail.com>
# key: gclip
# --
`(replace-regexp-in-string "^ *\\([^ \n\r]+\\) *$" "- [ ] \\1" (simpleclip-get-contents))`
`(replace-regexp-in-string "^ *\\([^ \n\r]+\\) *$" "- [ ] \\1" (my-gclip))`
2 changes: 1 addition & 1 deletion snippets/org-mode/todos-from-clipboard.yasnippet
Expand Up @@ -3,4 +3,4 @@
# contributor : Chen Bin <chenbin.sh AT gmail.com>
# key: gclip
# --
`(replace-regexp-in-string "^ *\\([^ \n\r]+\\) *$" "\* TODO \\1" (simpleclip-get-contents))`
`(replace-regexp-in-string "^ *\\([^ \n\r]+\\) *$" "\* TODO \\1" (my-gclip))`

0 comments on commit 8ccc835

Please sign in to comment.