Commits

Yuya Nishihara committed 92cfe4e

fileview: add option to guess encoding from its content on load (closes #3620)

Enabled by default because guessencoding() isn't slow.

Comments (0)

Files changed (1)

tortoisehg/hgqt/fileview.py

         self.actionShelf.setVisible(False)
         self.actionShelf.triggered.connect(self._launchShelve)
 
+        self._actionAutoTextEncoding = a = QAction(_('&Auto Detect'), self)
+        a.setCheckable(True)
         self._textEncodingGroup = fileencoding.createActionGroup(self)
         self._textEncodingGroup.triggered.connect(self._applyTextEncoding)
 
 
     def loadSettings(self, qs, prefix):
         self.sci.loadSettings(qs, prefix)
+        self._actionAutoTextEncoding.setChecked(
+            qs.value(prefix + '/autotextencoding', True).toBool())
         enc = str(qs.value(prefix + '/textencoding').toString())
         if enc:
             try:
 
     def saveSettings(self, qs, prefix):
         self.sci.saveSettings(qs, prefix)
+        qs.setValue(prefix + '/autotextencoding', self._autoTextEncoding())
         qs.setValue(prefix + '/textencoding', self._textEncoding())
 
     @pyqtSlot()
                             pctx.rev(), pctx, firstline))
             a.setChecked(fd.baseRev() == pctx.rev())
 
+    def _autoTextEncoding(self):
+        return self._actionAutoTextEncoding.isChecked()
+
     def _textEncoding(self):
         return fileencoding.checkedActionName(self._textEncodingGroup)
 
     def display(self, fd):
         fd.load(self.isChangeSelectionEnabled())
         fd.setTextEncoding(self._textEncoding())
+        if self._autoTextEncoding():
+            fd.detectTextEncoding()
+            fileencoding.checkActionByName(self._textEncodingGroup,
+                                           fd.textEncoding())
         self._displayLoaded(fd)
 
     def _displayLoaded(self, fd):
     def _createContextMenu(self, point):
         menu = self.sci.createEditorContextMenu()
         m = menu.addMenu(_('E&ncoding'))
+        m.addAction(self._actionAutoTextEncoding)
+        m.addSeparator()
         fileencoding.addActionsToMenu(m, self._textEncodingGroup)
 
         line = self.sci.lineNearPoint(point)