Re: Bookmark features

From: Jay <robert.jay.go....at.gmail.com>
Date: Tue, 27 Apr 2010 21:40:33 -0700 (PDT)

Here I finished the functionality for jumping between bookmarks in
different buffers
It's quite handy when working with several files

----------- start: bookmarks.lua -------------

-- Copyright 2007-2009 Mitchell Foral mitchell<att>caladbolg.net. See
LICENSE.

local textadept = _G.textadept
local locale = _G.locale

---
-- Bookmarks for the textadept module.
module('_m.textadept.bookmarks', package.seeall)
-- Markdown:
-- ## Settings
--
-- * `MARK_BOOKMARK`: The unique integer mark used to identify a
bookmarked
--    line.
-- * `MARK_BOOKMARK_COLOR`: The [Scintilla color][scintilla_color]
used for a
--    bookmarked line.
--
-- [scintilla_color]: http://scintilla.org/ScintillaDoc.html#colour
-- settings
MARK_BOOKMARK = 1
MARK_BOOKMARK_COLOR = 0xC08040
-- end settings
_bookmarks = {}
---
-- Adds a bookmark to the current line.
function add(line)
  local buffer = buffer
  if buffer.filename then
    local line = line or buffer:line_from_position(buffer.current_pos)
    if line < buffer.line_count then
      buffer:marker_add(line, MARK_BOOKMARK)
      _bookmarks[buffer.filename] = _bookmarks[buffer.filename] or {}
      _bookmarks[buffer.filename][line]=true
    end
  end
end
---
-- Clears the bookmark at the current line.
function remove(line)
  local buffer = buffer
  if buffer.filename then
    local line = line or buffer:line_from_position(buffer.current_pos)
    if line < buffer.line_count then
      buffer:marker_delete(line, MARK_BOOKMARK)
      _bookmarks[buffer.filename][line]=nil
    end
  end
end
---
-- Toggles a bookmark on the current line.
function toggle(line)
  local buffer = buffer
  local line = buffer:line_from_position(line or buffer.current_pos)
  local markers = buffer:marker_get(line) -- bit mask
  local bit = 2^MARK_BOOKMARK
  if markers % (bit + bit) < bit then add() else remove() end
end
---
-- Clears all bookmarks in the current buffer.
function clear()
  local buffer = buffer
  buffer:marker_delete_all(MARK_BOOKMARK)
  _bookmarks[buffer.filename]=nil
end
---
-- Goes to the next bookmark in the current buffer.
function goto_next(allbuffers, current_line, currentbuffer)
  local current_line = current_line or
buffer:line_from_position(buffer.current_pos)
  local line = buffer:marker_next(current_line + 1, 2^MARK_BOOKMARK)
  if line == -1 then
    if allbuffers then
	  local currentbuffer = currentbuffer or buffer.doc_pointer
	  repeat
		view:goto_buffer(1,false) --next buffer
		if goto_next(allbuffers, -1, currentbuffer) then
			return true
		end
	  until(currentbuffer == buffer.doc_pointer)
    else
      line = buffer:marker_next(0, 2^MARK_BOOKMARK)
    end
  end
  if line >= 0 then
    _m.textadept.editing.goto_line(line + 1)
	return true
  end
end
---
-- Goes to the previous bookmark in the current buffer.
function goto_prev(allbuffers, current_line, currentbuffer)
  local current_line = current_line or
buffer:line_from_position(buffer.current_pos)
  local line = buffer:marker_previous(current_line - 1,
2^MARK_BOOKMARK)
  if line == -1 then
    if allbuffers then
	  local currentbuffer = currentbuffer or buffer.doc_pointer
	  repeat
		view:goto_buffer(-1,false) --next buffer
		if goto_prev(allbuffers, buffer.line_count, currentbuffer) then
			return true
		end
	  until(currentbuffer == buffer.doc_pointer)
    else
      line = buffer:marker_previous(buffer.line_count,
2^MARK_BOOKMARK)
    end
  end
  if line >= 0 then
    _m.textadept.editing.goto_line(line + 1)
	return true
  end
end
if buffer then buffer:marker_set_back(MARK_BOOKMARK,
MARK_BOOKMARK_COLOR) end
textadept.events.add_handler('view_new',
  function() buffer:marker_set_back(MARK_BOOKMARK,
MARK_BOOKMARK_COLOR) end)
----------- end: bookmarks.lua -------------
here are my bindings for this:
	local bookmarks = _m.textadept.bookmarks
	keys['f9']  = { bookmarks.toggle	}
	keys['sf9']  = { bookmarks.clear		}
	keys['adown'] = { bookmarks.goto_next, false	}
	keys['aup'] = { bookmarks.goto_prev, false }
	keys['sadown'] = { bookmarks.goto_next, true	}
	keys['saup'] = { bookmarks.goto_prev, true }
I also finished the code to save and load (load is very unoptimized
thou) bookmarks from the session file, currently I placed the code
directly in the save/load functions of session.lua. But I'm wondering
what is the "official" pattern for this sort of functionality.
-Jay
On Apr 28, 3:47 am, Robert <ro....at.web.de> wrote:
> On Tue, Apr 27, 2010 at 11:46 AM, Jay <robert.jay.go....at.gmail.com> wrote:
> > Seems like Textadept has a halfway implemented bookmark system
> > (doesn't save, can't switch between buffers, etc...).
> > I'm still using 2.0 right now, but was wondering if anyone improved
> > the bookmark system since then, or if there are any definate plans to
> > do so?
>
> There have been quite a few changes recently but none regarding the
> bookmark system as far as I know (and searching the commit notes
> yields...).
>
>
>
> > Otherwise I think I'll do it myself.
> > BTW if no one has done it yet, and I wind up doing it, anyone has
> > bookmark feature requests?
>
> I've never used them, but maybe that would change...
>
> Robert
>
> --
> You received this message because you are subscribed to the Google Groups "textadept" group.
> To post to this group, send email to textadept.at.googlegroups.com.
> To unsubscribe from this group, send email to textadept+unsubscribe.at.googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/textadept?hl=en.
-- 
You received this message because you are subscribed to the Google Groups "textadept" group.
To post to this group, send email to textadept.at.googlegroups.com.
To unsubscribe from this group, send email to textadept+unsubscribe.at.googlegroups.com.
For more options, visit this group at http://groups.google.com/group/textadept?hl=en.
Received on Wed 28 Apr 2010 - 00:40:33 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:43:57 EST