Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:Scratch コマンドの、ファイル名指定 write 時に、scratch解除するハンドラが 動作していない #30

Closed
KSR-Yasuda opened this issue Apr 15, 2021 · 11 comments
Assignees
Labels

Comments

@KSR-Yasuda
Copy link

:Scratchコマンド
で作成した scratchバッファの、
ファイル書込み時の scratch解除のハンドル (s:CheckScratchWritten()) が、期待通り 動作していないようです。

s:CheckScratchWritten() の期待としては:
scratchバッファを :write NEW_FILE.txt として 新しいファイルを作った時に、
scratch解除して、新しい NEW_FILE.txt のバッファ としたい;
ということだと思います。

が、どうも、scratchバッファを :write NEW_FILE.txt しても、
buffer名 (%) が変わらず、scratchの解除条件 に引っ掛からないようです。

以前は 動作していた気がするので、vim本体側の仕様変更 かなにかではないかと。

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jul 24 2020 11:05:54)
MS-Windows 64 ビット GUI/コンソール 版
適用済パッチ: 1-1287
Modified by [email protected]
Compiled by [email protected]
@KSR-Yasuda
Copy link
Author

buftype=nofile の設定を、Vimの挙動によらず s:CheckScratchWritten() のスクリプト側でいじるの は良くない気がするので;
buftype=nofile を手動解除した上で writeしたケース を考慮したほうが妥当かも。

  • setl buftype=write NEW_FILE.txt
  • setl buftype=f NEW_FILE.txtwrite
    • いずれも、%NEW_FILE.txt になっているはず
 function! s:CheckScratchWritten()
-  if &buftype ==# 'nofile' && expand('%').'x' !=# 'x' && exists('b:cmdex_scratch') && b:cmdex_scratch == 1
-    setlocal buftype= swapfile
+  if exists('b:cmdex_scratch') && b:cmdex_scratch == 1 && expand('%').'x' !=# 'x'
+    set buftype< swapfile<
     unlet b:cmdex_scratch
   endif
 endfunction

@koron
Copy link
Owner

koron commented Oct 3, 2022

9.0.639 で再現を確認しました。

Scratchバッファを :w foo.txt した際に
BufWritePost のタイミングでは buftype=nofile ではなくなってるらしい。
(こちらは未確認で推測)

さらに推測だが
以前は Scratch バッファでの :w foo.txt 自体はバッファを作らなかった ≒ バッファ名を変えて保存していたが
現在はバッファはいじらず別名で保存するという処理になっていると考えられる。

どうするかはまだ未定。なにが起こってるのか要検証。

@koron
Copy link
Owner

koron commented Oct 3, 2022

たぶんコレが原因で挙動が変わったんやなかろか?
vim/vim#5807

@koron
Copy link
Owner

koron commented Oct 3, 2022

何が起こったかはおおよそわかった。
どう対処するかは要検討。

@koron
Copy link
Owner

koron commented Oct 3, 2022

BufWritePost のタイミングでは buftype=nofile ではなくなってるらしい。

これは間違い。bt=nofileのままだった。

現在はバッファはいじらず別名で保存するという処理になっている

これはあってた。よって expand('%').'x' !=# 'x' が合致せず。
つまり bt=nofile のとき :w foo.txt でバッファに名前が付かなくなってる、と。

これ自体はVimの挙動変更なので、尊重する。
つまり Scratch バッファに名前付けする方法が変更になった。
言い換えると :write ではScratchバッファに名前はつけられない。

逆に Scratch バッファに名前を付ける正しい方法は何か?
その際、正しくScratch バッファが解除されるかが問題。

@koron
Copy link
Owner

koron commented Oct 3, 2022

無名バッファについては :w foo.txt でバッファに foo.txt という名前が付く。
無名かつbuftype=nofileなバッファについては :w foo.txt でもバッファに名前は付かない。

この非直交性はdocumentedかしら? intendedかしら?

@koron
Copy link
Owner

koron commented Oct 3, 2022

 	nofile only:	The buffer name is fixed, it is not handled like a
 			file name.  It is not modified in response to a |:cd|
 			command.

:help 'buftype' より。intendedだった。

@koron
Copy link
Owner

koron commented Oct 3, 2022

expand('<afile>') を見て、空でなければ解除か。

@koron
Copy link
Owner

koron commented Oct 3, 2022

s:CheckScratchWritten() を次のように変えると、以前の動作に近づきそう。

以下のテストをしてる

  • :w - バッファ名を省略して書き込みに失敗する
  • :w foo.txt - foo.txtは存在せず、ファイルの新規作成&書き込み&scratchの解除(名前付け)に成功する
  • :w exist.txt - exist.txtが存在するので、ファイルの上書きに失敗する
  • :w! exist.txt - 既存の exist.txt を上書きし、scratchの解除(名前付け)に成功する
function! s:CheckScratchWritten()
  if &buftype ==# 'nofile' && get(b:, 'cmdex_scratch', 0) == 1 && expand('<afile>') !=# ''
    unlet b:cmdex_scratch
    setlocal buftype= swapfile
    execute "edit!" expand('<afile>')
  endif
endfunction

差分

 function! s:CheckScratchWritten()
-  if &buftype ==# 'nofile' && expand('%').'x' !=# 'x' && exists('b:cmdex_scratch') && b:cmdex_scratch == 1
-    setlocal buftype= swapfile
+  if &buftype ==# 'nofile' && get(b:, 'cmdex_scratch', 0) == 1 && expand('<afile>') !=# ''
     unlet b:cmdex_scratch
+    setlocal buftype= swapfile
+    execute "edit!" expand('<afile>')
   endif

koron added a commit that referenced this issue Oct 3, 2022
@koron
Copy link
Owner

koron commented Oct 3, 2022

9.0 で修正予定。
8.2系列にはバックポートしないので、上述の差分をユーザーごとに適用してもらう

以上、本件は終了します。

@koron koron closed this as completed Oct 3, 2022
@koron koron self-assigned this Oct 3, 2022
@koron koron added the bug label Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants