#
# Snipmate Snippets for Pandoc Markdown
#
# Many snippets have starred versions, i.e., versions
# that end with an asterisk (`*`). These snippets use
# vim's `"*` register---i.e., the contents of the
# system clipboard---to insert text.

extends markdown

# Insert Title Block
snippet %%
	% ${1:`Filename('', 'title')`}
	% ${2:`g:snips_author`}
	% ${3:`strftime("%d %B %Y")`}

	${4}
snippet %%*
	% ${1:`Filename('', @*)`}
	% ${2:`g:snips_author`}
	% ${3:`strftime("%d %b %Y")`}

	${4}

# library()
snippet req
	require(${1:}, quietly = TRUE)
# If Condition
snippet if
	if ( $1 )
	{
		${2:}
	}
snippet el
	else
	{
		${1:}
	}

# Function
snippet fun
	${1:funname} <- 			# ${2:}
		function
	(
	 	${3:}
	)
	{
	  ${4:}
	}
# repeat
snippet re
	repeat{
	  ${2:}
	  if($1) break
	}

# matrix
snippet ma
	matrix(NA, nrow = ${1:}, ncol = ${2:})

# data frame
snippet df
	data.frame(${1:}, header = TRUE)

snippet cmdarg
	args <- commandArgs(TRUE)
	if (length(args) == 0)
	    stop("Please give ${1:}!")
	if (!all(file.exists(args)))
	     stop("Couln't find input files!")

snippet getopt
	require('getopt', quietly = TRUE)
	opt_spec <- matrix(c(
					'help',     'h', 0, "logical", 	"Getting help",
					'file',     'f', 1, "character","File to process"
	                ), ncol = 5, byrow = TRUE)
	opt <- getopt(spec = opt_spec)
	if ( !is.null(opt$help) || is.null(commandArgs()) )   {
	    cat(getopt(spec = opt_spec, usage = TRUE, command = "yourCmd"))
	    q(status=0)
	}
	# some inital value
	if ( is.null(opt$???) )    { opt$??? <- ??? }

snippet optparse
	require("optparse", quietly = TRUE)
	option_list <-
	    list(make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
	                     help="Print line number at the beginning of each line [default]")
	         )
	parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
	arguments <- parse_args(parser, positional_arguments = TRUE)
	opt <- arguments$options
	
	if(length(arguments$args) != 1) {
	    cat("Incorrect number of required positional arguments\n\n")
	    print_help(parser)
	    stop()
	} else {
	    file <- arguments$args
	}
	
	if( file.access(file) == -1) {
	    stop(sprintf("Specified file ( %s ) does not exist", file))
	} else {
	    file_text <- readLines(file)
	}

snippet #!
	#!/usr/bin/env Rscript

snippet debug
	# Development & Debugging, don't forget to uncomment afterwards!
	#--------------------------------------------------------------------------------
	#setwd("~/Projekte/${1:}")
	#opt <- list(${2:}
	#            )
	#--------------------------------------------------------------------------------


# Took from pandoc-plugin <<<<
# Underline with `=`s or `-`s
snippet #===
	#`repeat('=', strlen(getline(line(".") - 1)))`
	${1}
snippet #---
	#`repeat('-', strlen(getline(line(".") - 1)))`
	${1}

# >>>>

snippet r
	\`\`\`{r ${1:chung_tag}, echo = FALSE ${2:options}}
	${3:}
	\`\`\`
snippet ri
	\`{r ${1:}}\`

snippet copt
	\`\`\` {r setup, echo = FALSE}
		opts_chunk$set(fig.path='../figures/${1:}', cache.path='../cache/-'
		, fig.align='center', fig.show='hold', par=TRUE)	
		#opts_knit$set(upload.fun = imgur_upload) # upload images
	\`\`\`

	
# End of File ===================================================================
# vim: set noexpandtab:
