Add BOM option to convert AppleScript (close #1210)

This commit is contained in:
1024jp 2022-02-12 14:29:36 +09:00
parent 6b6d4441ba
commit a52b2d6d8b
9 changed files with 57 additions and 10 deletions

View File

@ -5,6 +5,10 @@ Change Log
4.1.0-beta.2 (unreleased)
--------------------------
### New Features
- Indtroduce a new read-only AppleScript paramater `has BOM` to document objects and a new option `BOM` to the `convert` command.
4.1.0-beta (475)

View File

@ -77,6 +77,9 @@
<dt><code>IANA charset</code></dt>
<dd>The IANA charset name of the document. (Unicode text, e.g.<code>Shift_JIS</code>, <code>EUC-JP</code>)</dd>
<dt><code>has BOM</code></dt>
<dd>Has the file encoding a BOM (byte order mark)? (boolean) <span class="added">new on CotEditor 4.1</span></dd>
<dt><code>line ending</code></dt>
<dd>The line ending code of the document. (<code>CR</code> / <code>LF</code> / <code>CRLF</code>)</dd>
@ -363,8 +366,11 @@ For example, when not using the <code>wrap</code> or <code>backwards</code> opti
<dt><code>to</code> (Unicode text)</dt>
<dd>The new encoding, either in localized encoding name or an IANA charset name.</dd>
<dt><code>lossy</code> (boolean)</dt>
<dt>[<code>lossy</code>] (boolean)</dt>
<dd>Whether to allow “lossy” conversion (might result in loss of character that couldn't be converted) or not.</dd>
<dt>[<code>BOM</code>] (boolean)</dt>
<dd>Whether to add a BOM (byte order mark). <span class="added">(added on CotEditor 4.1)</span></dd>
</dl></dd>
<dd><dl>
<dt>Return value</dt>
@ -380,8 +386,8 @@ For example, when not using the <code>wrap</code> or <code>backwards</code> opti
<h4>Example</h4>
<figure>
<pre class="source"><code class="AppleScript">convert front document to &quot;Unicode (UTF-8)&quot; without lossy</code></pre>
<figcaption>Converts the text encoding of the frontmost window to UTF-8, returns the result.</figcaption>
<pre class="source"><code class="AppleScript">convert front document to &quot;Unicode (UTF-8)&quot; with BOM without lossy</code></pre>
<figcaption>Converts the text encoding of the frontmost window to Unicode (UTF-8) with BOM, returns the result.</figcaption>
</figure>
<h4>Discussion</h4>

View File

@ -15,8 +15,15 @@
<article>
<h2>Terminology change on CotEditor 4.1.0</h2>
<section>
<h3>jump</h3>
<p>A new command <code>jump</code> was added to the document object.</p>
</section>
<section>
<h3>Support encodings with BOM</h3>
<p>A new property <code>has BOM</code> was added to the document object.</p>
<p>A new option <code>BOM</code> was added to the <code>convert</code> command.</p>
</section>
</article>

View File

@ -34,6 +34,7 @@
<li>Add “Open Outline Menu” command to the Find menu.</li>
<li>Add Turkish (thanks to Emir SARI!) and British English (thanks to Alex Newson!) localizations.</li>
<li>Introduce a new AppleScript command <code>jump</code> to document objects.</li>
<li>Indtroduce a new read-only AppleScript paramater <code>has BOM</code> to document objects and a new option <code>BOM</code> to the <code>convert</code> command.</li>
<li>Place line number views on the right side in the editor if the writing direction is right-to-left.</li>
<li>Add syntax style for Protocol Buffer.</li>
</ul>

View File

@ -76,6 +76,9 @@
<dt><code>IANA charset</code></dt>
<dd>エンコーディングのIANA charset名Unicode text型、<code>Shift_JIS</code>, <code>EUC-JP</code>など)</dd>
<dt><code>has BOM</code></dt>
<dd>エンコーディングでBOMがあるかどうかboolean型<span class="added">CotEditor 4.1で追加</span></dd>
<dt><code>line ending</code></dt>
<dd>改行コード(<code>CR</code> / <code>LF</code> / <code>CRLF</code></dd>
@ -364,8 +367,11 @@ lengthが負である場合、指定される範囲は対象書類の文字列
<dt><code>to</code> (Unicode text)</dt>
<dd>新しいエンコーディングローカライズされたエンコーディング名かIANA charset名で指定</dd>
<dt><code>lossy</code> (boolean)</dt>
<dt>[<code>lossy</code>] (boolean)</dt>
<dd>エンコーディング適用によって一部の文字列が失われても良いか</dd>
<dt>[<code>BOM</code>] (boolean)</dt>
<dd>エンコーディングにBOMを付けるかどうか <span class="added">(CotEditor 4.1で追加)</span></dd>
</dl></dd>
<dd><dl>
<dt>返り値</dt>
@ -381,8 +387,8 @@ lengthが負である場合、指定される範囲は対象書類の文字列
<h4></h4>
<figure>
<pre class="source"><code class="AppleScript">convert front document to &quot;Unicode (UTF-8)&quot; without lossy</code></pre>
<figcaption>UTF-8に変換し、変換できたかどうかを返す。</figcaption>
<pre class="source"><code class="AppleScript">convert front document to &quot;Unicode (UTF-8)&quot; with BOM without lossy</code></pre>
<figcaption>Unicode (UTF-8)BOM付きに変換し、変換できたかどうかを返す。</figcaption>
</figure>
<h4>詳細</h4>

View File

@ -20,6 +20,12 @@
<h3>ジャンプ</h3>
<p>documentオブジェクトに新しく<code>jump</code>コマンドが追加されました。</p>
</section>
<section>
<h3>BOM付きエンコーディングに対応</h3>
<p>documentオブジェクトに新しく<code>has BOM</code>プロパティが追加されました。</p>
<p><code>convert</code>コマンドに新しく<code>BOM</code>オプションが追加されました。</p>
</section>
</article>

View File

@ -34,6 +34,7 @@
<li>検索メニューに「アウトラインメニューを開く」コマンドを追加</li>
<li>トルコ語Emir SARIさんに感謝とイギリス英語Alex Newsonさんに感謝のローカライゼーションを追加</li>
<li>新しいAppleScriptコマンド<code>jump</code>をdocumentオブジェクトに追加</li>
<li>新しいAppleScriptパラメータ<code>has BOM</code>をdocumentオブジェクトに追加し、<code>BOM</code>オプションを <code>convert</code>コマンドに追加</li>
<li>文章の方向が右から左のときには行番号ビューも右側に配置</li>
<li>シンタックス定義Protocol Bufferを追加</li>
</ul>

View File

@ -59,6 +59,9 @@
<property name="IANA charset" code="iacs" type="text" access="r" description="The IANA charset name of the document.">
<cocoa key="IANACharSetName"/>
</property>
<property name="has BOM" code="hBOM" type="boolean" access="r" description="Is the file encoding of the document has BOM (byte mark order)?">
<cocoa key="hasBOM"/>
</property>
<property name="length" code="slen" type="integer" access="r" description="The number of characters in the document."/>
<property name="line ending" code="lend" type="line ending character" access="rw" description="The line ending type of the document.">
<cocoa key="lineEndingChar"/>
@ -197,9 +200,12 @@
<command name="convert" code="cEd1CVrt" description="Convert the document text to new encoding.">
<access-group identifier="com.coteditor.CotEditor.edit" access="rw"/>
<direct-parameter type="document" description="The document to convert encoding."/>
<parameter name="lossy" code="lOsy" type="boolean" description="Allows lossy conversion?">
<parameter name="lossy" code="lOsy" type="boolean" description="Allows lossy conversion?" optional="yes">
<cocoa key="lossy"/>
</parameter>
<parameter name="BOM" code="wBOM" type="boolean" description="Has the new encoding a BOM (byte mark order)?" optional="yes">
<cocoa key="BOM"/>
</parameter>
<parameter name="to" code="nEcd" type="text" description="The new encoding, either in localized encoding name or an IANA charset name.">
<cocoa key="newEncoding"/>
</parameter>

View File

@ -9,7 +9,7 @@
// ---------------------------------------------------------------------------
//
// © 2004-2007 nakamuxu
// © 2014-2021 1024jp
// © 2014-2022 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -159,6 +159,14 @@ extension Document {
}
@objc var hasBOM: Bool {
self.fileEncoding.withUTF8BOM ||
self.fileEncoding.encoding == .utf16 ||
self.fileEncoding.encoding == .utf32
}
/// syntax style name (Unicode text)
@objc var coloringStyle: String {
@ -227,11 +235,13 @@ extension Document {
return false
}
if encoding == self.fileEncoding.encoding {
let withBOM = arguments["BOM"] as? Bool ?? false
let fileEncoding = FileEncoding(encoding: encoding, withUTF8BOM: withBOM)
if fileEncoding == self.fileEncoding {
return true
}
let fileEncoding = FileEncoding(encoding: encoding)
let lossy = (arguments["lossy"] as? Bool) ?? false
do {