<commit_before>
Marks =
  marks: {}

  # This returns the key which is used for storing mark locations in chrome.storage.local.
  getLocationKey: (markName) -> "vimiumGlobalMark|#{markName}"

  create: (req, sender) ->
    chrome.storage.local.get "vimiumSecret", (items) =>
      item = {}
      item[@getLocationKey req.markName] =
        vimiumSecret: items.vimiumSecret
        tabId: sender.tab.id
        url: sender.tab.url
        scrollX: req.scrollX
        scrollY: req.scrollY
      console.log item
      chrome.storage.local.set item

  goto: (req, sender) ->
    key = @getLocationKey req.markName
    chrome.storage.local.get [ "vimiumSecret", key ], (items) =>
      markInfo = items[key]
      if not markInfo
        # The mark is not defined.
        chrome.tabs.sendMessage sender.tab.id,
          name: "showHUDforDuration",
          text: "Global mark not set: '#{req.markName}'."
          duration: 1000
      else if markInfo.vimiumSecret != items.vimiumSecret
        # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date.
        @focusOrLaunch markInfo
      else
        # Check whether markInfo.tabId still exists.
        { tabId, url, scrollX, scrollY } = markInfo
        chrome.tabs.get tabId, (tab) =>
          if chrome.runtime.lastError or not tab
            # The tab no longer exists.
            @focusOrLaunch markInfo
          else
            # The original tab still exists.
            chrome.tabs.update tabId, { selected: true }, ->
              chrome.tabs.sendMessage tabId,
                { name: "setScrollPosition", scrollX: scrollX, scrollY: scrollY }, ->
                  chrome.tabs.sendMessage tabId,
                    name: "showHUDforDuration",
                    text: "Jumped to global mark '#{req.markName}'."
                    duration: 1000

  # The tab we're trying to find no longer exists.  Either find another tab with a matching URL and use it, or
  # create a new tab.
  focusOrLaunch: (info) ->
    console.log info

root = exports ? window
root.Marks = Marks
<commit_msg>Global marks; find another (existing) tab if tabId has been removed.<commit_after>
Marks =
  marks: {}

  # This returns the key which is used for storing mark locations in chrome.storage.local.
  getLocationKey: (markName) -> "vimiumGlobalMark|#{markName}"

  create: (req, sender) ->
    chrome.storage.local.get "vimiumSecret", (items) =>
      item = {}
      item[@getLocationKey req.markName] =
        vimiumSecret: items.vimiumSecret
        tabId: sender.tab.id
        url: sender.tab.url
        scrollX: req.scrollX
        scrollY: req.scrollY
        markName: req.markName
      chrome.storage.local.set item

  goto: (req, sender) ->
    key = @getLocationKey req.markName
    chrome.storage.local.get [ "vimiumSecret", key ], (items) =>
      markInfo = items[key]
      if not markInfo
        # The mark is not defined.
        chrome.tabs.sendMessage sender.tab.id,
          name: "showHUDforDuration",
          text: "Global mark not set: '#{req.markName}'."
          duration: 1000
      else if markInfo.vimiumSecret != items.vimiumSecret
        # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date.
        @focusOrLaunch markInfo
      else
        # Check whether markInfo.tabId still exists.
        chrome.tabs.get markInfo.tabId, (tab) =>
          if chrome.runtime.lastError or not tab
            # The original tab no longer exists.
            @focusOrLaunch markInfo
          else
            # The original tab still exists.
            @gotoPositionInTab markInfo

  gotoPositionInTab: ({ tabId, scrollX, scrollY, markName }) ->
    chrome.tabs.update tabId, { selected: true }, ->
      chrome.tabs.sendMessage tabId,
        { name: "setScrollPosition", scrollX: scrollX, scrollY: scrollY }, ->
          chrome.tabs.sendMessage tabId,
            name: "showHUDforDuration",
            text: "Jumped to global mark '#{markName}'."
            duration: 1000

  # The tab we're trying to find no longer exists.  Either find another tab with a matching URL and use it, or
  # create a new tab.
  focusOrLaunch: (markInfo) ->
    chrome.windows.getAll { populate: true }, (windows) =>
      baseUrl = @getBaseUrl markInfo.url
      for window in windows
        for tab in window.tabs
          if baseUrl == @getBaseUrl tab.url
            # We have a matching tab.  We'll use it.
            return @gotoPositionInTab extend markInfo, tabId: tab.id

  getBaseUrl: (url) -> url.split("#")[0]

root = exports ? window
root.Marks = Marks

{'fraction_non_alphanumeric': 0.0972644376899696, 'fraction_numerical': 0.0036908380373425966, 'mean_word_length': 2.2650602409638556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '7126076', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1303, 'n_words': 413}