vim 的 expand("<afile>:p") 问题 [解决]

Vim、Emacs配置和使用
回复
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

vim 的 expand("<afile>:p") 问题 [解决]

#1

帖子 自由建客 » 2011-05-09 16:56

代码: 全选

fun s:Load(sid, ref)
	if a:ref>1
		return
	endif
	augroup filetypelib
		au FileType * call s:ReSet()
		au BufUnload * call s:UnSet()
	augroup END
endfun

fun s:Free(sid, ref)
	if a:ref>1
		return
	endif
	augroup filetypelib
		au! FileType *
		au! BufUnload *
	augroup END
endfun

fun s:ReSet()
	call s:UnSet()
	let buf=expand("<afile>:p")
	let ft=expand("<amatch>")
	call setbufvar(buf, "filetype_bak", ft)
	call Lib_LoadAll("filetype/".ft.".vim", buf)
endfun

fun s:UnSet()
	let buf=expand("<afile>:p")
	let ft=getbufvar(buf, "filetype_bak")
	if ft!=""
		call Lib_FreeAll("filetype/".ft.".vim", buf)
		call setbufvar(buf, "filetype_bak", "")
	endif
endfun
对于磁盘文件没有任何问题,但对于刚启动 vim 时或 :new 时创建的“未命名缓冲区”就不行了,:set ft=c 就说“E94: 没有匹配的缓冲区”。

代码: 全选

:h E94

:bdelete[!] {bufname}						*E93* *E94*
		类似 ":bdelete[!] [N]",但是使用名字指定缓冲区。
		注意 如果缓冲区名字是一个数字,不能使用名字来访问;而应用其编
		号代替。如果名字里含有空格,在空格前插入反斜杠。
对于刚启动的 vim 我可以假定其未命名缓冲区编号为 1,但对于 :new 创建出来的没法假设。
附件
1.png
上次由 自由建客 在 2011-05-10 0:32,总共编辑 1 次。
头像
fanhe
帖子: 2357
注册时间: 2007-03-24 23:45

Re: vim 的 expand("<afile>:p") 问题

#2

帖子 fanhe » 2011-05-09 19:15

你都自动命令了,你还用setbufvar干嘛, 直接setlocal
要么你就用 bufexists 判断一下
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

Re: vim 的 expand("<afile>:p") 问题

#3

帖子 自由建客 » 2011-05-09 20:20

setlocal 不行,当我 :bdel 某后台缓冲区的时候会设置当前缓冲区,这显然不对

代码: 全选

	let buf=str2nr(expand("<abuf>"))
:em20
回复