<commit_before>"
"
" File: simplenote.vim
" Author: <NAME> <<EMAIL>>
" WebPage: http://github.com/mrtazz/simplenote.vim
" License: MIT
" Usage:
"
"
"

if &cp || (exists('g:loaded_simplenote_vim') && g:loaded_simplenote_vim)
  finish
endif
let g:loaded_simplenote_vim = 1

" check for python
if !has("python")
  echoerr "Simplenote: Plugin needs vim to be compiled with python support."
  finish
endif

" user auth settings
let s:user = ""
let s:password = ""

let s:user = g:SimpleNoteUserName
let s:password = g:SimpleNotePassword

if (s:user == "") || (s:password == "")
  echoerr "No valid username or password."
  finish
endif

"
" Helper functions
"

" the id of the note currently edited is stored in a global variable
let g:simplenote_current_note_id = ""

" Everything is displayed in a scratch buffer named SimpleNote
let g:simplenote_scratch_buffer = 'SimpleNote'

" Function that opens or navigates to the scratch buffer.
function! s:ScratchBufferOpen(name)

    let scr_bufnum = bufnr(a:name)
    if scr_bufnum == -1
        exe "new " . a:name
    else
        let scr_winnum = bufwinnr(scr_bufnum)
        if scr_winnum != -1
            if winnr() != scr_winnum
                exe scr_winnum . "wincmd w"
            endif
        else
            exe "split +buffer" . scr_bufnum
        endif
    endif
    call ScratchBuffer()
endfunction

" After opening the scratch buffer, this sets some properties for it.
function! ScratchBuffer()
    setlocal buftype=nofile
    setlocal bufhidden=hide
    setlocal noswapfile
    setlocal buflisted
    setlocal cursorline
    setlocal filetype=txt
endfunction


"
" python functions
"

python << ENDPYTHON
import vim
import urllib2
import base64
import json

AUTH_URL = 'https://simple-note.appspot.com/api/login'
DATA_URL = 'https://simple-note.appspot.com/api2/data/'
INDX_URL = 'https://simple-note.appspot.com/api2/index?'
DEFAULT_SCRATCH_NAME = vim.eval("g:simplenote_scratch_buffer")

def scratch_buffer(sb_name = DEFAULT_SCRATCH_NAME):
    """ Opens a scratch buffer from python """
    vim.command("call s:ScratchBufferOpen('%s')" % sb_name)

#
# @brief function to get simplenote auth token
#
# @param user -> simplenote email address
# @param password -> <PASSWORD>
#
# @return simplenote API token
#
def simple_note_auth(user, password):
    auth_params = "email=%s&password=%s" % (user, password)
    values = base64.encodestring(auth_params)
    request = urllib2.Request(AUTH_URL, values)
    try:
        token = urllib2.urlopen(request).read()
    except IOError, e: # no connection exception
        token = None
    return token

#
# @brief function to get a specific note
#
# @param user -> simplenote username
# @param token -> simplenote API token
# @param noteid -> ID of the note to get
#
# @return content of the desired note

def get_note(user, token, noteid):
    # request note
    params = '%s?auth=%s&email=%s' % (str(noteid), token, user)
    request = urllib2.Request(DATA_URL+params)
    try:
        response = urllib2.urlopen(request)
    except IOError, e:
        return None
    note = json.loads(response.read())
    return note["content"]

#
# @brief function to update a specific note
#
# @param user -> simplenote username
# @param token -> simplenote API token
# @param noteid -> noteid to update
# @param content -> content of the note to update
#
# @return
#
def update_note(user, token, noteid, content):
    params = '%s?auth=%s&email=%s' % (noteid, token, user)
    noteobject = {}
    noteobject["content"] = content
    note = json.dumps(noteobject)
    values = urllib.urlencode(note)
    request = urllib2.Request(DATA_URL+params, values)
    try:
        response = urllib2.urlopen(request)
    except IOError, e:
        return False
    return True

#
# @brief function to get the note list
#
# @param user -> simplenote username
# @param token -> simplenote API token
#
# @return list of note titles and success status
#
def get_note_list(user, token):
    params = 'auth=%s&email=%s' % (token, user)
    request = urllib2.Request(INDX_URL+params)
    status = 0
    try:
      response = json.loads(urllib2.urlopen(request).read())
    except IOError, e:
      status = -1
      response = { "data" : [] }
    ret = []
    # parse data fields in response
    for d in response["data"]:
        ret.append(d["key"])

    return ret, status

# retrieve a token to interact with the API
SN_USER = vim.eval("s:user")
SN_TOKEN = simple_note_auth(SN_USER, vim.eval("s:password"))

ENDPYTHON

"
" interface functions
"


" function to get a note and display in current buffer
function! s:GetNoteToCurrentBuffer()
python << EOF
line = vim.current.line
vim.eval(""" let g:simplenote_current_note_id="%s" """ % line)
note = get_note(SN_USER, SN_TOKEN, line)
buffer = vim.current.buffer
del buffer[:]
buffer.append(note)
EOF
endfunction

" function to update the note from the current buffer
function! s:UpdateNoteFromCurrentBuffer()
python << EOF
note_id = vim.eval("g:simplenote_current_note_id")
content = vim.current.buffer[:]
result = update_note(SN_USER, SN_TOKEN, note_id, content)
if result == True:
    print "Update successful."
else:
    print "Update failed."
EOF
endfunction

function! s:SimpleNote(param)
python << EOF
param = vim.eval("a:param")
if param == "-l":
    # Initialize the scratch buffer
    scratch_buffer()
    del vim.current.buffer[:]
    buffer = vim.current.buffer
    notes, status = get_note_list(SN_USER, SN_TOKEN)
    if status == 0:
        for n in notes:
            # get note from server
            note = get_note(SN_USER, SN_TOKEN, n)
            # fetch first line and display as title
            note_lines = note.split("\n")
            if len(note_lines) > 0:
                title = "%s[%s]" % (note_lines[0],n)
            else:
                title = "%s[%s]" % (n,n)

            buffer.append(str(title))
    else:
        print "Error: Unable to connect to server."

    # map <CR> to call get_note()
    vim.command("map <buffer> <CR> <Esc>:call GetNoteToCurrentBuffer()<CR>")

elif param == "-d":
    print "Delete note"
elif param == "-u":
    print "Update note"
else:<commit_msg>choose note from list and display in buffer<commit_after>"
"
" File: simplenote.vim
" Author: <NAME> <<EMAIL>>
" WebPage: http://github.com/mrtazz/simplenote.vim
" License: MIT
" Usage:
"
"
"

if &cp || (exists('g:loaded_simplenote_vim') && g:loaded_simplenote_vim)
  finish
endif
let g:loaded_simplenote_vim = 1

" check for python
if !has("python")
  echoerr "Simplenote: Plugin needs vim to be compiled with python support."
  finish
endif

" user auth settings
let s:user = ""
let s:password = ""

let s:user = g:SimpleNoteUserName
let s:password = g:SimpleNotePassword

if (s:user == "") || (s:password == "")
  echoerr "No valid username or password."
  finish
endif

"
" Helper functions
"

" the id of the note currently edited is stored in a global variable
let g:simplenote_current_note_id = ""

" Everything is displayed in a scratch buffer named SimpleNote
let g:simplenote_scratch_buffer = 'SimpleNote'

" Function that opens or navigates to the scratch buffer.
function! s:ScratchBufferOpen(name)

    let scr_bufnum = bufnr(a:name)
    if scr_bufnum == -1
        exe "new " . a:name
    else
        let scr_winnum = bufwinnr(scr_bufnum)
        if scr_winnum != -1
            if winnr() != scr_winnum
                exe scr_winnum . "wincmd w"
            endif
        else
            exe "split +buffer" . scr_bufnum
        endif
    endif
    call ScratchBuffer()
endfunction

" After opening the scratch buffer, this sets some properties for it.
function! ScratchBuffer()
    setlocal buftype=nofile
    setlocal bufhidden=hide
    setlocal noswapfile
    setlocal buflisted
    setlocal cursorline
    setlocal filetype=txt
endfunction


"
" python functions
"

python << ENDPYTHON
import vim
import urllib2
import base64
import json

AUTH_URL = 'https://simple-note.appspot.com/api/login'
DATA_URL = 'https://simple-note.appspot.com/api2/data/'
INDX_URL = 'https://simple-note.appspot.com/api2/index?'
DEFAULT_SCRATCH_NAME = vim.eval("g:simplenote_scratch_buffer")

def scratch_buffer(sb_name = DEFAULT_SCRATCH_NAME):
    """ Opens a scratch buffer from python """
    vim.command("call s:ScratchBufferOpen('%s')" % sb_name)

#
# @brief function to get simplenote auth token
#
# @param user -> simplenote email address
# @param password -> <PASSWORD>
#
# @return simplenote API token
#
def simple_note_auth(user, password):
    auth_params = "email=%s&password=%s" % (user, password)
    values = base64.encodestring(auth_params)
    request = urllib2.Request(AUTH_URL, values)
    try:
        token = urllib2.urlopen(request).read()
    except IOError, e: # no connection exception
        token = None
    return token

#
# @brief function to get a specific note
#
# @param user -> simplenote username
# @param token -> simplenote API token
# @param noteid -> ID of the note to get
#
# @return the desired note

def get_note(user, token, noteid):
    # request note
    params = '%s?auth=%s&email=%s' % (str(noteid), token, user)
    request = urllib2.Request(DATA_URL+params)
    try:
        response = urllib2.urlopen(request)
    except IOError, e:
        return None
    note = json.loads(response.read())
    return note

#
# @brief function to update a specific note
#
# @param user -> simplenote username
# @param token -> simplenote API token
# @param noteid -> noteid to update
# @param content -> content of the note to update
#
# @return
#
def update_note(user, token, noteid, content):
    params = '%s?auth=%s&email=%s' % (noteid, token, user)
    noteobject = {}
    noteobject["content"] = content
    note = json.dumps(noteobject)
    values = urllib.urlencode(note)
    request = urllib2.Request(DATA_URL+params, values)
    try:
        response = urllib2.urlopen(request)
    except IOError, e:
        return False
    return True

#
# @brief function to get the note list
#
# @param user -> simplenote username
# @param token -> simplenote API token
#
# @return list of note titles and success status
#
def get_note_list(user, token):
    params = 'auth=%s&email=%s' % (token, user)
    request = urllib2.Request(INDX_URL+params)
    status = 0
    try:
      response = json.loads(urllib2.urlopen(request).read())
    except IOError, e:
      status = -1
      response = { "data" : [] }
    ret = []
    # parse data fields in response
    for d in response["data"]:
        ret.append(d["key"])

    return ret, status

# retrieve a token to interact with the API
SN_USER = vim.eval("s:user")
SN_TOKEN = simple_note_auth(SN_USER, vim.eval("s:password"))

ENDPYTHON

"
" interface functions
"


" function to get a note and display in current buffer
function! s:GetNoteToCurrentBuffer()
python << EOF
# get the notes id which is shown in brackets in the current line
line = vim.current.line.split("[")[-1].split("]")[0]
# store it as a global script variable
vim.command(""" let g:simplenote_current_note_id="%s" """ % line)
note = get_note(SN_USER, SN_TOKEN, line)
buffer = vim.current.buffer
# remove cursorline
vim.command("setlocal nocursorline")
del buffer[:]
for noteline in note["content"].split("\n"):
    buffer.append(str(noteline))
EOF
endfunction

" function to update the note from the current buffer
function! s:UpdateNoteFromCurrentBuffer()
python << EOF
note_id = vim.eval("g:simplenote_current_note_id")
content = vim.current.buffer[:]
result = update_note(SN_USER, SN_TOKEN, note_id, content)
if result == True:
    print "Update successful."
else:
    print "Update failed."
EOF
endfunction

function! s:SimpleNote(param)
python << EOF
param = vim.eval("a:param")
if param == "-l":
    # Initialize the scratch buffer
    scratch_buffer()
    del vim.current.buffer[:]
    buffer = vim.current.buffer
    notes, status = get_note_list(SN_USER, SN_TOKEN)
    if status == 0:
        for n in notes:
            # get note from server
            note = get_note(SN_USER, SN_TOKEN, n)
            # fetch first line and display as title
            note_lines = note["content"].split("\n")
            if len(note_lines) > 0:
                title = "%s  [%s]" % (note_lines[0],n)
            else:
                title = "%s  [%s]" % (n,n)

            buffer.append(str(title))
    else:
        print "Error: Unable to connect to server."

    # map <CR> to call get_note()
    vim.command("map <buffer> <CR> <Esc>:call <SID>GetNoteToCurrentBuffer()<CR>")

elif param == "-d":
    print "Delete note"
elif param == "-u":
    print "Update note"
else:
{'fraction_non_alphanumeric': 0.11708558701877188, 'fraction_numerical': 0.003818008272351257, 'mean_word_length': 3.3550398337374436, 'pattern_counts': {'":': 6, '<': 38, '<?xml version=': 0, '>': 46, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4379441', 'n_tokens_mistral': 4197, 'n_tokens_neox': 4128, 'n_words': 1196}