Overwrite spaces for markdown


#1

I just noticed a rather huge problem with editing markdown in atom.io: I have set my tabs to 2 spaces, which is also used in markdown, but most markdown parsers seem to expect 4 spaces (for example for creating sub-lists). This means that the .md file produced in atom.io cannot be parsed correctly in most other editors.

As far as I know, it is not possible currently to override the spaces settings for markdown files only, is it? Would be really helpful here!


#2

Technically, it is … but there is no built-in method for doing so. There are a couple packages including Project Manager and Editor Settings that offer this capability. I have the following in my init.coffee because I wanted to understand the mechanisms better and teach myself a little more CoffeeScript:

# Settings to assign based on grammar name.
grammarSettings =
  'GitHub Markdown':
    showInvisibles: false
    softWrap: true
    tabLength: 4
  Java:
    tabLength: 4
  Python:
    tabLength: 4
  'Shell Script (Bash)':
    tabLength: 4

# Apply the settings to the editor session.
#
# editorView - EditorView to which to apply the settings.
# settings   - The settings to apply.
applySettingsForGrammar = (editorView, settings) ->
  editor = editorView.getEditor()
  for key, value of settings
    switch key
      when 'showInvisibles' then editorView.setShowInvisibles(value)
      when 'softWrap' then editor.setSoftWrap(value)
      when 'tabLength' then editor.setTabLength(value)

# Apply grammar-specific settings.
#
# editorView - EditorView to which to apply the grammar-specific settings.
applySettings = (editorView) ->
  settings = grammarSettings[editorView.getEditor().getGrammar().name]
  return unless settings?

  applySettingsForGrammar(editorView, settings)

# Executes for each and every EditorView, past and future.
atom.workspaceView.eachEditorView (editorView) ->
  applySettings(editorView)

  editorView.getEditor().on 'grammar-changed', ->
    applySettings(editorView)