diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-09-07 22:27:48 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-09-07 22:35:53 +0900 |
| commit | 994e0114d76ae19768d5c303874a968cf6369fd0 (patch) | |
| tree | 5fd3f8b169eea00084b24fbae820f75273864d2a /vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre | |
| parent | 57f015992f678bfd7281f171fb9d71349c96a1a0 (diff) | |
| download | nsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.tar.gz nsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.tar.zst nsfisis.dev-994e0114d76ae19768d5c303874a968cf6369fd0.zip | |
meta: migrate to monorepo
Diffstat (limited to 'vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre')
| -rw-r--r-- | vhosts/blog/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html | 224 |
1 files changed, 224 insertions, 0 deletions
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="© 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">"BufAdd"</span>, EVENT_BUFADD}, +{<span class="hljs-string">"BufCreate"</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">"BufRead"</span>, EVENT_BUFREADPOST}, +{<span class="hljs-string">"BufReadCmd"</span>, EVENT_BUFREADCMD}, +{<span class="hljs-string">"BufReadPost"</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">"BufWrite"</span>, EVENT_BUFWRITEPRE}, +{<span class="hljs-string">"BufWritePost"</span>, EVENT_BUFWRITEPOST}, +{<span class="hljs-string">"BufWritePre"</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">'BufAdd'</span>, +BufRead = <span class="hljs-string">'BufReadPost'</span>, +BufWrite = <span class="hljs-string">'BufWritePre'</span>, +FileEncoding = <span class="hljs-string">'EncodingChanged'</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> は歴史的な理由により ("for historic reasons") 存在しているため、新しい方 (<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'' と明言されているので、`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"> + © 2021 nsfisis + </footer> + </body> +</html> |
