<commit_before>{Range,Pane} = require 'atom'
{$$} = require 'space-pen'
_ = require 'underscore-plus'
KillRing = require './kill-ring'
DOMListener = require 'dom-listener'

module.exports =
  config:
    useLegacySearch:
      type: 'boolean'
      default: true
  killring: null
  commands: null
  eventListeners: []
  addTextEditorListener: null
  isUserCommand: true
  isearchWord: ""
  isearchLast: ""
  isearchTile: null
  isforward: true

  activate: (state) ->
    @isUserCommand = true
    @commands = atom.commands.add 'atom-text-editor',
      'emacs:cancel': => @cancel()
      'emacs:set-mark': => @setMark()
      'emacs:yank': => @yank()
      'emacs:kill': => @kill()
      'emacs:kill-region': => @killRegion()
      'emacs:copy-region': => @copyRegion()
      'emacs:kill-backward-word': => @killBackwardWord()
      'emacs:kill-region-or-backward-word': => @killRegionOrBackwardWord()
      'emacs:isearch': (e)=> @search(e,true,false)
      'emacs:backward-isearch': (e)=> @search(e,false,false)
      'emacs:isearch-regexp': (e)=> @search(e,true,true)
      'emacs:backward-isearch-regexp': (e)=> @search(e,false,true)
      'emacs:backspace': => return @backspace()

    addEditorEventListner = (editor) =>
      @eventListeners.push editor.onDidChangeCursorPosition (e)=>
        @killring?.seal() if @isUserCommand
        @deactivateISearch() if @isUserCommand
      listener = new DOMListener(atom.views.getView(editor))
      @eventListeners.push listener.add 'atom-text-editor', 'click', (e)->
        (atom.views.getView editor).classList.remove("transient-marked")

    atom.workspace.getTextEditors().forEach addEditorEventListner
    @addTextEditorListener = atom.workspace.onDidAddTextEditor (event) ->
      addEditorEventListner(event.textEditor)

    @keymapListener = atom.keymaps.onDidMatchBinding (e)=>
      if @isearchTile
        if e.keystrokes.length == 1
          @isearchWord += e.keystrokes
          @searchNext @isearchWord
        else if e.keystrokes.startsWith 'shift-'
          @isearchWord += e.keystrokes[-1..]
          @searchNext @isearchWord
        else if e.keystrokes == 'space'
          @isearchWord += ' '
          @searchNext @isearchWord
    @keymapFlistener = atom.keymaps.onDidFailToMatchBinding (e)->
      # console.log "Fail",e

    @killring = new KillRing()

  deactivate: ->
    @commands?.dispose()
    @eventListeners.forEach (listener) -> listener.dispose()
    @eventListeners = []
    @addTextEditorListener?.dispose()
    @keymapListener?.dispose()
    @keymapFlistener?.dispose()
    delete @killring

  serialize: ->
    @killring.seal()

  cancel: ->
    editor = atom.workspace.getActiveTextEditor()
    return if @deactivateISearch()
    return if @clearSelections editor
    return if @consolidateSelections editor
    atom.commands.dispatch atom.views.getView(atom.workspace), 'core:cancel'

  clearSelections: (editor)->
    return true if editor.getSelections().some (selection) ->
      unless selection.isEmpty()
        selection.clear()
      false
    false

  backspace: ()->
    if @isearchTile
      if @isearchWord.length
        @isearchWord = @isearchWord.slice 0, -1
        @searchNext @isearchWord
      else
        @deactivateISearch()
      return false
    atom.commands.dispatch document.activeElement, 'core:backspace'

  search: (e,forward,useRegex) ->
    if atom.config.get("transient-emacs.useLegacySearch")
      @activateIsearch(forward)
    else
      findAndReplace = atom.packages.getActivePackage("find-and-replace")
      if findAndReplace?.mainModule.findPanel?.isVisible()
        if forward
          atom.commands.dispatch(e.target, "find-and-replace:find-next")
        else
          atom.commands.dispatch(e.target, "find-and-replace:find-previous")
        findAndReplace?.mainModule.findView?.findEditor?.element?.focus()
      else
        findAndReplace?.mainModule.findOptions?.set 'useRegex':useRegex
        atom.packages.onDidActivatePackage (pkg)->
          if pkg.name == "find-and-replace"
            pkg.mainModule.findOptions?.set 'useRegex':useRegex
        atom.commands.dispatch(e.target, "find-and-replace:show")

  activateIsearch: (forward)-><commit_msg>Change to dispose the listener<commit_after>{Range,Pane} = require 'atom'
{$$} = require 'space-pen'
_ = require 'underscore-plus'
KillRing = require './kill-ring'
DOMListener = require 'dom-listener'

module.exports =
  config:
    useLegacySearch:
      type: 'boolean'
      default: true
  killring: null
  commands: null
  eventListeners: []
  addTextEditorListener: null
  isUserCommand: true
  isearchWord: ""
  isearchLast: ""
  isearchTile: null
  isforward: true

  activate: (state) ->
    @isUserCommand = true
    @commands = atom.commands.add 'atom-text-editor',
      'emacs:cancel': => @cancel()
      'emacs:set-mark': => @setMark()
      'emacs:yank': => @yank()
      'emacs:kill': => @kill()
      'emacs:kill-region': => @killRegion()
      'emacs:copy-region': => @copyRegion()
      'emacs:kill-backward-word': => @killBackwardWord()
      'emacs:kill-region-or-backward-word': => @killRegionOrBackwardWord()
      'emacs:isearch': (e)=> @search(e,true,false)
      'emacs:backward-isearch': (e)=> @search(e,false,false)
      'emacs:isearch-regexp': (e)=> @search(e,true,true)
      'emacs:backward-isearch-regexp': (e)=> @search(e,false,true)
      'emacs:backspace': => return @backspace()

    addEditorEventListner = (editor) =>
      @eventListeners.push editor.onDidChangeCursorPosition (e)=>
        @killring?.seal() if @isUserCommand
        @deactivateISearch() if @isUserCommand
      listener = new DOMListener(atom.views.getView(editor))
      @eventListeners.push listener.add 'atom-text-editor', 'click', (e)->
        (atom.views.getView editor).classList.remove("transient-marked")

    atom.workspace.getTextEditors().forEach addEditorEventListner
    @addTextEditorListener = atom.workspace.onDidAddTextEditor (event) ->
      addEditorEventListner(event.textEditor)

    @keymapListener = atom.keymaps.onDidMatchBinding (e)=>
      if @isearchTile
        if e.keystrokes.length == 1
          @isearchWord += e.keystrokes
          @searchNext @isearchWord
        else if e.keystrokes.startsWith 'shift-'
          @isearchWord += e.keystrokes[-1..]
          @searchNext @isearchWord
        else if e.keystrokes == 'space'
          @isearchWord += ' '
          @searchNext @isearchWord
    @keymapFlistener = atom.keymaps.onDidFailToMatchBinding (e)->
      # console.log "Fail",e

    @killring = new KillRing()

  deactivate: ->
    @commands?.dispose()
    @eventListeners.forEach (listener) -> listener.dispose()
    @eventListeners = []
    @addTextEditorListener?.dispose()
    @keymapListener?.dispose()
    @keymapFlistener?.dispose()
    delete @killring

  serialize: ->
    @killring.seal()

  cancel: ->
    editor = atom.workspace.getActiveTextEditor()
    return if @deactivateISearch()
    return if @clearSelections editor
    return if @consolidateSelections editor
    atom.commands.dispatch atom.views.getView(atom.workspace), 'core:cancel'

  clearSelections: (editor)->
    return true if editor.getSelections().some (selection) ->
      unless selection.isEmpty()
        selection.clear()
      false
    false

  backspace: ()->
    if @isearchTile
      if @isearchWord.length
        @isearchWord = @isearchWord.slice 0, -1
        @searchNext @isearchWord
      else
        @deactivateISearch()
      return false
    atom.commands.dispatch document.activeElement, 'core:backspace'

  search: (e,forward,useRegex) ->
    if atom.config.get("transient-emacs.useLegacySearch")
      @activateIsearch(forward)
    else
      findAndReplace = atom.packages.getActivePackage("find-and-replace")
      if findAndReplace?.mainModule.findPanel?.isVisible()
        if forward
          atom.commands.dispatch(e.target, "find-and-replace:find-next")
        else
          atom.commands.dispatch(e.target, "find-and-replace:find-previous")
        findAndReplace?.mainModule.findView?.findEditor?.element?.focus()
      else
        findAndReplace?.mainModule.findOptions?.set 'useRegex':useRegex
        tempListener = atom.packages.onDidActivatePackage (pkg)->
          if pkg.name == "find-and-replace"
            pkg.mainModule.findOptions?.set 'useRegex':useRegex
            tempListener.dispose()
        atom.commands.dispatch(e.target, "find-and-replace:show")

  activateIsearch: (forward)->
{'fraction_non_alphanumeric': 0.13660493098973694, 'fraction_numerical': 0.0009437300931933467, 'mean_word_length': 3.2326510234648027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, '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': '2613875', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2647, 'n_words': 459}