#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Text editor class for your favourite editor."""

#
# (C) Gerrit Holl, 2004
# (C) Pywikibot team, 2004-2014
#
# Distributed under the terms of the MIT license.
#
__version__ = "$Id$"
#

import sys
import os
import tempfile
import pywikibot
from pywikibot import config


class TextEditor(object):

    """Text editor."""

    def command(self, tempFilename, text, jumpIndex=None):
        command = config.editor
        if jumpIndex:
            # Some editors make it possible to mark occurences of substrings,
            # or to jump to the line of the first occurence.
            # TODO: Find a better solution than hardcoding these, e.g. a config
            # option.
            line = text[:jumpIndex].count('\n')
            column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1)
        else:
            line = column = 0
        # Linux editors. We use startswith() because some users might use
        # parameters.
        if config.editor.startswith('kate'):
            command += " -l %i -c %i" % (line + 1, column + 1)
        elif config.editor.startswith('gedit'):
            command += " +%i" % (line + 1)  # seems not to support columns
        elif config.editor.startswith('emacs'):
            command += " +%i" % (line + 1)  # seems not to support columns
        elif config.editor.startswith('jedit'):
            command += " +line:%i" % (line + 1)  # seems not to support columns
        elif config.editor.startswith('vim'):
            command += " +%i" % (line + 1)  # seems not to support columns
        elif config.editor.startswith('nano'):
            command += " +%i,%i" % (line + 1, column + 1)
        # Windows editors
        elif config.editor.lower().endswith('notepad++.exe'):
            command += " -n%i" % (line + 1)  # seems not to support columns

        command += ' %s' % tempFilename
        #print command
        return command

    def convertLinebreaks(self, text):
        if sys.platform == 'win32':
            return text.replace('\r\n', '\n')
        # TODO: Mac OS handling
        return text

    def restoreLinebreaks(self, text):
        if text is None:
            return
        if sys.platform == 'win32':
            return text.replace('\n', '\r\n')
        # TODO: Mac OS handling
        return text

    def edit(self, text, jumpIndex=None, highlight=None):
        """
        Call the editor and thus allows the user to change the text.

        Returns the modified text. Halts the thread's operation until the editor
        is closed.

        Returns None if the user didn't save the text file in his text editor.

        Parameters:
            * text      - a Unicode string
            * jumpIndex - an integer: position at which to put the caret
            * highlight - a substring; each occurence will be highlighted
        """
        text = self.convertLinebreaks(text)
        if config.editor:
            tempFilename = '%s.%s' % (tempfile.mktemp(),
                                      config.editor_filename_extension)
            tempFile = open(tempFilename, 'w')
            tempFile.write(text.encode(config.editor_encoding))
            tempFile.close()
            creationDate = os.stat(tempFilename).st_mtime
            command = self.command(tempFilename, text, jumpIndex)
            os.system(command)
            lastChangeDate = os.stat(tempFilename).st_mtime
            if lastChangeDate == creationDate:
                # Nothing changed
                return None
            else:
                newcontent = open(tempFilename).read().decode(config.editor_encoding)
                os.unlink(tempFilename)
                return self.restoreLinebreaks(newcontent)
        else:
            return self.restoreLinebreaks(
                pywikibot.ui.editText(
                    text,
                    jumpIndex=jumpIndex,
                    highlight=highlight))

@@ -410,32 +410,88 @@
 umpIndex=None):%0A
+        %22%22%22Return editor selected in user-config.py.%22%22%22%0A
         command 
@@ -502,24 +502,24 @@
 nfig.editor%0A
-
         if j
@@ -2016,32 +2016,67 @@
 ks(self, text):%0A
+        %22%22%22Convert line-breaks.%22%22%22%0A
         if sys.p
@@ -2225,32 +2225,67 @@
 ks(self, text):%0A
+        %22%22%22Restore line-breaks.%22%22%22%0A
         if text 
@@ -3233,16 +3233,17 @@
 name, 'w
+b
 ')%0A     
@@ -3664,32 +3664,32 @@
           else:%0A
-
                 
@@ -3718,16 +3718,22 @@
 Filename
+, 'rb'
 ).read()

Make editor.py python3-compatible