summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/public/posts/2021-10-02
diff options
context:
space:
mode:
Diffstat (limited to 'vhosts/blog/public/posts/2021-10-02')
-rw-r--r--vhosts/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html200
-rw-r--r--vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html130
-rw-r--r--vhosts/blog/public/posts/2021-10-02/ruby-detect-running-implementation/index.html217
-rw-r--r--vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html290
-rw-r--r--vhosts/blog/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html258
-rw-r--r--vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html224
-rw-r--r--vhosts/blog/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html253
7 files changed, 1572 insertions, 0 deletions
diff --git a/vhosts/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html b/vhosts/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html
new file mode 100644
index 00000000..75ba6ea9
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html
@@ -0,0 +1,200 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="C++ の属性構文の属性名には、キーワードが使える。ネタ記事。">
+ <meta name="keywords" content="C++,C++ 17">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【C++】 属性構文の属性名にはキーワードが使える | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【C++】 属性構文の属性名にはキーワードが使える</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/cpp/">C++</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/cpp17/">C++ 17</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/94090937bcf860cfa93b">https://qiita.com/nsfisis/items/94090937bcf860cfa93b</a>
+ </div>
+ </div>
+
+ <p>
+ タイトル落ち。まずはこのコードを見て欲しい。
+ </p>
+
+ <pre class="highlight" language="cpp" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;iostream&gt;</span></span>
+
+[[<span class="hljs-keyword">alignas</span>]] [[<span class="hljs-keyword">alignof</span>]] [[<span class="hljs-keyword">and</span>]] [[<span class="hljs-keyword">and_eq</span>]] [[<span class="hljs-keyword">asm</span>]] [[<span class="hljs-keyword">auto</span>]] [[<span class="hljs-keyword">bitand</span>]]
+[[<span class="hljs-keyword">bitor</span>]] [[<span class="hljs-type">bool</span>]] [[<span class="hljs-keyword">break</span>]] [[<span class="hljs-keyword">case</span>]] [[<span class="hljs-keyword">catch</span>]] [[<span class="hljs-type">char</span>]] [[<span class="hljs-type">char16_t</span>]]
+[[<span class="hljs-type">char32_t</span>]] [[<span class="hljs-keyword">class</span>]] [[<span class="hljs-keyword">compl</span>]] [[<span class="hljs-type">const</span>]] [[<span class="hljs-keyword">const_cast</span>]] [[<span class="hljs-keyword">constexpr</span>]]
+[[<span class="hljs-keyword">continue</span>]] [[<span class="hljs-keyword">decltype</span>]] [[<span class="hljs-keyword">default</span>]] [[<span class="hljs-keyword">delete</span>]] [[<span class="hljs-keyword">do</span>]] [[<span class="hljs-type">double</span>]]
+[[<span class="hljs-keyword">dynamic_cast</span>]] [[<span class="hljs-keyword">else</span>]] [[<span class="hljs-keyword">enum</span>]] [[<span class="hljs-keyword">explicit</span>]] [[<span class="hljs-keyword">export</span>]] [[<span class="hljs-keyword">extern</span>]] [[<span class="hljs-literal">false</span>]]
+[[<span class="hljs-keyword">final</span>]] [[<span class="hljs-type">float</span>]] [[<span class="hljs-keyword">for</span>]] [[<span class="hljs-keyword">friend</span>]] [[<span class="hljs-keyword">goto</span>]] [[<span class="hljs-keyword">if</span>]] [[<span class="hljs-keyword">inline</span>]] [[<span class="hljs-type">int</span>]]
+[[<span class="hljs-type">long</span>]] [[<span class="hljs-keyword">mutable</span>]] [[<span class="hljs-keyword">namespace</span>]] [[<span class="hljs-keyword">new</span>]] [[<span class="hljs-keyword">noexcept</span>]] [[<span class="hljs-keyword">not</span>]] [[<span class="hljs-keyword">not_eq</span>]]
+[[<span class="hljs-literal">nullptr</span>]] [[<span class="hljs-keyword">operator</span>]] [[<span class="hljs-keyword">or</span>]] [[<span class="hljs-keyword">or_eq</span>]] [[<span class="hljs-keyword">override</span>]] [[<span class="hljs-keyword">private</span>]]
+[[<span class="hljs-keyword">protected</span>]] [[<span class="hljs-keyword">public</span>]] [[<span class="hljs-keyword">register</span>]] [[<span class="hljs-keyword">reinterpret_cast</span>]] [[<span class="hljs-keyword">return</span>]] [[<span class="hljs-type">short</span>]]
+[[<span class="hljs-type">signed</span>]] [[<span class="hljs-keyword">sizeof</span>]] [[<span class="hljs-type">static</span>]] [[<span class="hljs-keyword">static_assert</span>]] [[<span class="hljs-keyword">static_cast</span>]] [[<span class="hljs-keyword">struct</span>]]
+[[<span class="hljs-keyword">switch</span>]] [[<span class="hljs-keyword">template</span>]] [[<span class="hljs-keyword">this</span>]] [[<span class="hljs-keyword">thread_local</span>]] [[<span class="hljs-keyword">throw</span>]] [[<span class="hljs-literal">true</span>]] [[<span class="hljs-keyword">try</span>]]
+[[<span class="hljs-keyword">typedef</span>]] [[<span class="hljs-keyword">typeid</span>]] [[<span class="hljs-keyword">typename</span>]] [[<span class="hljs-keyword">union</span>]] [[<span class="hljs-type">unsigned</span>]]
+[[<span class="hljs-keyword">virtual</span>]] [[<span class="hljs-type">void</span>]] [[<span class="hljs-keyword">volatile</span>]] [[<span class="hljs-type">wchar_t</span>]] [[<span class="hljs-keyword">while</span>]] [[<span class="hljs-keyword">xor</span>]] [[<span class="hljs-keyword">xor_eq</span>]]
+<span class="hljs-comment">// [[using]]</span>
+<span class="hljs-type">int</span> <span class="hljs-built_in">main</span>() {
+ std::cout &lt;&lt; <span class="hljs-string">&quot;Hello, World!&quot;</span> &lt;&lt; std::endl;
+}</code></pre>
+
+ <blockquote>
+ <p>
+ コンパイラのバージョン $ clang++ –version Apple clang version 11.0.0 (clang-1100.0.33.8) Target: x86_64-apple-darwin19.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
+ </p>
+
+ <p>
+ コンパイルコマンド (C17指定) $ clang –std=c++17 hoge.cpp
+ </p>
+ </blockquote>
+
+ <p>
+ この記事から得られるものはこれ以上ないので以下は蛇足になる。
+ </p>
+
+ <p>
+ 別件で cppreference.com の <a href="https://en.cppreference.com/w/cpp/language/identifiers">identifier のページ</a> を読んでいた時、次の文が目に止まった。
+ </p>
+
+ <blockquote>
+ <ul>
+ <li>
+ the identifiers that are keywords cannot be used for other purposes;
+ <ul>
+ <li>
+ The only place they can be used as non-keywords is in an attribute-token. (e.g. [[private]] is a valid attribute) (since C++11)
+ </li>
+ </ul>
+ </li>
+ </ul>
+ </blockquote>
+
+ <p>
+ キーワードでも属性として指定する場合は非キーワードとして使えるらしい。 実際にやってみる。
+ </p>
+
+ <p>
+ 同サイトの <a href="https://en.cppreference.com/w/cpp/keyword">keywords のページ</a> から一覧を拝借し、上のコードが出来上がった (C++17 においてキーワードでないものなど、一部省いている)。 大量の警告 (unknown attribute `〇〇&apos; ignored) がコンパイラから出力されるが、コンパイルできる。
+ </p>
+
+ <p>
+ 上のコードでは <code>[[using]]</code> をコメントアウトしているが、これは <code>using</code> キーワードのみ属性構文の中で意味を持つからであり、このコメントアウトを外すとコンパイルに失敗する。
+ </p>
+
+ <pre class="highlight" language="cpp" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">// using の例</span>
+[[<span class="hljs-keyword">using</span> foo: attr1, attr2]] <span class="hljs-type">int</span> x; <span class="hljs-comment">// [[foo::attr1, foo::attr2]] の糖衣構文</span></code></pre>
+
+ <p>
+ C++17 の仕様も見てみる (正確には標準化前のドラフト)。
+ </p>
+
+ <p>
+ 引用元: <a href="https://timsong-cpp.github.io/cppwp/n4659/dcl.attr#grammar-4">https://timsong-cpp.github.io/cppwp/n4659/dcl.attr#grammar-4</a>
+ </p>
+
+ <blockquote>
+ <p>
+ If a keyword or an alternative token that satisfies the syntactic requirements of an identifier is contained in an attribute-token, it is considered an identifier.
+ </p>
+ </blockquote>
+
+ <p>
+ 「<code>identifier</code> の構文上の要件を満たすキーワードまたは代替トークンが <code>attribute-token</code> に含まれている場合、<code>identifier</code> とみなされる」とある。どうやら間違いないようだ。
+ </p>
+
+ <p>
+ ところで、代替トークン (alternative token) とは <code>and</code> (<code>&amp;</code>) や <code>bitor</code> (<code>|</code>) などのことだが、<code>identifier</code> の構文上の要件を満たさないような代替トークンなどあるのか? 疑問に思って調べたところ、代替トークンという語にはダイグラフも含まれるらしい (参考: <a href="https://timsong-cpp.github.io/cppwp/n4659/lex.digraph">同ドラフト</a>)
+ </p>
+
+ <ul>
+ <li>
+ <code>&lt;%</code> → <code>{</code>
+ </li>
+
+ <li>
+ <code>%&gt;</code> → <code>}</code>
+ </li>
+
+ <li>
+ <code>&lt;:</code> → <code>[</code>
+ </li>
+
+ <li>
+ <code>:&gt;</code> → <code>]</code>
+ </li>
+
+ <li>
+ <code>%:</code> → <code>#</code>
+ </li>
+
+ <li>
+ <code>%:%:</code> → <code>##</code>
+ </li>
+ </ul>
+
+ <p>
+ 「<code>identifier</code> の構文上の要件を満たさないような代替トークン」はこれらが当てはまると思われる。
+ </p>
+
+ <p>
+ 調べた感想: 字句解析器か構文解析器が辛そう
+ </p>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html b/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html
new file mode 100644
index 00000000..105b946b
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/python-unbound-local-error/index.html
@@ -0,0 +1,130 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Python における UnboundLocalError の理由と対処法。">
+ <meta name="keywords" content="Python,Python 3">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【Python】 クロージャとUnboundLocalError: local variable &apos;x&apos; referenced before assignment | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【Python】 クロージャとUnboundLocalError: local variable &apos;x&apos; referenced before assignment</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/python/">Python</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/python3/">Python 3</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/5d733703afcb35bbf399">https://qiita.com/nsfisis/items/5d733703afcb35bbf399</a>
+ </div>
+ </div>
+
+ <p>
+ 本記事は Python 3.7.6 の動作結果を元にして書かれている。
+ </p>
+
+ <p>
+ Python でクロージャを作ろうと、次のようなコードを書いた。
+ </p>
+
+ <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
+ x = <span class="hljs-number">0</span>
+ <span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>():
+ x += <span class="hljs-number">1</span>
+ g()
+
+f()</code></pre>
+
+ <p>
+ 関数 <code>g</code> から 関数 <code>f</code> のスコープ内で定義された変数 <code>x</code> を参照し、それに 1 を足そうとしている。 これを実行すると <code>x += 1</code> の箇所でエラーが発生する。
+ </p>
+
+ <blockquote>
+ <p>
+ UnboundLocalError: local variable `x&apos; referenced before assignment
+ </p>
+ </blockquote>
+
+ <p>
+ local変数 <code>x</code> が代入前に参照された、とある。これは、<code>f</code> の <code>x</code> を参照するのではなく、新しく別の変数を <code>g</code> 内に作ってしまっているため。 前述のコードを宣言と代入を便宜上分けて書き直すと次のようになる。<code>var</code> を変数宣言のための構文として擬似的に利用している。
+ </p>
+
+ <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># 注: var は正しい Python の文法ではない。上記参照のこと</span>
+<span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
+ var x <span class="hljs-comment"># f の local変数 &#x27;x&#x27; を宣言</span>
+ x = <span class="hljs-number">0</span> <span class="hljs-comment"># x に 0 を代入</span>
+ <span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>(): <span class="hljs-comment"># f の内部関数 g を定義</span>
+ var x <span class="hljs-comment"># g の local変数 &#x27;x&#x27; を宣言</span>
+ <span class="hljs-comment"># たまたま f にも同じ名前の変数があるが、それとは別の変数</span>
+ x += <span class="hljs-number">1</span> <span class="hljs-comment"># x に 1 を加算 (x = x + 1 の糖衣構文)</span>
+ <span class="hljs-comment"># 加算する前の値を参照しようとするが、まだ代入されていないためエラー</span>
+ g()</code></pre>
+
+ <p>
+ 当初の意図を表現するには、次のように書けばよい。
+ </p>
+
+ <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>():
+ x = <span class="hljs-number">0</span>
+ <span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>():
+ <span class="hljs-keyword">nonlocal</span> x <span class="hljs-comment">## (*)</span>
+ x += <span class="hljs-number">1</span>
+ g()</code></pre>
+
+ <p>
+ <code>(*)</code> のように、<code>nonlocal</code> を追加する。これにより一つ外側のスコープ (<code>g</code> の一つ外側 = <code>f</code>) で定義されている <code>x</code> を探しに行くようになる。
+ </p>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/ruby-detect-running-implementation/index.html b/vhosts/blog/public/posts/2021-10-02/ruby-detect-running-implementation/index.html
new file mode 100644
index 00000000..d9f9e79f
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/ruby-detect-running-implementation/index.html
@@ -0,0 +1,217 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Ruby には複数の実装があるが、自身を実行している処理系の種類をスクリプト上からどのように判定すればよいだろうか。">
+ <meta name="keywords" content="Ruby">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【Ruby】 自身を実行している処理系の種類を判定する | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【Ruby】 自身を実行している処理系の種類を判定する</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/ruby/">Ruby</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/74d7ffeeebc51b20d791">https://qiita.com/nsfisis/items/74d7ffeeebc51b20d791</a>
+ </div>
+ </div>
+
+ <p>
+ Ruby という言語には複数の実装があるが、それらをスクリプト上からどのようにして programmatically に見分ければよいだろうか。
+ </p>
+
+ <p>
+ <code>Object</code> クラスに定義されている <code>RUBY_ENGINE</code> という定数がこの用途に使える。
+ </p>
+
+ <p>
+ 参考: <a href="https://docs.ruby-lang.org/ja/latest/method/Object/c/RUBY_ENGINE.html">Object::RUBY_ENGINE</a>
+ </p>
+
+ <p>
+ 上記ページの例から引用する:
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ruby-1.9.1 -ve &apos;p RUBY_ENGINE&apos;
+ruby 1.9.1p0 (2009-03-04 revision 22762) [x86_64-linux]
+&quot;ruby&quot;
+$ jruby -ve &apos;p RUBY_ENGINE&apos;
+jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-03-16 rev 9419) [i386-java]
+&quot;jruby&quot;</code></pre>
+
+ <p>
+ それぞれの処理系がどのような値を返すかだが、stack overflow に良い質問と回答があった。
+ </p>
+
+ <p>
+ <a href="https://stackoverflow.com/a/9894232">What values for RUBY_ENGINE correspond to which Ruby implementations?</a> より引用:
+ </p>
+
+ <blockquote>
+ <table>
+ <thead>
+ <tr>
+ <td>
+ RUBY_ENGINE
+ </td>
+
+ <td>
+ Implementation
+ </td>
+ </tr>
+ </thead>
+
+ <tbody>
+ <tr>
+ <td>
+ &lt;undefined&gt;
+ </td>
+
+ <td>
+ MRI &lt; 1.9
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `ruby&apos;
+ </td>
+
+ <td>
+ MRI &gt;= 1.9 or REE
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `jruby&apos;
+ </td>
+
+ <td>
+ JRuby
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `macruby&apos;
+ </td>
+
+ <td>
+ MacRuby
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `rbx&apos;
+ </td>
+
+ <td>
+ Rubinius
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `maglev&apos;
+ </td>
+
+ <td>
+ MagLev
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `ironruby&apos;
+ </td>
+
+ <td>
+ IronRuby
+ </td>
+ </tr>
+
+ <tr>
+ <td>
+ `cardinal&apos;
+ </td>
+
+ <td>
+ Cardinal
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </blockquote>
+
+ <p>
+ なお、この質問・回答は 2014年になされたものであり、値は変わっている可能性がある。MRI (aka CRuby) については執筆時現在 (2020/12/8) も <code>&apos;ruby&apos;</code> が返ってくることを確認済み。
+ </p>
+
+ <p>
+ この表にない主要な処理系として、https://mruby.org[mruby] は <code>&apos;mruby&apos;</code> を返す。
+ </p>
+
+ <p>
+ <a href="https://github.com/mruby/mruby/blob/ed29d74bfd95362eaeb946fcf7e865d80346b62b/include/mruby/version.h#L32-L35">mruby 該当部分のソース</a> より引用:
+ </p>
+
+ <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/*
+* Ruby engine.
+*/</span>
+<span class="hljs-meta">#<span class="hljs-keyword">define</span> MRUBY_RUBY_ENGINE <span class="hljs-string">&quot;mruby&quot;</span></span></code></pre>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html b/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html
new file mode 100644
index 00000000..fcd653e2
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html
@@ -0,0 +1,290 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Ruby 3.0 で追加される case in 構文と、then キーワードについて。">
+ <meta name="keywords" content="Ruby,Ruby 3">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【Ruby】 then キーワードと case in | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【Ruby】 then キーワードと case in</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/ruby/">Ruby</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/ruby3/">Ruby 3</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/787a8cf888a304497223">https://qiita.com/nsfisis/items/787a8cf888a304497223</a>
+ </div>
+ </div>
+
+ <section id="section--tl-dr">
+ <h2><a href="#section--tl-dr">TL; DR</a></h2>
+ <p>
+ <code>case</code> - <code>in</code> によるパターンマッチング構文でも、<code>case</code> - <code>when</code> と同じように <code>then</code> が使える (場合によっては使う必要がある)。
+ </p>
+ </section>
+
+ <section id="section--what-is-then-keyword">
+ <h2><a href="#section--what-is-then-keyword"><code>then</code> とは</a></h2>
+ <p>
+ 使われることは稀だが、Ruby では <code>then</code> がキーワードになっている。次のように使う:
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> cond <span class="hljs-keyword">then</span>
+ puts <span class="hljs-string">&quot;Y&quot;</span>
+<span class="hljs-keyword">else</span>
+ puts <span class="hljs-string">&quot;N&quot;</span>
+<span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ このキーワードが現れうる場所はいくつかあり、<code>if</code>、<code>unless</code>、<code>rescue</code>、<code>case</code> 構文がそれに当たる。 上記のように、何か条件を書いた後 <code>then</code> を置き、式がそこで終了していることを示すマーカーとして機能する。
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># Example:</span>
+
+<span class="hljs-keyword">if</span> x <span class="hljs-keyword">then</span>
+ a
+<span class="hljs-keyword">end</span>
+
+<span class="hljs-keyword">unless</span> x <span class="hljs-keyword">then</span>
+ a
+<span class="hljs-keyword">end</span>
+
+<span class="hljs-keyword">begin</span>
+ a
+<span class="hljs-keyword">rescue</span> <span class="hljs-keyword">then</span>
+ b
+<span class="hljs-keyword">end</span>
+
+<span class="hljs-keyword">case</span> x
+<span class="hljs-keyword">when</span> p <span class="hljs-keyword">then</span>
+ a
+<span class="hljs-keyword">end</span></code></pre>
+ </section>
+
+ <section id="section--why-then-is-usually-unnecessary">
+ <h2><a href="#section--why-then-is-usually-unnecessary">なぜ普段は書かなくてもよいのか</a></h2>
+ <p>
+ 普通 Ruby のコードで <code>then</code> を書くことはない。なぜか。次のコードを実行してみるとわかる。
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> <span class="hljs-literal">true</span> puts <span class="hljs-string">&#x27;Hello, World!&#x27;</span> <span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ 次のような構文エラーが出力される。
+ </p>
+
+ <pre class="highlight monospaced"><code>20:1: syntax error, unexpected local variable or method, expecting `then&apos; or &apos;;&apos; or &apos;\n&apos;
+if true puts &apos;Hello, World!&apos; end
+ ^~~~
+20:1: syntax error, unexpected `end&apos;, expecting end-of-input
+...f true puts &apos;Hello, World!&apos; end</code></pre>
+
+ <p>
+ 二つ目のメッセージは無視して一つ目を読むと、<code>then</code> か <code>;</code> か改行が来るはずのところ変数だかメソッドだかが現れたことによりエラーとなっているようだ。
+ </p>
+
+ <p>
+ ポイントは改行が <code>then</code> (や <code>;</code>) の代わりとなることである。<code>true</code> の後に改行を入れてみる。
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> <span class="hljs-literal">true</span>
+puts <span class="hljs-string">&#x27;Hello, World!&#x27;</span> <span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ 無事 Hello, World! と出力されるようになった。
+ </p>
+ </section>
+
+ <section id="section--why-then-or-linebreak-is-needed">
+ <h2><a href="#section--why-then-or-linebreak-is-needed">なぜ <code>then</code> や <code>;</code> や改行が必要か</a></h2>
+ <p>
+ なぜ <code>then</code> や <code>;</code> や改行 (以下 「<code>then</code> 等」) が必要なのだろうか。次の例を見てほしい:
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> a b <span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ <code>then</code> も <code>;</code> も改行もないのでエラーになるが、これは条件式がどこまで続いているのかわからないためだ。 この例は二通りに解釈できる。
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価</span>
+<span class="hljs-keyword">if</span> a <span class="hljs-keyword">then</span>
+b
+<span class="hljs-keyword">end</span></code></pre>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、</span>
+<span class="hljs-comment"># その結果が truthy なら何もしない</span>
+<span class="hljs-keyword">if</span> a(b) <span class="hljs-keyword">then</span>
+<span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ <code>then</code> 等はこの曖昧性を排除するためにあり、条件式は <code>if</code> から <code>then</code> 等までの間にある、ということを明確にする。 C系の <code>if</code> 後に来る <code>(</code>/<code>)</code> や、Python の <code>:</code>、Rust/Go/Swift などの <code>{</code> も同じ役割を持つ。
+ </p>
+
+ <p>
+ Ruby の場合、プログラマーが書きやすいよう改行でもって <code>then</code> が代用できるので、ほとんどの場合 <code>then</code> は必要ない。
+ </p>
+ </section>
+
+ <section id="section--then-in-case-in">
+ <h2><a href="#section--then-in-case-in"><code>case</code> - <code>in</code> における <code>then</code></a></h2>
+ <p>
+ ようやく本題にたどり着いた。来る Ruby 3.0 では <code>case</code> と <code>in</code> キーワードを使ったパターンマッチングの構文が入る予定である。この構文でもパターン部との区切りとして <code>then</code> 等が必要になる。 (現在の) Ruby には formal な形式での文法仕様は存在しないので、yacc の定義ファイルを参照した (yacc の説明は省略)。
+ </p>
+
+ <p>
+ <a href="https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986">https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986</a>
+ </p>
+
+ <pre class="highlight" language="yacc" linenumbering="unnumbered"><code>p_case_body : keyword_in
+{
+ SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
+ p-&gt;command_start = FALSE;
+ $&lt;ctxt&gt;1 = p-&gt;ctxt;
+ p-&gt;ctxt.in_kwarg = 1;
+ $&lt;tbl&gt;$ = push_pvtbl(p);
+}
+{
+ $&lt;tbl&gt;$ = push_pktbl(p);
+}
+p_top_expr then
+{
+ pop_pktbl(p, $&lt;tbl&gt;3);
+ pop_pvtbl(p, $&lt;tbl&gt;2);
+ p-&gt;ctxt.in_kwarg = $&lt;ctxt&gt;1.in_kwarg;
+}
+compstmt
+p_cases
+{
+ /*%%%*/
+ $$ = NEW_IN($4, $7, $8, &amp;@$);
+ /*% %*/
+ /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
+}
+;</code></pre>
+
+ <p>
+ 簡略版:
+ </p>
+
+ <pre class="highlight" language="yacc" linenumbering="unnumbered"><code>p_case_body : keyword_in p_top_expr then compstmt p_cases
+;</code></pre>
+
+ <p>
+ ここで、<code>keyword_in</code> は文字通り <code>in</code>、<code>p_top_expr</code> はいわゆるパターン、<code>then</code> は <code>then</code> キーワードのことではなく、この記事で <code>then</code> 等と呼んでいるもの、つまり <code>then</code> キーワード、<code>;</code>、改行のいずれかである。
+ </p>
+
+ <p>
+ これにより、<code>case</code> - <code>when</code> による従来の構文と同じように、<code>then</code> 等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる:
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">case</span> x
+<span class="hljs-keyword">in</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> a
+<span class="hljs-keyword">in</span> <span class="hljs-number">2</span> <span class="hljs-keyword">then</span> b
+<span class="hljs-keyword">in</span> <span class="hljs-number">3</span> <span class="hljs-keyword">then</span> c
+<span class="hljs-keyword">end</span>
+
+<span class="hljs-keyword">case</span> x
+<span class="hljs-keyword">in</span> <span class="hljs-number">1</span>
+ a
+<span class="hljs-keyword">in</span> <span class="hljs-number">2</span>
+ b
+<span class="hljs-keyword">in</span> <span class="hljs-number">3</span>
+ c
+<span class="hljs-keyword">end</span>
+
+<span class="hljs-keyword">case</span> x
+<span class="hljs-keyword">in</span> <span class="hljs-number">1</span>; a
+<span class="hljs-keyword">in</span> <span class="hljs-number">2</span>; b
+<span class="hljs-keyword">in</span> <span class="hljs-number">3</span>; c
+<span class="hljs-keyword">end</span></code></pre>
+
+ <p>
+ ところで、<code>p_top_expr</code> には <code>if</code> による guard clause が書けるので、その場合は <code>if</code> - <code>then</code> と似たような見た目になる。
+ </p>
+
+ <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">case</span> x
+<span class="hljs-keyword">in</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> a
+<span class="hljs-keyword">in</span> n <span class="hljs-keyword">if</span> n &lt; <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> b
+<span class="hljs-keyword">in</span> n <span class="hljs-keyword">then</span> c
+<span class="hljs-keyword">end</span></code></pre>
+ </section>
+
+ <section id="section--outro">
+ <h2><a href="#section--outro">まとめ</a></h2>
+ <ul>
+ <li>
+ <code>if</code> や <code>case</code> の条件の後ろには <code>then</code>、<code>;</code>、改行のいずれかが必要
+ <ul>
+ <li>
+ 通常は改行しておけばよい
+ </li>
+ </ul>
+ </li>
+
+ <li>
+ 3.0 で入る予定の <code>case</code> - <code>in</code> でも <code>then</code> 等が必要になる
+ </li>
+
+ <li>
+ Ruby の構文を正確に知るには (現状) <code>parse.y</code> を直接読めばよい
+ </li>
+ </ul>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html b/vhosts/blog/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html
new file mode 100644
index 00000000..7509e3a0
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html
@@ -0,0 +1,258 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Rust のプリミティブ型は予約語ではなく普通の識別子である。どのようにこれが名前解決されるのかを調べた。">
+ <meta name="keywords" content="Rust">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>Rust のプリミティブ型はどこからやって来るか | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">Rust のプリミティブ型はどこからやって来るか</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/rust/">Rust</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/9a429432258bbcd6c565">https://qiita.com/nsfisis/items/9a429432258bbcd6c565</a>
+ </div>
+ </div>
+
+ <section id="section--intro">
+ <h2><a href="#section--intro">前置き</a></h2>
+ <p>
+ Rust において、プリミティブ型の名前は予約語でない。したがって、次のコードは合法である。
+ </p>
+
+ <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span>
+<span class="hljs-meta">#![allow(dead_code)]</span>
+
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">char</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i8</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i16</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i32</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i64</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i128</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">isize</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u8</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u16</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u32</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u64</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u128</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">usize</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f32</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f64</span>;
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">str</span>;</code></pre>
+
+ <p>
+ では、普段単に <code>bool</code> と書いたとき、この <code>bool</code> は一体どこから来ているのか。rustc のソースを追ってみた。
+ </p>
+
+ <blockquote>
+ <p>
+ 前提知識: 一般的なコンパイラの構造、用語。<code>rustc</code> そのものの知識は不要 (というよりも筆者自身がよく知らない)
+ </p>
+ </blockquote>
+ </section>
+
+ <section id="section--code-reading">
+ <h2><a href="#section--code-reading">調査</a></h2>
+ <p>
+ 調査に使用したソース (調査時点での最新 master)
+ </p>
+
+ <p>
+ <a href="https://github.com/rust-lang/rust/tree/511ed9f2356af365ad8affe046b3dd33f7ac3c98">https://github.com/rust-lang/rust/tree/511ed9f2356af365ad8affe046b3dd33f7ac3c98</a>
+ </p>
+
+ <p>
+ どのようにして調べるか。rustc の構造には詳しくないため、すぐに当たりをつけるのは難しい。
+ </p>
+
+ <p>
+ 大雑把な構造としては、<code>compiler</code> フォルダ以下に <code>rustc_*</code> という名前のクレートが数十個入っている。これがどうやら <code>rustc</code> コマンドの実装部のようだ。
+ </p>
+
+ <p>
+ <code>rustc</code> はセルフホストされている (= <code>rustc</code> 自身が Rust で書かれている) ので、<code>bool</code> や <code>char</code> などで適当に検索をかけてもノイズが多すぎて話にならない。 しかし、お誂え向きなことに <code>i128</code>/<code>u128</code> というコンパイラ自身が使うことがなさそうな型が存在するのでこれを使って <code>git grep</code> してみる。
+ </p>
+
+ <pre class="highlight monospaced"><code>$ git grep &quot;\bi128\b&quot; | wc # i128
+165 1069 15790
+
+$ git grep &quot;\bu128\b&quot; | wc # u128
+293 2127 26667
+
+$ git grep &quot;\bbool\b&quot; | wc # cf. bool の結果
+3563 23577 294659</code></pre>
+
+ <p>
+ 165 程度であれば探すことができそうだ。今回は、クレート名を見ておおよその当たりをつけた。
+ </p>
+
+ <pre class="highlight monospaced"><code>$ git grep &quot;\bi128\b&quot;
+...
+rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128));
+...</code></pre>
+
+ <p>
+ <code>rustc_resolve</code> というのはいかにも名前解決を担いそうなクレート名である。該当箇所を見てみる。
+ </p>
+
+ <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/// Interns the names of the primitive types.</span>
+<span class="hljs-comment">///</span>
+<span class="hljs-comment">/// All other types are defined somewhere and possibly imported, but the primitive ones need</span>
+<span class="hljs-comment">/// special handling, since they have no place of origin.</span>
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">PrimitiveTypeTable</span> {
+ primitive_types: FxHashMap&lt;Symbol, PrimTy&gt;,
+}
+
+<span class="hljs-keyword">impl</span> <span class="hljs-title class_">PrimitiveTypeTable</span> {
+ <span class="hljs-keyword">fn</span> <span class="hljs-title function_">new</span>() <span class="hljs-punctuation">-&gt;</span> PrimitiveTypeTable {
+ <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">table</span> = FxHashMap::<span class="hljs-title function_ invoke__">default</span>();
+
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">bool</span>, Bool);
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">char</span>, Char);
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f32</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F32));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f64</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F64));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">isize</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::Isize));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i8</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I8));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i16</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I16));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i32</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I32));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i64</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I64));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i128</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I128));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">str</span>, Str);
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">usize</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::Usize));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u8</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U8));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u16</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U16));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u32</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U32));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u64</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U64));
+ table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u128</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U128));
+ <span class="hljs-keyword">Self</span> { primitive_types: table }
+ }
+}</code></pre>
+
+ <p>
+ これは初めに列挙したプリミティブ型の一覧と一致している。doc comment にも、
+ </p>
+
+ <blockquote>
+ <p>
+ All other types are defined somewhere and possibly imported, but the primitive ones need special handling, since they have no place of origin.
+ </p>
+ </blockquote>
+
+ <p>
+ とある。次はこの struct の使用箇所を追う。追うと言っても使われている箇所は次の一箇所しかない。なお説明に不要な箇所は大きく削っている。
+ </p>
+
+ <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.</span>
+<span class="hljs-comment">/// (略)</span>
+<span class="hljs-keyword">fn</span> <span class="hljs-title function_">resolve_ident_in_lexical_scope</span>(
+ &amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>,
+ <span class="hljs-keyword">mut</span> ident: Ident,
+ ns: Namespace,
+ <span class="hljs-comment">// (略)</span>
+) <span class="hljs-punctuation">-&gt;</span> <span class="hljs-type">Option</span>&lt;LexicalScopeBinding&lt;<span class="hljs-symbol">&#x27;a</span>&gt;&gt; {
+ <span class="hljs-comment">// (略)</span>
+
+ <span class="hljs-keyword">if</span> ns == TypeNS {
+ <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> <span class="hljs-variable">Some</span>(prim_ty) = <span class="hljs-keyword">self</span>.primitive_type_table.primitive_types.<span class="hljs-title function_ invoke__">get</span>(&amp;ident.name) {
+ <span class="hljs-keyword">let</span> <span class="hljs-variable">binding</span> =
+ (Res::<span class="hljs-title function_ invoke__">PrimTy</span>(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::<span class="hljs-title function_ invoke__">root</span>())
+ .<span class="hljs-title function_ invoke__">to_name_binding</span>(<span class="hljs-keyword">self</span>.arenas);
+ <span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">Some</span>(LexicalScopeBinding::<span class="hljs-title function_ invoke__">Item</span>(binding));
+ }
+ }
+
+ <span class="hljs-literal">None</span>
+}</code></pre>
+
+ <p>
+ 関数名や doc comment が示している通り、この関数は識別子 (identifier, ident) を現在のレキシカルスコープ内で解決 (resolve) する。 <code>if ns == TypeNS</code> のブロック内では、<code>primitive_type_table</code> (上記の <code>PrimitiveTypeTable::new()</code> で作られた変数) に含まれている識別子 (<code>bool</code>、<code>i32</code> など) かどうか判定し、そうであればそれに紐づけられたプリミティブ型を返している。
+ </p>
+
+ <p>
+ なお、<code>ns</code> は「名前空間」を示す変数である。Rust における名前空間はC言語におけるそれとほとんど同じで、今探している名前が関数名/変数名なのか型なのかマクロなのかを区別している。この <code>if</code> は、プリミティブ型に解決されるのは型を探しているときだけだ、と言っている。
+ </p>
+
+ <p>
+ 重要なのは、これが <code>resolve_ident_in_lexical_scope()</code> の最後に書かれている点である。つまり、最初に挙げたプリミティブ型の識別子は、「名前解決の最終段階で」、「他に同名の型が見つかっていなければ」プリミティブ型として解決される。
+ </p>
+
+ <p>
+ 動作がわかったところで、例として次のコードを考える。
+ </p>
+
+ <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span>
+
+<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>;
+
+<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() {
+ <span class="hljs-keyword">let</span> <span class="hljs-variable">_</span>: <span class="hljs-type">bool</span> = <span class="hljs-type">bool</span>;
+}</code></pre>
+
+ <p>
+ ここで <code>main()</code> の <code>bool</code> は <code>struct bool</code> として解決される。なぜなら、プリミティブ型の判定をする前に <code>bool</code> という名前の別の型が見つかるからだ。
+ </p>
+ </section>
+
+ <section id="section--outro">
+ <h2><a href="#section--outro">まとめ</a></h2>
+ <p>
+ Rust のプリミティブ型は予約語ではない。名前解決の最終段階で特別扱いされ、他に同名の型が見つかっていなければ対応するプリミティブ型に解決される。
+ </p>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html b/vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html
new file mode 100644
index 00000000..ea874004
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html
@@ -0,0 +1,224 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Vim の autocmd events における BufWrite/BufWritePre がどう違うのかを調べた結果、違いはないことがわかった。">
+ <meta name="keywords" content="Vim">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【Vim】 autocmd events の BufWrite/BufWritePre の違い | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【Vim】 autocmd events の BufWrite/BufWritePre の違い</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/vim/">Vim</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/79ab4db8564032de0b25">https://qiita.com/nsfisis/items/79ab4db8564032de0b25</a>
+ </div>
+ </div>
+
+ <section id="section--tl-dr">
+ <h2><a href="#section--tl-dr">TL; DR</a></h2>
+ <p>
+ 違いはない。ただのエイリアス。
+ </p>
+ </section>
+
+ <section id="section--code-reading">
+ <h2><a href="#section--code-reading">調査記録</a></h2>
+ <p>
+ Vim の autocmd events には似通った名前のものがいくつかある。大抵は <code>:help</code> に説明があるが、この記事のタイトルにある2つを含めた以下のイベントには、その違いについて説明がない。
+ </p>
+
+ <ul>
+ <li>
+ <code>BufRead</code>/<code>BufReadPost</code>
+ </li>
+
+ <li>
+ <code>BufWrite</code>/<code>BufWritePre</code>
+ </li>
+
+ <li>
+ <code>BufAdd</code>/<code>BufCreate</code>
+ </li>
+ </ul>
+
+ <p>
+ このうち、<code>BufAdd</code>/<code>BufCreate</code> に関しては、<code>:help BufCreate</code> に
+ </p>
+
+ <blockquote>
+ <p>
+ The BufCreate event is for historic reasons.
+ </p>
+ </blockquote>
+
+ <p>
+ とあり、おそらくは <code>BufAdd</code> のエイリアスであろうということがわかる。他の2組も同様ではないかと予想されるが、確認のため vim と neovim のソースコードを調査した。
+ </p>
+
+ <blockquote>
+ <p>
+ ソースコードへのリンク <a href="https://github.com/vim/vim/tree/8e6be34338f13a6a625f19bcef82019c9adc65f2">vim (調査時点での master branch)</a> <a href="https://github.com/neovim/neovim/tree/71d4f5851f068eeb432af34850dddda8cc1c71e3">neovim (上に同じ)</a>
+ </p>
+ </blockquote>
+
+ <section id="section--code-reading--vim">
+ <h3><a href="#section--code-reading--vim">vim のソースコード</a></h3>
+ <p>
+ 以下は、autocmd events の名前と内部で使われている整数値とのマッピングを定義している箇所である。見ての通り、上でエイリアスではないかと述べた3組には、それぞれ同じ内部値が使われている。
+ </p>
+
+ <p>
+ <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L85-L86">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L85-L86</a>
+ </p>
+
+ <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight">{<span class="hljs-string">&quot;BufAdd&quot;</span>, EVENT_BUFADD},
+{<span class="hljs-string">&quot;BufCreate&quot;</span>, EVENT_BUFADD},</code></pre>
+
+ <p>
+ <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L95-L97">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L95-L97</a>
+ </p>
+
+ <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight">{<span class="hljs-string">&quot;BufRead&quot;</span>, EVENT_BUFREADPOST},
+{<span class="hljs-string">&quot;BufReadCmd&quot;</span>, EVENT_BUFREADCMD},
+{<span class="hljs-string">&quot;BufReadPost&quot;</span>, EVENT_BUFREADPOST},</code></pre>
+
+ <p>
+ <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L103-L105">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L103-L105</a>
+ </p>
+
+ <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight">{<span class="hljs-string">&quot;BufWrite&quot;</span>, EVENT_BUFWRITEPRE},
+{<span class="hljs-string">&quot;BufWritePost&quot;</span>, EVENT_BUFWRITEPOST},
+{<span class="hljs-string">&quot;BufWritePre&quot;</span>, EVENT_BUFWRITEPRE},</code></pre>
+ </section>
+
+ <section id="section--code-reading--neovim">
+ <h3><a href="#section--code-reading--neovim">neovim のソースコード</a></h3>
+ <p>
+ neovim の場合でも同様のマッピングが定義されているが、こちらの場合は Lua で書かれている。以下にある通り、はっきり <code>aliases</code> と書かれている。
+ </p>
+
+ <p>
+ <a href="https://github.com/neovim/neovim/blob/71d4f5851f068eeb432af34850dddda8cc1c71e3/src/nvim/auevents.lua#L119-L124">https://github.com/neovim/neovim/blob/71d4f5851f068eeb432af34850dddda8cc1c71e3/src/nvim/auevents.lua#L119-L124</a>
+ </p>
+
+ <pre class="highlight" language="lua" linenumbering="unnumbered"><code class="highlight">aliases = {
+BufCreate = <span class="hljs-string">&#x27;BufAdd&#x27;</span>,
+BufRead = <span class="hljs-string">&#x27;BufReadPost&#x27;</span>,
+BufWrite = <span class="hljs-string">&#x27;BufWritePre&#x27;</span>,
+FileEncoding = <span class="hljs-string">&#x27;EncodingChanged&#x27;</span>,
+},</code></pre>
+
+ <p>
+ ところで、上では取り上げなかった <code>FileEncoding</code> だが、これは <code>:help FileEncoding</code> にしっかりと書いてある。
+ </p>
+
+ <pre class="highlight monospaced"><code> *FileEncoding*
+FileEncoding Obsolete. It still works and is equivalent
+ to |EncodingChanged|.</code></pre>
+ </section>
+ </section>
+
+ <section id="section--outro">
+ <h2><a href="#section--outro">まとめ</a></h2>
+ <p>
+ 記事タイトルについて言えば、どちらも変わらないので好きな方を使えばよい。あえて言えば、次のようになるだろう。
+ </p>
+
+ <ul>
+ <li>
+ <code>BufAdd</code>/<code>BufCreate</code>
+ <ul>
+ <li>
+ → <code>BufCreate</code> は歴史的な理由により (&quot;for historic reasons&quot;) 存在しているため、新しい方 (<code>BufAdd</code>) を使う
+ </li>
+ </ul>
+ </li>
+
+ <li>
+ <code>BufRead</code>/<code>BufReadPost</code>
+ <ul>
+ <li>
+ → <code>BufReadPre</code> との対称性のため、あるいは <code>BufWritePost</code> との対称性のため <code>BufReadPost</code> を使う
+ </li>
+ </ul>
+ </li>
+
+ <li>
+ <code>BufWrite</code>/<code>BufWritePre</code>
+ <ul>
+ <li>
+ → <code>BufWritePost</code> との対称性のため、あるいは <code>BufReadPre</code> との対称性のため <code>BufWritePre</code> を使う
+ </li>
+ </ul>
+ </li>
+
+ <li>
+ <code>FileEncoding</code>/<code>EncodingChanged</code>
+ <ul>
+ <li>
+ → <code>FileEncoding</code> は <code>`Obsolete&apos;&apos; と明言されているので、`EncodingChanged</code> を使う
+ </li>
+ </ul>
+ </li>
+ </ul>
+
+ <p>
+ ところでこの調査で知ったのだが、<code>BufRead</code> と <code>BufWrite</code> は上にある通り発火するタイミングが「後」と「前」で対称性がない。可能なら <code>Pre</code>/<code>Post</code> 付きのものを使った方が分かりやすいだろう。
+ </p>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>
diff --git a/vhosts/blog/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html b/vhosts/blog/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html
new file mode 100644
index 00000000..9ca04808
--- /dev/null
+++ b/vhosts/blog/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html
@@ -0,0 +1,253 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="Vim で選択した行の順番を入れ替える方法。">
+ <meta name="keywords" content="Vim">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>Vimで選択した行の順番を入れ替える | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">Vimで選択した行の順番を入れ替える</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/vim/">Vim</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ この記事は Qiita から移植してきたものです。 元 URL: <a href="https://qiita.com/nsfisis/items/4fefb361d9a693803520">https://qiita.com/nsfisis/items/4fefb361d9a693803520</a>
+ </div>
+ </div>
+
+ <section id="section--tl-dr">
+ <h2><a href="#section--tl-dr">TL; DR</a></h2>
+ <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">&quot; License: Public Domain</span>
+
+command! -bar -<span class="hljs-built_in">range</span>=%
+ \ Reverse
+ \ keeppatterns <span class="hljs-symbol">&lt;line1&gt;</span>,<span class="hljs-symbol">&lt;line2&gt;</span>g/^/<span class="hljs-keyword">m</span><span class="hljs-symbol">&lt;line1&gt;</span>-<span class="hljs-number">1</span></code></pre>
+ </section>
+
+ <section id="section--version">
+ <h2><a href="#section--version">バージョン情報</a></h2>
+ <p>
+ <code>:version</code> の一部
+ </p>
+
+ <blockquote>
+ <p>
+ VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jan 26 2020 11:30:30) macOS version Included patches: 1-148 Huge version without GUI.
+ </p>
+ </blockquote>
+ </section>
+
+ <section id="section--existing-solution">
+ <h2><a href="#section--existing-solution">よく紹介されている手法</a></h2>
+ <section id="section--existing-solution--external-commands">
+ <h3><a href="#section--existing-solution--external-commands"><code>tac</code> / <code>tail</code></a></h3>
+ <p>
+ <code>tac</code> や <code>tail -r</code> などの外部コマンドを <code>!</code> を使って呼び出し、置き換える。
+ </p>
+
+ <blockquote>
+ <p>
+ :h v_!
+ </p>
+ </blockquote>
+
+ <p>
+ <code>tac</code> コマンドや <code>tail</code> の <code>-r</code> オプションは環境によって利用できないことがあり、複数の環境を行き来する場合に採用しづらい
+ </p>
+ </section>
+
+ <section id="section--existing-solution--global-command">
+ <h3><a href="#section--existing-solution--global-command"><code>:g/^/m0</code></a></h3>
+ <p>
+ こちらは外部コマンドに頼らず、Vim の機能のみを使う。<code>g</code> は <code>:global</code> コマンドの、<code>m</code> は <code>:move</code> コマンドの略
+ </p>
+
+ <p>
+ <code>:global</code> コマンドは <code>:[range]global/{pattern}/[command]</code> のように使い、<code>[range]</code> で指定された範囲の行のうち、<code>{pattern}</code> で指定された検索パターンにマッチする行に対して、順番に <code>[command]</code> で指定された Ex コマンドを呼び出す。
+ </p>
+
+ <blockquote>
+ <p>
+ :h :global
+ </p>
+ </blockquote>
+
+ <p>
+ <code>:move</code> コマンドは <code>[range]:move {address}</code> のように使い、<code>[range]</code> で指定された範囲の行を <code>{address}</code> で指定された位置に移動させる。
+ </p>
+
+ <blockquote>
+ <p>
+ :h :move
+ </p>
+ </blockquote>
+
+ <p>
+ <code>:g/^/m0</code> のように組み合わせると、「すべての行を1行ずつ 0行目(1行目の上)に動かす」という動きをする。これは確かに行の入れ替えになっている。
+ </p>
+
+ <p>
+ なお、<code>:g/^/m0</code> は全ての行を入れ替えるが、<code>:N,Mg/^/mN-1</code> とすることで N行目から M行目を処理範囲とするよう拡張できる。手でこれを入力するわけにはいかないので、次のようなコマンドを用意する。
+ </p>
+
+ <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">command! -bar -<span class="hljs-built_in">range</span>=%
+ \ Reverse
+ \ <span class="hljs-symbol">&lt;line1&gt;</span>,<span class="hljs-symbol">&lt;line2&gt;</span>g/^/<span class="hljs-keyword">m</span><span class="hljs-symbol">&lt;line1&gt;</span>-<span class="hljs-number">1</span></code></pre>
+
+ <p>
+ これは望みの動作をするが、実際に実行してみると全行がハイライトされてしまう。次節で詳細を述べる。
+ </p>
+ </section>
+ </section>
+
+ <section id="section--problem-of-global-command">
+ <h2><a href="#section--problem-of-global-command"><code>:g/^/m0</code> の問題点</a></h2>
+ <p>
+ <code>:global</code> コマンドは各行に対してマッチングを行う際、現在の検索パターンを上書きしてしまう。<code>^</code> は行の先頭にマッチするため、結果として全ての行がハイライトされてしまう。<code>&apos;hlsearch&apos;</code> オプションを無効にしている場合その限りではないが、その場合でも直前の検索パターンが失われてしまうと <code>n</code> コマンドなどの際に不便である。
+ </p>
+
+ <blockquote>
+ <p>
+ :h @/
+ </p>
+ </blockquote>
+ </section>
+
+ <section id="section--solution">
+ <h2><a href="#section--solution">解決策</a></h2>
+ <blockquote>
+ <p>
+ [2020/9/28追記] より簡潔な方法を見つけたので次節に追記した
+ </p>
+ </blockquote>
+
+ <p>
+ 前述した <code>:Reverse</code> コマンドの定義を少し変えて、次のようにする:
+ </p>
+
+ <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">function!</span> <span class="hljs-title">s</span>:reverse_lines<span class="hljs-params">(from, to)</span> abort
+ <span class="hljs-keyword">execute</span> <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d,%dg/^/m%d&quot;</span>, <span class="hljs-variable">a:from</span>, <span class="hljs-variable">a:to</span>, <span class="hljs-variable">a:from</span> - <span class="hljs-number">1</span>)
+<span class="hljs-keyword">endfunction</span>
+
+command! -bar -<span class="hljs-built_in">range</span>=%
+ \ Reverse
+ \ <span class="hljs-keyword">call</span> <span class="hljs-symbol">&lt;SID&gt;</span>reverse_lines(<span class="hljs-symbol">&lt;line1&gt;</span>, <span class="hljs-symbol">&lt;line2&gt;</span>)</code></pre>
+
+ <p>
+ 実行しているコマンドが変わったわけではないが、関数呼び出しを経由するようにした。これだけで前述の問題が解決する。
+ </p>
+
+ <p>
+ この理由は、ユーザー定義関数を実行する際は検索パターンが一度保存され、実行が終了したあと復元されるため。結果として検索パターンが <code>^</code> で上書きされることがなくなる。
+ </p>
+
+ <p>
+ Vim のヘルプから該当箇所を引用する (強調は筆者による)。
+ </p>
+
+ <blockquote>
+ <p>
+ :h autocmd-searchpat
+ </p>
+
+ <p>
+ <em role="strong">Autocommands do not change the current search patterns.</em> Vim saves the current search patterns before executing autocommands then restores them after the autocommands finish. This means that autocommands do not affect the strings highlighted with the `hlsearch&apos; option.
+ </p>
+ </blockquote>
+
+ <p>
+ これは autocommand の実行に関しての記述だが、これと同じことがユーザー定義関数の実行時にも適用される。このことは <code>:nohlsearch</code> のヘルプにある。同じく該当箇所を引用する (強調は筆者による)。
+ </p>
+
+ <blockquote>
+ <p>
+ :h :nohlsearch
+ </p>
+
+ <p>
+ (略) This command doesn’t work in an autocommand, because the highlighting state is saved and restored when executing autocommands |autocmd-searchpat|. <em role="strong">Same thing for when invoking a user function.</em>
+ </p>
+ </blockquote>
+
+ <p>
+ この仕様により、<code>:g/^/m0</code> の呼び出しをユーザー定義関数に切り出すことで上述の問題を解決できる。
+ </p>
+ </section>
+
+ <section id="section--solution-revised">
+ <h2><a href="#section--solution-revised">解決策 (改訂版)</a></h2>
+ <blockquote>
+ <p>
+ [2020/9/28追記] より簡潔な方法を見つけたため追記する
+ </p>
+ </blockquote>
+
+ <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">command! -bar -<span class="hljs-built_in">range</span>=%
+ \ Reverse
+ \ keeppatterns <span class="hljs-symbol">&lt;line1&gt;</span>,<span class="hljs-symbol">&lt;line2&gt;</span>g/^/<span class="hljs-keyword">m</span><span class="hljs-symbol">&lt;line1&gt;</span>-<span class="hljs-number">1</span></code></pre>
+
+ <p>
+ まさにこのための Exコマンド、<code>:keeppatterns</code> が存在する。<code>:keeppatterns {command}</code> のように使い、読んで字の如く、後ろに続く Exコマンドを「現在の検索パターンを保ったまま」実行する。はるかに分かりやすく意図を表現できる。
+ </p>
+
+ <blockquote>
+ <p>
+ :h :keeppatterns
+ </p>
+ </blockquote>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>