Skip to content

Commit

Permalink
Merge branch 'all_xclip'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlincoln committed Sep 4, 2015
2 parents 956ef51 + 7ba8a4e commit c5e28c4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: clipr
Type: Package
Title: Read and Write from the System Clipboard
Version: 0.1.0.9000
Version: 0.1.1
Authors@R: c(
person("Matthew", "Lincoln", email = "matthew.d.lincoln@gmail.com", role = c("aut", "cre")),
person("Louis", "Maddox", role = "ctb"))
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## clipr 0.1.1

- Bug fix that removes the explicit test for "Linux" in favor of a check for "xclip" or "xsel"
6 changes: 2 additions & 4 deletions R/clipboard.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ read_clip <- function() {
# Use the appropriate handler function
chosen_read_clip <- switch(sys.type,
"Darwin" = osx_read_clip,
"Linux" = linux_read_clip,
"Windows" = win_read_clip,
stop("System not recognized!")
linux_read_clip
)

content <- chosen_read_clip()
Expand Down Expand Up @@ -63,9 +62,8 @@ write_clip <- function(content, sep = NULL, eos = NULL) invisible({
# Choose an operating system-specific function (stop with error if not recognized)
chosen_write_clip <- switch(sys.type,
"Darwin" = osx_write_clip,
"Linux" = linux_write_clip,
"Windows" = win_write_clip,
stop("System not recognized!")
linux_write_clip
)

# Supply the clipboard content to write and options list to this function
Expand Down
30 changes: 20 additions & 10 deletions R/linux_clipboard.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Function to stop the read/write and return an error of missing clipboard software.
# Determine if system has 'xclip' installed
has_xclip <- function() {
nzchar(Sys.which("xclip"))
}

# Determine if system has 'xsel' installed
has_xsel <- function() {
nzchar(Sys.which("xsel"))
}

# Stop read/write and return an error of missing clipboard software.
notify_no_cb <- function() {
stop("Clipboard on Linux requires 'xclip' (recommended) or 'xsel'. Try using:\nsudo apt-get install xclip",
stop("Clipboard on Unix-like systems requires 'xclip' (recommended) or 'xsel'.",
call.=FALSE)
}

Expand All @@ -10,15 +20,15 @@ notify_no_cb <- function() {
# Adapted from: https://github.com/mrdwab/overflow-mrdwab/blob/master/R/readClip.R
# and: https://github.com/jennybc/reprex/blob/master/R/clipboard.R
linux_read_clip <- function() {
if (Sys.which("xclip") != "") {
if (has_xclip()) {
con <- pipe("xclip -o -selection clipboard")
} else if (Sys.which("xsel") != "") {
} else if (has_xsel()) {
con <- pipe("xsel --clipboard")
} else {
notify_no_cb()
}
content <- scan(con, what = character(), sep = "\n",
blank.lines.skip = FALSE, quiet = TRUE)
blank.lines.skip = FALSE, quiet = TRUE)
close(con)
return(content)
}
Expand All @@ -30,22 +40,22 @@ linux_read_clip <- function() {
#
# Targets "primary" and "clipboard" clipboards if using xclip, see: http://unix.stackexchange.com/a/69134/89254
linux_write_clip <- function(content, wc.opts) {
if (Sys.which("xclip") != "") {
if (has_xclip()) {
con <- pipe("xclip -i -sel p -f | xclip -i -sel c", "w")
} else if (Sys.which("xsel") != "") {
} else if (has_xsel()) {
con <- pipe("xsel -b", "w")
} else {
notify_no_cb()
}

# If no custom line separator has been specified, use Unix's default newline character: (\code{\n})
sep <- ifelse(is.null(wc.opts$sep), '\n', wc.opts$sep)

# If no custom 'end of string' character is specified, then by default assign \code{eos = NULL}
# Text will be sent to the clipboard without a terminator character.
eos <- wc.opts$eos
# Note - works the same as ifelse(is.null,NULL,wc.opts$eos)

content <- flat_str(content, sep)
writeChar(content, con = con, eos = eos)
close(con)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clipr

[![CRAN status.](http://www.r-pkg.org/badges/version/clipr)](http://www.r-pkg.org/pkg/clipr)

Simple utility functions to read and write from the system clipboards of Windows, OS X, and Linux.
Simple utility functions to read and write from the system clipboards of Windows, OS X, and Unix-like systems (which require either xclip or xsel.)

```R
devtools::install_github("mdlincoln/clipr")
Expand Down
7 changes: 6 additions & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
This is my first submission.
## Resubmission
This is a resubmission. In this version I have:

* Removed the check for a "Linux" system. Instead, if system != "Darwin" or "Windows", will assume a Unix-like system

* Reimplemented the check for 'xclip' and/or 'xsel'. The following error will still be thrown if neither program is installed: `Error: Clipboard on Unix-like systems requires 'xclip' (recommended) or 'xsel'.`

## Test environments
* local OS X install, R 3.2.2
Expand Down

0 comments on commit c5e28c4

Please sign in to comment.