Method: Doing::Util#find_default_editor

Defined in:
lib/doing/util.rb

#find_default_editor(editor_for = 'default') ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/doing/util.rb', line 163

def find_default_editor(editor_for = 'default')
  # return nil unless $stdout.isatty || ENV['DOING_EDITOR_TEST']

  return ENV['EDITOR'] if ENV['DOING_EDITOR_TEST']

  editor_config = Doing.setting('editors')

  if editor_config.is_a?(String)
    msg = "Please update your configuration, 'editors' should be a mapping."
    msg << ' Delete the key and run `doing config --update`.'
    Doing.logger.warn('Deprecated:', msg)
    return editor_config
  end

  if editor_config[editor_for]
    editor = editor_config[editor_for]
    Doing.logger.debug('Editor:', "Using #{editor} from config 'editors.#{editor_for}' for #{editor_for}")
    return editor if editor.good?
  end

  if editor_for != 'editor' && editor_config['default']
    editor = editor_config['default']
    Doing.logger.debug('Editor:', "Using #{editor} from config: 'editors.default' for #{editor_for}")
    return editor if editor.good?
  end

  editor ||= ENV['DOING_EDITOR'] || ENV['GIT_EDITOR'] || ENV['EDITOR']

  if editor.good?
    Doing.logger.debug('Editor:', "Found editor in environment variables: #{editor} for #{editor_for}")
    return editor
  end

  Doing.logger.debug('Editor:', 'No EDITOR environment variable, testing available editors')
  editors = %w[vim vi code subl mate mvim nano emacs]
  editors.each do |ed|
    try = TTY::Which.which(ed)
    if try
      Doing.logger.debug('Editor:', "Using editor #{try} for #{editor_for}")
      return try
    end

    Doing.logger.debug('Editor:', "#{ed} not available")
  end

  nil
end