Skip to content

Instantly share code, notes, and snippets.

@yohanesgultom
Last active May 20, 2021 04:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohanesgultom/215c4b3e34e7aead7fbb5dae94669f17 to your computer and use it in GitHub Desktop.
Save yohanesgultom/215c4b3e34e7aead7fbb5dae94669f17 to your computer and use it in GitHub Desktop.
Atom.io init script collections
# Add this blocks of code below to atom.io init.coffe (init scripts) to implement the feature
# Custom file extension mapping
# description: let atom.io automatically apply certain syntax highlighting (eg. C) to new certain extension (eg. *.cl)
# start
{extname} = require 'path'
fileTypes =
'.cl': 'text.source.c'
atom.workspace.observeTextEditors (editor) ->
scopeName = fileTypes[extname editor.getPath()]
return unless scopeName?
g = atom.grammars.grammarForScopeName scopeName
return unless g?
editor.setGrammar g
# end
# Custom text-wrapper command
# description: create custom command (eg: enamex:person, wrap:a) to wrap selected text with custom tags
# useful for dataset annotation or massive HTML/XML editing
# start
wrapSelections = (editor, before, after) ->
after ?= before
cursorOffset = before.indexOf '$1'
if cursorOffset < 0 then cursorOffset = before.length
cursorPositions = for selection in editor.getSelections()
cursorPosition = selection.getBufferRange().start.translate [0, cursorOffset]
selectedText = selection.getText()
selection.insertText("#{before.replace '$1', ''}#{selectedText}#{after}")
cursorPosition
for cursorPosition, i in cursorPositions
if i == 0
editor.setCursorBufferPosition cursorPosition
else
editor.addCursorAtBufferPosition cursorPosition
# enamex:person
# simply wrap using enamex:person tag
atom.commands.add 'atom-text-editor', 'enamex:person', ->
editor = @getModel()
editor.transact ->
wrapSelections editor, '<ENAMEX TYPE="PERSON">', '</ENAMEX>'
# wrap:a
# wrap then change focus to $1
atom.commands.add 'atom-text-editor', 'wrap:a', ->
editor = @getModel()
editor.transact ->
wrapSelections editor, '<a href="$1" target="blank">', '</a>'
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment