
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  clipCopy.r                                                                             #
  #           Last Updated Funclist  :  08 Feb 2015,  5:14 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   clipPaste          ( flat=TRUE )                                                                                         #
  #   clipCopy           ( txt, sep="", undo.save=TRUE )                                                                       #
  #   cp                 ( flat=TRUE )                                                                                         #
  #   ccnms              ( x, cols=4 )                                                                                         #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

clipPaste <- function(flat=TRUE)  {
# equivalent of CMD+v piped through. 
#
# returns whatsever in the OS's clipboard
# if flat is TRUE, then it is collapsed into a single element 
#    as opposed to multiple lines
  
  con <- pipe("pbpaste", open="rb")
  ret <- readLines(con, warn=FALSE)

  if (flat)
    ret <- paste0(ret, collapse="\n")

  close(con)
  return(ret)
}

clipCopy <- function(txt, sep="", undo.save=TRUE) { 
# equivalent of highlighting txt and hitting  CMD+C 

  if (!is.character(txt))
    txt <- capture.output(txt)

  txt <- paste(txt, collapse="\n")

  if (undo.save && exists(".undo.bank"))
    .undo.bank()

  # Currently, this works only Mac OSX
  if(Sys.info()[['sysname']] != "Darwin")
    return(txt)

  con <- pipe("pbcopy", "w")
  writeLines(txt, con, sep=sep)
  close(con)

  return(invisible(txt))
}

.cp <- function(flat=TRUE) {
## This function pastes and then copies whatever is in the clipboard.
## 
## This is useful when needing to escape a long text, etc
  clipCopy(clipPaste(flat=flat))
}


## shortcut
.cc <- clipCopy

.ccnms <- function(x, cols=4) {
  ## copies the names
  nms <- names(x)
  clipCopy(paste_l(paste0('"', nms, '"'), cols=cols))
}
