diff --git a/src/plugins/coreplugin/textdocument.cpp b/src/plugins/coreplugin/textdocument.cpp index 78a3447d7d5f89e70903888e3de329b49c113c51..377567180b810300efdeb0fec09edac654072807 100644 --- a/src/plugins/coreplugin/textdocument.cpp +++ b/src/plugins/coreplugin/textdocument.cpp @@ -140,6 +140,13 @@ void TextDocument::setCodec(const QTextCodec *codec) d->m_format.codec = codec; } +void TextDocument::switchUtf8Bom() +{ + if (debug) + qDebug() << Q_FUNC_INFO << this << "UTF-8 BOM: " << !d->m_format.hasUtf8Bom; + d->m_format.hasUtf8Bom = !d->m_format.hasUtf8Bom; +} + /*! \brief Returns the format obtained from the last call to read(). */ diff --git a/src/plugins/coreplugin/textdocument.h b/src/plugins/coreplugin/textdocument.h index 9fc5a89056ccfcd091b2b4b24a3ed24c59a670b3..6c59c69fcb8e2acb22b79ab26cf4608ba7a52349 100644 --- a/src/plugins/coreplugin/textdocument.h +++ b/src/plugins/coreplugin/textdocument.h @@ -55,6 +55,7 @@ public: Utils::TextFileFormat format() const; const QTextCodec *codec() const; void setCodec(const QTextCodec *); + void switchUtf8Bom(); ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString); ReadResult read(const QString &fileName, QString *plainText, QString *errorString); diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 9f8540965cbcc51d06250417ba46a5ab5e64e2eb..021468d45f0a29aa66dd24099c101f0c7333e5ff 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -5838,6 +5838,11 @@ void BaseTextEditorWidget::circularPaste() QPlainTextEdit::copy(); } +void BaseTextEditorWidget::switchUtf8bom() +{ + baseTextDocument()->switchUtf8Bom(); +} + QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const { if (d->m_inBlockSelectionMode) { @@ -6087,6 +6092,17 @@ void BaseTextEditorWidget::appendStandardContextMenuActions(QMenu *menu) a = am->command(Constants::CIRCULAR_PASTE)->action(); if (a && a->isEnabled()) menu->addAction(a); + + BaseTextDocument *doc = baseTextDocument(); + if (doc->codec()->name() == QString(QLatin1String("UTF-8"))) { + a = am->command(Constants::SWITCH_UTF8BOM)->action(); + if (a && a->isEnabled()) { + a->setText(doc->format().hasUtf8Bom ? tr("Delete UTF-8 BOM on Save") + : tr("Add UTF-8 BOM on Save")); + menu->addSeparator(); + menu->addAction(a); + } + } } diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index dddb6bf0089af13e4146565a460f763307e0f01c..d61ae1237b71ed9fde7f4d7c0ee05480016323ec 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -260,6 +260,7 @@ public slots: virtual void selectAll(); void circularPaste(); + void switchUtf8bom(); void zoomIn(int range = 1); void zoomOut(int range = 1); diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index 0e20f1901f3a56e5c4e61497b60905a0837b558a..319e559a63e5fb33370900dd54970285a5fa2453 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -65,6 +65,7 @@ TextEditorActionHandler::TextEditorActionHandler(const char *context, m_cutAction(0), m_pasteAction(0), m_circularPasteAction(0), + m_switchUtf8bomAction(0), m_selectAllAction(0), m_gotoAction(0), m_printAction(0), @@ -379,6 +380,11 @@ void TextEditorActionHandler::createActions() connect(m_circularPasteAction, SIGNAL(triggered()), this, SLOT(circularPasteAction())); medit->addAction(command, Core::Constants::G_EDIT_COPYPASTE); + m_switchUtf8bomAction = new QAction(this); + m_modifyingActions << m_switchUtf8bomAction; + command = am->registerAction(m_switchUtf8bomAction, Constants::SWITCH_UTF8BOM, m_contextId, true); + connect(m_switchUtf8bomAction, SIGNAL(triggered()), this, SLOT(switchUtf8bomAction())); + m_indentAction = new QAction(tr("Indent"), this); m_modifyingActions << m_indentAction; command = am->registerAction(m_indentAction, Constants::INDENT, m_contextId, true); @@ -589,6 +595,7 @@ FUNCTION2(copyAction, copy) FUNCTION2(cutAction, cut) FUNCTION2(pasteAction, paste) FUNCTION2(circularPasteAction, circularPaste) +FUNCTION2(switchUtf8bomAction, switchUtf8bom) FUNCTION2(formatAction, format) FUNCTION2(rewrapParagraphAction, rewrapParagraph) FUNCTION2(selectAllAction, selectAll) diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h index f94b3842673599eceae90bb8c5fe4384126a4daa..d0a41e0e74a089c48d077f2c9b3ffe628226fd94 100644 --- a/src/plugins/texteditor/texteditoractionhandler.h +++ b/src/plugins/texteditor/texteditoractionhandler.h @@ -99,6 +99,7 @@ private slots: void cutAction(); void pasteAction(); void circularPasteAction(); + void switchUtf8bomAction(); void selectAllAction(); void gotoAction(); void printAction(); @@ -170,6 +171,7 @@ private: QAction *m_cutAction; QAction *m_pasteAction; QAction *m_circularPasteAction; + QAction *m_switchUtf8bomAction; QAction *m_selectAllAction; QAction *m_gotoAction; QAction *m_printAction; diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 86108f84a5c28d76543c638467eb83877215e26f..bd90e06631bebdff92145c58ac00c70aacefc3fb 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -102,6 +102,7 @@ const char TASK_DOWNLOAD_DEFINITIONS[] = "TextEditor.Task.Download"; const char TASK_REGISTER_DEFINITIONS[] = "TextEditor.Task.Register"; const char TASK_OPEN_FILE[] = "TextEditor.Task.OpenFile"; const char CIRCULAR_PASTE[] = "TextEditor.CircularPaste"; +const char SWITCH_UTF8BOM[] = "TextEditor.SwitchUtf8bom"; const char INDENT[] = "TextEditor.Indent"; const char UNINDENT[] = "TextEditor.Unindent";