<commit_before>/* global atom document:true */
const clipboard = atom.clipboard;
const SelectList = require('atom-select-list');

module.exports =
class KillRing {
  constructor(buffer) {
    this.buffer = buffer;
    this.sealed = true;
  }

  serialize() {
    const conf = atom.config.get('transient-emacs.killRing');
    const buffer = conf.persistent ? this.buffer : [];
    return {deserializer: 'KillRing', buffer: buffer};
  }

  put(texts, forward=true) {
    if(this.sealed)
      this.push(texts);
    else
      this.update(texts, forward);
  }

  seal() {
    this.sealed = true;
  }

  push(texts) {
    this.buffer.push(texts);
    clipboard.write(texts.join('\n'));
    const conf = atom.config.get('transient-emacs.killRing');
    while(this.buffer.length > conf.length) this.buffer.shift();
    this.sealed = false;
  }

  update(texts, forward) {
    const lasts = this.buffer.pop();
    const newTexts = texts.map((t,i) => forward ? lasts[i]+(t?t:'') : (t?t:'')+lasts[i]);
    this.push(newTexts);
  }

  top() {
    this.updateBuffer();
    return this.buffer.slice(-1)[0];
  }

  show(onSelect) {
    this.updateBuffer();
    const killRingList = new SelectList({
      items: this.buffer.map(ss => ({label: ss.join('\n'), value:ss})),
      elementForItem: item => {
        var element = document.createElement('li');
        element.innerHTML = item.label;
        return element;
      },
      filterKeyForItem: item => item.label,
      didConfirmSelection: item => {
        this.hide();
        onSelect(item);
      },
      didCancelSelection: () => this.hide()
    });
    this.lastFocusedElement = document.activeElement;
    this.panel = atom.workspace.addModalPanel({item: killRingList});
    killRingList.focus();
  }

  hide() {
    if (this.panel)
      this.panel.destroy();
    if (this.lastFocusedElement) {
      this.lastFocusedElement.focus();
      this.lastFocusedElement = null;
    }
  }

  updateBuffer() {
    const lasts = this.buffer.slice(-1)[0];
    if(clipboard.md5(lasts?lasts.join('\n'):'') != clipboard.md5(clipboard.read()))
      this.push(clipboard.read().split('\n'));
  }

};<commit_msg>Reverse direction of kill ring<commit_after>/* global atom document:true */
const clipboard = atom.clipboard;
const SelectList = require('atom-select-list');

module.exports =
class KillRing {
  constructor(buffer) {
    this.buffer = buffer;
    this.sealed = true;
  }

  serialize() {
    const conf = atom.config.get('transient-emacs.killRing');
    const buffer = conf.persistent ? this.buffer : [];
    return {deserializer: 'KillRing', buffer: buffer};
  }

  put(texts, forward=true) {
    if(this.sealed)
      this.push(texts);
    else
      this.update(texts, forward);
  }

  seal() {
    this.sealed = true;
  }

  push(texts) {
    this.buffer.unshift(texts);
    clipboard.write(texts.join('\n'));
    const conf = atom.config.get('transient-emacs.killRing');
    while(this.buffer.length > conf.length) this.buffer.pop();
    this.sealed = false;
  }

  update(texts, forward) {
    const lasts = this.buffer.shift();
    const newTexts = texts.map((t,i) => forward ? lasts[i]+(t?t:'') : (t?t:'')+lasts[i]);
    this.push(newTexts);
  }

  top() {
    this.updateBuffer();
    return this.buffer[0];
  }

  show(onSelect) {
    this.updateBuffer();
    const killRingList = new SelectList({
      items: this.buffer.map((ss,i) => ({label: `${i}\t${ss.join('\\n')}`, value:ss})),
      elementForItem: item => {
        var element = document.createElement('li');
        element.innerHTML = item.label;
        return element;
      },
      filterKeyForItem: item => item.label,
      didConfirmSelection: item => {
        this.hide();
        onSelect(item);
      },
      didCancelSelection: () => this.hide()
    });
    this.lastFocusedElement = document.activeElement;
    this.panel = atom.workspace.addModalPanel({item: killRingList});
    killRingList.focus();
  }

  hide() {
    if (this.panel)
      this.panel.destroy();
    if (this.lastFocusedElement) {
      this.lastFocusedElement.focus();
      this.lastFocusedElement = null;
    }
  }

  updateBuffer() {
    const lasts = this.buffer[0];
    if(clipboard.md5(lasts?lasts.join('\n'):'') != clipboard.md5(clipboard.read()))
      this.push(clipboard.read().split('\n'));
  }

};
{'fraction_non_alphanumeric': 0.16975451597962019, 'fraction_numerical': 0.002315886984715146, 'mean_word_length': 2.966023875114784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6369471', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1445, 'n_words': 242}