diff --git a/package.json b/package.json index 94a73828d..b11f3f1ed 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "feedback": "0.22.0", "find-and-replace": "0.81.0", "fuzzy-finder": "0.32.0", - "gists": "0.15.0", + "gists": "0.16.0", "git-diff": "0.23.0", "github-sign-in": "0.18.0", "go-to-line": "0.16.0", @@ -89,7 +89,7 @@ "metrics": "0.24.0", "package-generator": "0.25.0", "release-notes": "0.17.0", - "settings-view": "0.64.0", + "settings-view": "0.65.0", "snippets": "0.24.0", "spell-check": "0.21.0", "status-bar": "0.32.0", @@ -98,8 +98,8 @@ "tabs": "0.18.0", "terminal": "0.27.0", "timecop": "0.13.0", - "to-the-hubs": "0.18.0", - "tree-view": "0.66.0", + "to-the-hubs": "0.19.0", + "tree-view": "0.67.0", "update-package-dependencies": "0.2.0", "visual-bell": "0.6.0", "welcome": "0.4.0", diff --git a/spec/clipboard-spec.coffee b/spec/clipboard-spec.coffee new file mode 100644 index 000000000..0553f0eae --- /dev/null +++ b/spec/clipboard-spec.coffee @@ -0,0 +1,12 @@ +describe "Clipboard", -> + describe "write(text, metadata) and read()", -> + it "writes and reads text to/from the native clipboard", -> + expect(atom.clipboard.read()).toBe 'initial clipboard content' + atom.clipboard.write('next') + expect(atom.clipboard.read()).toBe 'next' + + it "returns metadata if the item on the native clipboard matches the last written item", -> + atom.clipboard.write('next', {meta: 'data'}) + expect(atom.clipboard.read()).toBe 'next' + expect(atom.clipboard.readWithMetadata().text).toBe 'next' + expect(atom.clipboard.readWithMetadata().metadata).toEqual {meta: 'data'} diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index ecd6e6d7a..8d5e05d9a 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1856,12 +1856,12 @@ describe "Editor", -> expect(editor.getCursorBufferPosition()).toEqual [0, 2] expect(editor.getCursorScreenPosition()).toEqual [0, editor.getTabLength() * 2] - describe "pasteboard operations", -> + describe "clipboard operations", -> beforeEach -> editor.setSelectedBufferRanges([[[0, 4], [0, 13]], [[1, 6], [1, 10]]]) describe ".cutSelectedText()", -> - it "removes the selected text from the buffer and places it on the pasteboard", -> + it "removes the selected text from the buffer and places it on the clipboard", -> editor.cutSelectedText() expect(buffer.lineForRow(0)).toBe "var = function () {" expect(buffer.lineForRow(1)).toBe " var = function(items) {" @@ -1885,7 +1885,7 @@ describe "Editor", -> editor.cutToEndOfLine() expect(buffer.lineForRow(2)).toBe ' if (items.length' expect(buffer.lineForRow(3)).toBe ' var pivot = item' - expect(atom.pasteboard.read()[0]).toBe ' <= 1) return items;\ns.shift(), current, left = [], right = [];' + expect(atom.clipboard.read()).toBe ' <= 1) return items;\ns.shift(), current, left = [], right = [];' describe "when text is selected", -> it "only cuts the selected text, not to the end of the line", -> @@ -1895,7 +1895,7 @@ describe "Editor", -> expect(buffer.lineForRow(2)).toBe ' if (items.lengthurn items;' expect(buffer.lineForRow(3)).toBe ' var pivot = item' - expect(atom.pasteboard.read()[0]).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];' + expect(atom.clipboard.read()).toBe ' <= 1) ret\ns.shift(), current, left = [], right = [];' describe ".copySelectedText()", -> it "copies selected text onto the clipboard", -> @@ -1906,7 +1906,7 @@ describe "Editor", -> describe ".pasteText()", -> it "pastes text into the buffer", -> - atom.pasteboard.write('first') + atom.clipboard.write('first') editor.pasteText() expect(editor.buffer.lineForRow(0)).toBe "var first = function () {" expect(buffer.lineForRow(1)).toBe " var first = function(items) {" diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 23804de70..b63079d11 100644 --- a/spec/editor-view-spec.coffee +++ b/spec/editor-view-spec.coffee @@ -2515,9 +2515,9 @@ describe "EditorView", -> expect(edited).toBe false describe "when editor:copy-path is triggered", -> - it "copies the absolute path to the editor view's file to the pasteboard", -> + it "copies the absolute path to the editor view's file to the clipboard", -> editorView.trigger 'editor:copy-path' - expect(atom.pasteboard.read()[0]).toBe editor.getPath() + expect(atom.clipboard.read()).toBe editor.getPath() describe "when editor:move-line-up is triggered", -> describe "when there is no selection", -> diff --git a/spec/pasteboard-spec.coffee b/spec/pasteboard-spec.coffee deleted file mode 100644 index 467418204..000000000 --- a/spec/pasteboard-spec.coffee +++ /dev/null @@ -1,10 +0,0 @@ -describe "Pasteboard", -> - describe "write(text, metadata) and read()", -> - it "writes and reads text to/from the native pasteboard", -> - expect(atom.pasteboard.read()).toEqual ['initial pasteboard content'] - atom.pasteboard.write('next') - expect(atom.pasteboard.read()[0]).toBe 'next' - - it "returns metadata if the item on the native pasteboard matches the last written item", -> - atom.pasteboard.write('next', {meta: 'data'}) - expect(atom.pasteboard.read()).toEqual ['next', {meta: 'data'}] diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 8e939d03d..c93dcf4fd 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -95,9 +95,9 @@ beforeEach -> TokenizedBuffer.prototype.chunkSize = Infinity spyOn(TokenizedBuffer.prototype, "tokenizeInBackground").andCallFake -> @tokenizeNextChunk() - pasteboardContent = 'initial pasteboard content' - spyOn(clipboard, 'writeText').andCallFake (text) -> pasteboardContent = text - spyOn(clipboard, 'readText').andCallFake -> pasteboardContent + clipboardContent = 'initial clipboard content' + spyOn(clipboard, 'writeText').andCallFake (text) -> clipboardContent = text + spyOn(clipboard, 'readText').andCallFake -> clipboardContent addCustomMatchers(this) diff --git a/src/atom.coffee b/src/atom.coffee index 3cc30c31e..fb9488452 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -28,7 +28,7 @@ WindowEventHandler = require './window-event-handler' # * `atom.menu` - A {MenuManager} instance # * `atom.workspaceView` - A {WorkspaceView} instance # * `atom.packages` - A {PackageManager} instance -# * `atom.pasteboard` - A {Pasteboard} instance +# * `atom.clipboard` - A {Clipboard} instance # * `atom.project` - A {Project} instance # * `atom.syntax` - A {Syntax} instance # * `atom.themes` - A {ThemeManager} instance @@ -134,7 +134,7 @@ class Atom extends Model Config = require './config' Keymap = require './keymap' PackageManager = require './package-manager' - Pasteboard = require './pasteboard' + Clipboard = require './clipboard' Syntax = require './syntax' ThemeManager = require './theme-manager' ContextMenuManager = require './context-menu-manager' @@ -148,7 +148,8 @@ class Atom extends Model @themes = new ThemeManager({packageManager: @packages, configDirPath, resourcePath}) @contextMenu = new ContextMenuManager(devMode) @menu = new MenuManager({resourcePath}) - @pasteboard = new Pasteboard() + @clipboard = new Clipboard() + @syntax = @deserializers.deserialize(@state.syntax) ? new Syntax() @subscribe @packages, 'activated', => @watchThemes() diff --git a/src/clipboard.coffee b/src/clipboard.coffee new file mode 100644 index 000000000..00725b5e7 --- /dev/null +++ b/src/clipboard.coffee @@ -0,0 +1,48 @@ +clipboard = require 'clipboard' +crypto = require 'crypto' + +# Public: Represents the clipboard used for copying and pasting in Atom. +# +# A clipboard instance is always available under the `atom.clipboard` global. +module.exports = +class Clipboard + metadata: null + signatureForMetadata: null + + # Creates an `md5` hash of some text. + # + # * text: A {String} to hash. + # + # Returns a hashed {String}. + md5: (text) -> + crypto.createHash('md5').update(text, 'utf8').digest('hex') + + # Public: Write the given text to the clipboard. + # + # The metadata associated with the text is available by calling + # {.readWithMetadata}. + # + # * text: A {String} to store. + # * metadata: An {Object} of additional info to associate with the text. + write: (text, metadata) -> + @signatureForMetadata = @md5(text) + @metadata = metadata + clipboard.writeText(text) + + # Public: Read the text from the clipboard. + # + # Returns a {String}. + read: -> + clipboard.readText() + + # Public: Read the text from the clipboard and return both the text and the + # associated metadata. + # + # Returns an {Object} with a `text` key and a `metadata` key if it has + # associated metadata. + readWithMetadata: -> + text = @read() + if @signatureForMetadata is @md5(text) + {text, @metadata} + else + {text} diff --git a/src/editor-view.coffee b/src/editor-view.coffee index c3754070b..e29a2cc13 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -209,7 +209,7 @@ class EditorView extends View 'editor:toggle-line-comments': => @toggleLineCommentsInSelection() 'editor:log-cursor-scope': => @logCursorScope() 'editor:checkout-head-revision': => @checkoutHead() - 'editor:copy-path': => @copyPathToPasteboard() + 'editor:copy-path': => @copyPathToClipboard() 'editor:move-line-up': => @editor.moveLineUp() 'editor:move-line-down': => @editor.moveLineDown() 'editor:duplicate-line': => @editor.duplicateLine() @@ -1411,9 +1411,9 @@ class EditorView extends View @highlightedLine = null # Copies the current file path to the native clipboard. - copyPathToPasteboard: -> + copyPathToClipboard: -> path = @editor.getPath() - atom.pasteboard.write(path) if path? + atom.clipboard.write(path) if path? ### Internal ### diff --git a/src/editor.coffee b/src/editor.coffee index 753754be2..904c8dfa4 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -535,31 +535,31 @@ class Editor extends Model # Public: Copies and removes all characters from cursor to the end of the # line. cutToEndOfLine: -> - maintainPasteboard = false + maintainClipboard = false @mutateSelectedText (selection) -> - selection.cutToEndOfLine(maintainPasteboard) - maintainPasteboard = true + selection.cutToEndOfLine(maintainClipboard) + maintainClipboard = true # Public: Cuts the selected text. cutSelectedText: -> - maintainPasteboard = false + maintainClipboard = false @mutateSelectedText (selection) -> - selection.cut(maintainPasteboard) - maintainPasteboard = true + selection.cut(maintainClipboard) + maintainClipboard = true # Public: Copies the selected text. copySelectedText: -> - maintainPasteboard = false + maintainClipboard = false for selection in @getSelections() - selection.copy(maintainPasteboard) - maintainPasteboard = true + selection.copy(maintainClipboard) + maintainClipboard = true # Public: Pastes the text in the clipboard. # # * options: # + A set of options equivalent to {Selection.insertText}. pasteText: (options={}) -> - [text, metadata] = atom.pasteboard.read() + {text, metadata} = atom.clipboard.readWithMetadata() containsNewlines = text.indexOf('\n') isnt -1 diff --git a/src/pasteboard.coffee b/src/pasteboard.coffee deleted file mode 100644 index 2f97333d8..000000000 --- a/src/pasteboard.coffee +++ /dev/null @@ -1,36 +0,0 @@ -clipboard = require 'clipboard' -crypto = require 'crypto' - -# Public: Represents the clipboard used for copying and pasting in Atom. -# -# A pasteboard instance is always available under the `atom.pasteboard` global. -module.exports = -class Pasteboard - signatureForMetadata: null - - # Creates an `md5` hash of some text. - # - # text - A {String} to encrypt. - # - # Returns an encrypted {String}. - md5: (text) -> - crypto.createHash('md5').update(text, 'utf8').digest('hex') - - # Public: Write the given text to the clipboard. - # - # text - A {String} to store. - # metadata - An {Object} of additional info to associate with the text. - write: (text, metadata) -> - @signatureForMetadata = @md5(text) - @metadata = metadata - clipboard.writeText(text) - - # Public: Read the text from the clipboard. - # - # Returns an {Array}. The first element is the saved text and the second is - # any metadata associated with the text. - read: -> - text = clipboard.readText() - value = [text] - value.push(@metadata) if @signatureForMetadata == @md5(text) - value diff --git a/src/selection.coffee b/src/selection.coffee index 915f3fb0b..7aceb8a79 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -506,34 +506,33 @@ class Selection # Public: Cuts the selection until the end of the line. # - # * maintainPasteboard: + # * maintainClipboard: # ? - cutToEndOfLine: (maintainPasteboard) -> + cutToEndOfLine: (maintainClipboard) -> @selectToEndOfLine() if @isEmpty() - @cut(maintainPasteboard) + @cut(maintainClipboard) - # Public: Copies the selection to the pasteboard and then deletes it. + # Public: Copies the selection to the clipboard and then deletes it. # - # * maintainPasteboard: + # * maintainClipboard: # ? - cut: (maintainPasteboard=false) -> - @copy(maintainPasteboard) + cut: (maintainClipboard=false) -> + @copy(maintainClipboard) @delete() - # Public: Copies the current selection to the pasteboard. + # Public: Copies the current selection to the clipboard. # - # * maintainPasteboard: + # * maintainClipboard: # ? - copy: (maintainPasteboard=false) -> + copy: (maintainClipboard=false) -> return if @isEmpty() text = @editor.buffer.getTextInRange(@getBufferRange()) - if maintainPasteboard - [currentText, metadata] = atom.pasteboard.read() - text = currentText + '\n' + text + if maintainClipboard + text = "#{atom.clipboard.read()}\n#{text}" else metadata = { indentBasis: @editor.indentationForBufferRow(@getBufferRange().start.row) } - atom.pasteboard.write(text, metadata) + atom.clipboard.write(text, metadata) # Public: Creates a fold containing the current selection. fold: ->