Example #1
0
    def __init__(self, source):
        nchars = int(vim.eval("g:pad_read_nchars_from_files"))
        self.summary = ""
        self.body = ""
        self.isEmpty = True
        self.id = timestamp()

        if source is vim.current.buffer:
            source = source[:10]
        elif source.__class__ == file:
            source = source.read(nchars).split("\n")

        data = [line.strip() for line in source if line != ""]

        if data != []:
            # we discard modelines
            if re.match("^.* vim: set .*:.*$", data[0]):
                data = data[1:]

            self.summary = data[0].strip()
            if self.summary[0] in ("%", "#"):  # pandoc and markdown titles
                self.summary = str(self.summary[1:]).strip()

            self.body = u"\u21b2".encode("utf-8").join(data[1:]).strip()

        if self.summary != "":
            self.isEmpty = False
            self.id = self.summary.lower().replace(" ", "_")
Example #2
0
    def __init__(self, source):
        """

        source can be:

        * a vim buffer
        * a file object
        * a list of strings, one per line
        """

        nchars = int(vim.eval("g:pad_read_nchars_from_files"))
        self.summary = ""
        self.body = ""
        self.isEmpty = True
        self.folder = ""
        self.id = timestamp()

        if source is vim.current.buffer:
            source = source[:10]
        elif source.__class__ == file:
            pos = len(get_save_dir()), len(basename(source.name))
            self.folder = abspath(source.name)[pos[0]:-pos[1]]
            source = source.read(nchars).split("\n")

        data = [line.strip() for line in source if line != ""]

        if data != []:
            # we discard modelines
            if re.match("^.* vim: set .*:.*$", data[0]):
                data = data[1:]

            self.summary = data[0].strip()
            # vim-orgmode adds tags after whitespace
            org_tags_data = re.search("\s+(?P<tags>:.*$)", self.summary)
            if org_tags_data:
                self.summary = re.sub("\s+:.*$", "", self.summary)
            if self.summary[0] in ("%", "#"):  # pandoc and markdown titles
                self.summary = str(self.summary[1:]).strip()

            self.body = u'\u21b2'.encode('utf-8').join(data[1:]).strip()
            # if we have orgmode tag data, add it to the body
            if org_tags_data:
                self.body = ' '.join(\
                    [" ".join(\
                              map(lambda a: "@" + a, \
                                  filter(lambda a: a != "", \
                                         org_tags_data.group("tags").split(":")))), \
                     self.body])
            # remove extra spaces in bodies
            self.body = re.sub("\s{2,}", "", self.body)

        if self.summary != "":
            self.isEmpty = False
            self.id = self.summary.lower().replace(" ", "_")
            # remove ilegal characters from names (using rules for windows
            # systems to err on the side of precaution)
            self.id = re.sub("[*:<>/\|^]", "", self.id)

        if self.id.startswith("."):
            self.id = re.sub("^\.*", "", self.id)
Example #3
0
	def __init__(self, source):
		nchars = int(vim.eval("g:pad_read_nchars_from_files"))
		self.summary = ""
		self.body = ""
		self.isEmpty = True
		self.folder = ""
		self.id = timestamp()

		if source is vim.current.buffer:
			source = source[:10]
		elif source.__class__ == file:
			self.folder = abspath(source.name)[len(get_save_dir()):-len(basename(source.name))]
			source = source.read(nchars).split("\n")

		data = [line.strip() for line in source if line != ""]

		if data != []:
			# we discard modelines
			if re.match("^.* vim: set .*:.*$", data[0]):
				data = data[1:]

			self.summary = data[0].strip()
			if self.summary[0] in ("%", "#"): #pandoc and markdown titles
				self.summary = str(self.summary[1:]).strip()

			self.body = u'\u21b2'.encode('utf-8').join(data[1:]).strip()

		if self.summary != "":
			self.isEmpty = False
			self.id = self.summary.lower().replace(" ", "_")

		if self.id.startswith("."):
			self.id = re.sub("^\.*", "", self.id)
Example #4
0
def open_pad(path=None, first_line=None): #{{{1
	"""Creates or opens a note.

	path: a valid path for a note.

	first_line: a string to insert to a new note, if given.
	"""
	# we require self.save_dir_set to be set to a valid path
	if get_save_dir() == "":
		vim.command('let tmp = confirm("IMPORTANT:\n'\
				'Please set g:pad_dir to a valid path in your vimrc.", "OK", 1, "Error")')
		return
	
	# if no path is provided, we create one using the current time
	if not path:
		path = join(get_save_dir(), timestamp())

	vim.command("silent! botright" + str(vim.eval("g:pad_window_height")) + "split " + path)
	
	# set the filetype to our default
	if vim.eval('&filetype') in ('', 'conf'):
		vim.command("set filetype=" + vim.eval("g:pad_default_format"))
	
	# map the local commands
	if bool(int(vim.eval('has("gui_running")'))):
		vim.command("noremap <silent> <buffer> <localleader><delete> :call pad#DeleteThis()<cr>")
	else:
		vim.command("noremap <silent> <buffer> <localleader>dd :call pad#DeleteThis()<cr>")

	vim.command("noremap <silent> <buffer> <localleader>+m :call pad#AddModeline()<cr>")
	
	# insert the text in first_line to the buffer, if provided
	if first_line:
		vim.current.buffer.append(first_line,0)
		vim.command("normal! j")
Example #5
0
    def __init__(self, source):
        """

        source can be:

        * a vim buffer
        * a file object
        * a list of strings, one per line
        """

        nchars = int(vim.eval("g:pad_read_nchars_from_files"))
        self.summary = ""
        self.body = ""
        self.isEmpty = True
        self.folder = ""
        self.id = timestamp()

        if source is vim.current.buffer:
            source = source[:10]
        elif source.__class__ == file:
            pos = len(get_save_dir()), len(basename(source.name))
            self.folder = abspath(source.name)[pos[0]:-pos[1]]
            source = source.read(nchars).split("\n")

        data = [line.strip() for line in source if line != ""]

        if data != []:
            # we discard modelines
            if re.match("^.* vim: set .*:.*$", data[0]):
                data = data[1:]

            self.summary = data[0].strip()
            # vim-orgmode adds tags after whitespace
            org_tags_data = re.search("\s+(?P<tags>:.*$)", self.summary)
            if org_tags_data:
                self.summary = re.sub("\s+:.*$", "", self.summary)
            if self.summary[0] in ("%", "#"):  # pandoc and markdown titles
                self.summary = str(self.summary[1:]).strip()

            self.body = u'\u21b2'.encode('utf-8').join(data[1:]).strip()
            # if we have orgmode tag data, add it to the body
            if org_tags_data:
                self.body = ' '.join(\
                    [" ".join(\
                              map(lambda a: "@" + a, \
                                  filter(lambda a: a != "", \
                                         org_tags_data.group("tags").split(":")))), \
                     self.body])
            # remove extra spaces in bodies
            self.body = re.sub("\s{2,}", "", self.body)

        if self.summary != "":
            self.isEmpty = False
            self.id = self.summary.lower().replace(" ", "_")
            # remove ilegal characters from names (using rules for windows
            # systems to err on the side of precaution)
            self.id = re.sub("[*:<>/\|^]", "", self.id)

        if self.id.startswith("."):
            self.id = re.sub("^\.*", "", self.id)
Example #6
0
def open_pad(path=None, first_line=None):  # {{{1
    """Creates or opens a note.

    path: a valid path for a note.

    first_line: a string to insert to a new note, if given.
    """
    # we require self.save_dir_set to be set to a valid path
    if get_save_dir() == "":
        vim.command('let tmp = confirm("IMPORTANT:\n'
                'Please set g:pad_dir to a valid path in your vimrc.",'
                ' "OK", 1, "Error")')
        return

    # if no path is provided, we create one using the current time
    if not path:
        path = join(get_save_dir(),
                    timestamp() + vim.eval("g:pad_default_file_extension"))
    path = path.replace(" ", "\ ")

    if bool(int(vim.eval("g:pad_open_in_split"))):
        if vim.eval('g:pad_position["pads"]') == 'right':
            vim.command("silent! rightbelow"
                    + str(vim.eval("g:pad_window_width")) + "vsplit " + path)
        else:
            vim.command("silent! botright"
                    + str(vim.eval("g:pad_window_height")) + "split " + path)
    else:
        vim.command("silent! edit " + path)

    # we don't keep the buffer when we hide it
    vim.command("set bufhidden=wipe")

    # set the filetype to our default
    if vim.eval('&filetype') in ('', 'conf'):
        vim.command("set filetype=" + vim.eval("g:pad_default_format"))

    # map the local commands
    if bool(int(vim.eval('has("gui_running")'))):
        vim.command("noremap <silent> <buffer> <localleader><delete> :call pad#DeleteThis()<cr>")
    else:
        vim.command("noremap <silent> <buffer> <localleader>dd :call pad#DeleteThis()<cr>")

    vim.command("noremap <silent> <buffer> <localleader>+m :call pad#AddModeline()<cr>")
    vim.command("noremap <silent> <buffer> <localleader>+f :call pad#MoveToFolder()<cr>")
    vim.command("noremap <silent> <buffer> <localleader>-f :call pad#MoveToSaveDir()<cr>")
    vim.command("noremap <silent> <buffer> <localleader>+a :call pad#Archive()<cr>")
    vim.command("noremap <silent> <buffer> <localleader>-a :call pad#Unarchive()<cr>")

    # insert the text in first_line to the buffer, if provided
    if first_line:
        vim.current.buffer.append(first_line, 0)
        vim.command("normal! j")
Example #7
0
def protect(id):
    """ Prevent filename collisions
    """
    return id + "." + base36encode(int(timestamp()))
Example #8
0
def protect(id):
    """ Prevent filename collisions
    """
    return id + "." + base36encode(int(timestamp()))