Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding some plugins from @optilude configs

  • Loading branch information...
commit ea59d122dafd0175803974c8616b1c1f658a0172 1 parent 047ea0a
@douglas authored
Showing with 218 additions and 25 deletions.
  1. +109 −0 clipboardHistory.py
  2. +98 −0 highlight.py
  3. +11 −25 navigationHistory.py
View
109 clipboardHistory.py
@@ -0,0 +1,109 @@
+import threading
+import sublime, sublime_plugin
+
+class HistoryList(list):
+ """List type for storing the history - fairly
+ inefficient, but useful.
+ """
+
+ SIZE = 256
+ index = 0
+
+ def append(self, item, update_index=True):
+ self.insert(0, item)
+ if update_index:
+ self.index = 0
+ if len(self) > self.SIZE:
+ del self[self.SIZE:]
+
+ def current(self):
+ if len(self) == 0:
+ return None
+ return self[self.index]
+
+ def next(self):
+ if self.index > 0:
+ self.index -= 1
+
+ def previous(self):
+ if self.index < len(self) - 1:
+ self.index += 1
+
+_LOCK = threading.RLock()
+_HISTORY = HistoryList()
+
+class ClipboardHistoryBase(sublime_plugin.TextCommand):
+
+ def update_clipboard(self, content):
+ sublime.set_clipboard(content)
+
+ def next(self):
+ with _LOCK:
+ _HISTORY.next()
+ self.update_clipboard(_HISTORY.current())
+
+ def previous(self):
+ with _LOCK:
+ _HISTORY.previous()
+ self.update_clipboard(_HISTORY.current())
+
+ def appendClipboard(self):
+ with _LOCK:
+ # append the contents of the clipboard to the history if it is unique
+ if not self.onCurrent():
+ _HISTORY.append(sublime.get_clipboard())
+
+ def onCurrent(self):
+ return sublime.get_clipboard() == _HISTORY.current()
+
+class ClipboardHistoryPaste(ClipboardHistoryBase):
+ def run(self, edit):
+ # If the user pastes something that was copied in a different program, it will not be in sublime's buffer, so we attempt to append every time
+ self.appendClipboard()
+ self.view.run_command('paste')
+
+class ClipboardHistoryPasteAndIndent(ClipboardHistoryBase):
+ def run(self, edit):
+ self.appendClipboard()
+ self.view.run_command('paste_and_indent')
+
+class ClipboardHistoryCut(ClipboardHistoryBase):
+ def run(self, edit):
+ # First run sublime's command to extract the selected text.
+ # This will set the cut/copy'd data on the clipboard which we can easily steal without recreating the cut/copy logic.
+ self.view.run_command('cut')
+ self.appendClipboard()
+
+class ClipboardHistoryCopy(ClipboardHistoryBase):
+ def run(self, edit):
+ self.view.run_command('copy')
+ self.appendClipboard()
+
+class ClipboardHistoryNext(ClipboardHistoryBase):
+ def run(self, edit):
+ self.next()
+
+class ClipboardHistoryPrevious(ClipboardHistoryBase):
+ def run(self, edit):
+ self.previous()
+
+class ClipboardHistoryPreviousAndPaste(ClipboardHistoryBase):
+ def run(self, edit):
+ self.previous()
+ self.view.run_command('paste')
+
+class ClipboardHistoryChooseAndPaste(ClipboardHistoryBase):
+ def run(self, edit):
+
+ def on_done(idx):
+ if idx >= 0:
+ with _LOCK:
+ _HISTORY.index = idx
+ self.update_clipboard(_HISTORY.current())
+ self.view.run_command('paste')
+
+ def format(line):
+ return line.replace('\n', '$ ')[:64]
+
+ lines = map(format, _HISTORY)
+ sublime.active_window().show_quick_panel(lines, on_done)
View
98 highlight.py
@@ -0,0 +1,98 @@
+import sublime, sublime_plugin
+
+key = "HighlightCurrentWord"
+
+class HighlightCurrentWord(sublime_plugin.EventListener):
+ def __init__(self):
+ self.previousRegion = sublime.Region(0, 0)
+ sublime.set_timeout(self.on_timer, 50)
+
+ def on_timer(self):
+ sublime.set_timeout(self.on_timer, 50)
+ window = sublime.active_window()
+ if window is not None:
+ self.doSearch(window.active_view(), False)
+
+ def doSearch(self, view, force=True):
+ if view == None:
+ return
+
+ selections = view.sel()
+ if len(selections) == 0:
+ view.erase_regions(key)
+ return
+
+ visibleRegion = view.visible_region()
+ if force or (self.previousRegion != visibleRegion):
+ self.previousRegion = visibleRegion
+ view.erase_regions(key)
+ else:
+ return
+
+ # The default separator does not include whitespace, so I add that here no matter what
+ separatorString = view.settings().get('word_separators') + u" \n\r"
+ themeSelector = view.settings().get('highlight_word_theme_selector', 'comment')
+
+ currentRegion = view.word(selections[0])
+ currentWord = view.substr(currentRegion).strip(separatorString) # remove leading/trailing separator characters just in case
+
+ #print u"|%s|" % currentWord
+ if(len(currentWord) == 0):
+ view.erase_regions(key)
+ return
+
+ searchStart = self.previousRegion.a - len(currentWord)
+ searchEnd = self.previousRegion.b + len(currentWord)
+
+ # Reduce m*n search to just n by mapping each word separator character into a dictionary
+ separators = {}
+ for c in separatorString:
+ separators[c] = True
+
+ # ignore the selection if it spans multiple words
+ for c in currentWord:
+ if c in separators:
+ return
+
+ # If we are multi-selecting and all the words are the same, then we should still highlight
+ if len(selections) > 1:
+ for region in selections:
+ word = view.substr(region).strip(separatorString)
+ if word != currentWord:
+ return
+
+ validRegions = []
+ while True:
+ foundRegion = view.find(currentWord, searchStart, sublime.LITERAL)
+ if foundRegion == None:
+ break
+
+ # regions can have reversed start/ends so normalize them
+ start = min(foundRegion.a, foundRegion.b)
+ end = max(foundRegion.a, foundRegion.b)
+ searchStart = end
+
+ if foundRegion.empty() or foundRegion.intersects(currentRegion):
+ continue
+
+ # check if the character before and after the region is a separator character
+ # if it is not, then the region is part of a larger word and shouldn't match
+ # this can't be done in a regex because we would be unable to use the word_separators setting string
+ leadingCharacter = view.substr(sublime.Region(start-1, start))
+ followingCharacter = view.substr(sublime.Region(end, end+1))
+ if start == 0 or leadingCharacter in separators:
+ if end == view.size()-1 or followingCharacter in separators:
+ validRegions.append(foundRegion)
+
+ if searchStart > searchEnd:
+ break
+ view.add_regions(key, validRegions, themeSelector)
+
+ def on_selection_modified(self, view):
+ self.doSearch(view)
+
+ def on_close(self, view):
+ self.doSearch(view)
+
+ def on_activated(self, view):
+ self.doSearch(view)
View
36 navigationHistory.py
@@ -1,12 +1,9 @@
-import sublime
-import sublime_plugin
-
+import sublime, sublime_plugin
from collections import deque
MAX_SIZE = 64
LINE_THRESHOLD = 2
-
class Location(object):
"""A location in the history
"""
@@ -31,18 +28,17 @@ def near(self, other):
def copy(self):
return Location(self.path, self.line, self.col)
-
class History(object):
"""Keep track of the history for a single window
"""
def __init__(self, max_size=MAX_SIZE):
- self._current = None # current location as far as the
- # history is concerned
- self._back = deque([], max_size) # items before self._current
- self._forward = deque([], max_size) # items after self._current
+ self._current = None # current location as far as the
+ # history is concerned
+ self._back = deque([], max_size) # items before self._current
+ self._forward = deque([], max_size) # items after self._current
- self._last_movement = None # last recorded movement
+ self._last_movement = None # last recorded movement
def record_movement(self, location):
"""Record movement to the given location, pushing history if
@@ -87,7 +83,7 @@ def back(self):
self._forward.appendleft(self._current)
self._current = self._back.pop()
- self._last_movement = self._current # preempt, so we don't re-push
+ self._last_movement = self._current # preempt, so we don't re-push
return self._current
def forward(self):
@@ -100,11 +96,10 @@ def forward(self):
self._back.append(self._current)
self._current = self._forward.popleft()
- self._last_movement = self._current # preempt, so we don't re-push
+ self._last_movement = self._current # preempt, so we don't re-push
return self._current
-_histories = {} # window id -> History
-
+_histories = {} # window id -> History
def get_history():
"""Get a History object for the current window,
@@ -121,7 +116,6 @@ def get_history():
_histories[window_id] = history = History()
return history
-
class NavigationHistoryRecorder(sublime_plugin.EventListener):
"""Keep track of history
"""
@@ -152,7 +146,6 @@ def on_selection_modified(self, view):
# for window_id in closed_windows:
# del _histories[window_id]
-
class NavigationHistoryBack(sublime_plugin.TextCommand):
"""Go back in history
"""
@@ -165,11 +158,7 @@ def run(self, edit):
location = history.back()
if location:
window = sublime.active_window()
- window.open_file("%s:%d:%d" % (location.path,
- location.line,
- location.col),
- sublime.ENCODED_POSITION)
-
+ window.open_file("%s:%d:%d" % (location.path, location.line, location.col), sublime.ENCODED_POSITION)
class NavigationHistoryForward(sublime_plugin.TextCommand):
"""Go forward in history
@@ -183,7 +172,4 @@ def run(self, edit):
location = history.forward()
if location:
window = sublime.active_window()
- window.open_file("%s:%d:%d" % (location.path,
- location.line,
- location.col),
- sublime.ENCODED_POSITION)
+ window.open_file("%s:%d:%d" % (location.path, location.line, location.col), sublime.ENCODED_POSITION)
Please sign in to comment.
Something went wrong with that request. Please try again.