首页 > 其他分享 >从vimdiff get命令为什么不是dg看vim cmd解析

从vimdiff get命令为什么不是dg看vim cmd解析

时间:2024-08-09 19:27:41浏览次数:15  
标签:cmd get int dg cap vim operator nv op

intro

当使用vimdiff来获取另外一个文件的diff内容时,在Ex模式下使用的是diffget,但是在normal模式下对应的cmd却不是对应的dg而是另一个do(diff obtain),这个都少有些意外。

单单的对于"为什么vim使用do而不是dg命令来获得diff?"这个问题,其实在vim的“do”帮助文档中已经明确说明:

[count]do Same as ":diffget" without range. The "o" stands for "obtain"
("dg" can't be used, it could be the start of "dgg"!). Note:
this doesn't work in Visual mode.
If you give a [count], it is used as the [bufspec] argument
for ":diffget".

简言之,就是因为dg可能是dgg命令的前缀,当然,dg也可能是其它(例如dge)这种命令的前缀。因为d是一个operator,而g可以是很多移动命令(motion)的引导符,"operator + motion"是vim经典的操作模式。

类似于这种operator和多个字符组成的命令(例如gg,zO等)vim是如何解析的?尽管g开始的命令可能有很多,但是也不是g后面加任意一个字符都是合法的vim命令,如果出现vim不识别的双字符(例如gA),此时vim会把把A作为单独的Append吗(当然,单单这个问题在vim中试一下就知道了)?

如果不是"operator+motion"的输入而是"operator+operator"输入组合,vim会如何处理?

Operator-pending

在执行一个命令前,vim会将收集的信息保存在一个cmdarg_T结构中,这个结构中除了意料之中的cmdchar字段之外,还有一个额外的oparg_T结构。明显的,oparg_T结构中的op_type字段对应的就是operator类型。

//@file: vim\src\structs.h
/*
 * Arguments for operators.
 */
typedef struct oparg_S
{
    int		op_type;	// current pending operator type
    int		regname;	// register to use for the operator
    int		motion_type;	// type of the current cursor motion
    int		motion_force;	// force motion type: 'v', 'V' or CTRL-V
    int		use_reg_one;	// TRUE if delete uses reg 1 even when not
				// linewise
    int		inclusive;	// TRUE if char motion is inclusive (only
				// valid when motion_type is MCHAR)
    int		end_adjusted;	// backuped b_op_end one char (only used by
				// do_format())
    pos_T	start;		// start of the operator
    pos_T	end;		// end of the operator
    pos_T	cursor_start;	// cursor position before motion for "gw"

    long	line_count;	// number of lines from op_start to op_end
				// (inclusive)
    int		empty;		// op_start and op_end the same (only used by
				// do_change())
    int		is_VIsual;	// operator on Visual area
    int		block_mode;	// current operator is Visual block mode
    colnr_T	start_vcol;	// start col for block mode operator
    colnr_T	end_vcol;	// end col for block mode operator
    long	prev_opcount;	// ca.opcount saved for K_CURSORHOLD
    long	prev_count0;	// ca.count0 saved for K_CURSORHOLD
    int		excl_tr_ws;	// exclude trailing whitespace for yank of a
				// block
} oparg_T;

/*
 * Arguments for Normal mode commands.
 */
typedef struct cmdarg_S
{
    oparg_T	*oap;		// Operator arguments
    int		prechar;	// prefix character (optional, always 'g')
    int		cmdchar;	// command character
    int		nchar;		// next command character (optional)
    int		ncharC1;	// first composing character (optional)
    int		ncharC2;	// second composing character (optional)
    int		extra_char;	// yet another character (optional)
    long	opcount;	// count before an operator
    long	count0;		// count before command, default 0
    long	count1;		// count before command, default 1
    int		arg;		// extra argument from nv_cmds[]
    int		retval;		// return: CA_* values
    char_u	*searchbuf;	// return: pointer to search pattern or NULL
} cmdarg_T;

operator的解析和非operator的解析经过相同的流程,只是如果输入的是一个operator的话,这个信息不是保存在cmdarg_T的cmdchar字段,而是保存在了oparg_T结构中的op_type字段中,或者说,operator的相关信息主要保存在单独的oparg_T结构中。

这意味着:vim必须识别并区分处理哪些是operator

关于“operator和非operator经过相同流程”,在vim的介绍文档intro.txt中就有说明

Operator-pending mode This is like Normal mode, but after an operator
command has started, and Vim is waiting for a {motion}
to specify the text that the operator will work on.

对应的在vim的实现中,所谓的“pending"就是先pending到了单独的oparg_T结构中。

operator

operator的识别经过的是和常规cmd相同的流程:当读取到一个字符的时候,会从nv_cmds表查找到对应的命令项。这些命令行中包括了一些flags和对应的执行函数。大家常见的change、delete等operator命令,它们对应的都是nv_operator函数。

// Values for cmd_flags.
#define NV_NCH	    0x01	  // may need to get a second char
#define NV_NCH_NOP  (0x02|NV_NCH) // get second char when no operator pending
#define NV_NCH_ALW  (0x04|NV_NCH) // always get a second char
#define NV_LANG	    0x08	// second char needs language adjustment

#define NV_SS	    0x10	// may start selection
#define NV_SSS	    0x20	// may start selection with shift modifier
#define NV_STS	    0x40	// may stop selection without shift modif.
#define NV_RL	    0x80	// 'rightleft' modifies command
#define NV_KEEPREG  0x100	// don't clear regname
#define NV_NCW	    0x200	// not allowed in command-line window

/*
 * Generally speaking, every Normal mode command should either clear any
 * pending operator (with *clearop*()), or set the motion type variable
 * oap->motion_type.
 *
 * When a cursor motion command is made, it is marked as being a character or
 * line oriented motion.  Then, if an operator is in effect, the operation
 * becomes character or line oriented accordingly.
 */

/*
 * This table contains one entry for every Normal or Visual mode command.
 * The order doesn't matter, this will be sorted by the create_nvcmdidx.vim
 * script to generate the nv_cmd_idx[] lookup table.
 * It is faster when all keys from zero to '~' are present.
 */
static const struct nv_cmd
{
    int		cmd_char;	// (first) command character
    nv_func_T   cmd_func;	// function for this command
    short_u	cmd_flags;	// NV_ flags
    short	cmd_arg;	// value for ca.arg
} nv_cmds[] =

#else  // DO_DECLARE_NVCMD

/*
 * Used when creating nv_cmdidxs.h.
 */
# define NVCMD(a, b, c, d)  a
static const int nv_cmds[] =

#endif // DO_DECLARE_NVCMD
{
///...
    NVCMD(' ',		nv_right,	0,			0),
    NVCMD('!',		nv_operator,	0,			0),
    NVCMD('"',		nv_regname,	NV_NCH_NOP|NV_KEEPREG,	0),
    NVCMD('#',		nv_ident,	0,			0),
///...
    NVCMD('c',		nv_operator,	0,			0),
    NVCMD('d',		nv_operator,	0,			0),
///...
    NVCMD('f',		nv_csearch,	NV_NCH_ALW|NV_LANG,	FORWARD),
    NVCMD('g',		nv_g_cmd,	NV_NCH_ALW,		FALSE),
///...
    NVCMD('y',		nv_operator,	0,			0),
    NVCMD('z',		nv_zet,		NV_NCH_ALW,		0),
    NVCMD('{',		nv_findpar,	0,			BACKWARD),
///...
}

在nv_operator函数中,会从operator表中查找对应的动作,并把operator记录到cap->oap结构中。

nv_operator函数实现还有一个有意思的细节:如果有个operator处于pending状态,此时再次输入一个operator动作,那么两个动作会抵消,也就是新输入的operator也不生效。


/*
 * Check for operator active and clear it.
 *
 * Beep and return TRUE if an operator was active.
 */
    static int
checkclearop(oparg_T *oap)
{
    if (oap->op_type == OP_NOP)
	return FALSE;
    clearopbeep(oap);
    return TRUE;
}

/*
 * Handle an operator command.
 * The actual work is done by do_pending_operator().
 */
    static void
nv_operator(cmdarg_T *cap)
{
    int	    op_type;

    op_type = get_op_type(cap->cmdchar, cap->nchar);
#ifdef FEAT_JOB_CHANNEL
    if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
    {
	clearopbeep(cap->oap);
	return;
    }
#endif

    if (op_type == cap->oap->op_type)	    // double operator works on lines
	nv_lineop(cap);
    else if (!checkclearop(cap->oap))
    {
	cap->oap->start = curwin->w_cursor;
	cap->oap->op_type = op_type;
#ifdef FEAT_EVAL
	set_op_var(op_type);
#endif
    }
}

text object

在vim中有一些不是基于motion确定的范围,而是使用textobject选择范围。vim的text-objects文档说明了它的作用。

This is a series of commands that can only be used while in Visual mode or
after an operator. The commands that start with "a" select "a"n object
including white space, the commands starting with "i" select an "inner" object
without white space, or just the white space. Thus the "inner" commands
always select less text than the "a" commands.

可以注意到,其中的a和i都是vim已经存在的append和insert命令。当vim读到一个a字符的时候如何处理呢?当知道了vim的pending operator是单独存储的时候,就可以推测实现方法:当有operator pending的时候就认为是一个object,否则即使一个cmd。


/*
 * Handle "A", "a", "I", "i" and <Insert> commands.
 * Also handle K_PS, start bracketed paste.
 */
    static void
nv_edit(cmdarg_T *cap)
{
    // <Insert> is equal to "i"
    if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
	cap->cmdchar = 'i';

    // in Visual mode "A" and "I" are an operator
    if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
    {
#ifdef FEAT_TERMINAL
	if (term_in_normal_mode())
	{
	    end_visual_mode();
	    clearop(cap->oap);
	    term_enter_job_mode();
	    return;
	}
#endif
	v_visop(cap);
    }

    // in Visual mode and after an operator "a" and "i" are for text objects
    else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
	    && (cap->oap->op_type != OP_NOP || VIsual_active))
    {
	nv_object(cap);
    }

NV_NCH_ALW

在vim的命令配置表中,还有一些条目配置了NV_NCH_ALW属性,这个选项表示“always get a second char”,而一些常见的扩展,g、z、]、[ 引导的命令簇,它们都是必定要求第二个字符的。并且当第二个字符如果是不识别的合法组合,会不清除pending的operator,并且不会退回第二个字符进行再次处理。


/*
 * Commands starting with "g".
 */
    static void
nv_g_cmd(cmdarg_T *cap)
{
    oparg_T	*oap = cap->oap;
    int		i;

    switch (cap->nchar)
    {
    case '+':
    case '-': // "g+" and "g-": undo or redo along the timeline
	if (!checkclearopq(oap))
	    undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
							 FALSE, FALSE, FALSE);
	break;

    default:
	clearopbeep(oap);
	break;
    }
}    
    ///...

do/dp

在vimdiff模式下,do和dp都是vim的内置命令(而不是该模式下通过map实现的功能),但是由于d、o、p都是vim中已经存在的命令,所以在这两个看起来很常规的命令实现中,同样是vim内部做了“谨慎"的处理(take care)。


/*
 * "o" and "O" commands.
 */
    static void
nv_open(cmdarg_T *cap)
{
#ifdef FEAT_DIFF
    // "do" is ":diffget"
    if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
    {
	clearop(cap->oap);
	nv_diffgetput(FALSE, cap->opcount);
    }
    else
#endif
    if (VIsual_active)  // switch start and end of visual
	v_swap_corners(cap->cmdchar);
#ifdef FEAT_JOB_CHANNEL
    else if (bt_prompt(curbuf))
	clearopbeep(cap->oap);
#endif
    else
	n_opencmd(cap);
}

/*
 * "P", "gP", "p" and "gp" commands.
 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
 */
    static void
nv_put_opt(cmdarg_T *cap, int fix_indent)
{
    int		regname = 0;
    void	*reg1 = NULL, *reg2 = NULL;
    int		empty = FALSE;
    int		was_visual = FALSE;
    int		dir;
    int		flags = 0;
    int		keep_registers = FALSE;
#ifdef FEAT_FOLDING
    int		save_fen = curwin->w_p_fen;
#endif

    if (cap->oap->op_type != OP_NOP)
    {
#ifdef FEAT_DIFF
	// "dp" is ":diffput"
	if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
	{
	    clearop(cap->oap);
	    nv_diffgetput(TRUE, cap->opcount);
	}
	else
#endif
	    clearopbeep(cap->oap);
	return;
    }
}

回到主题

因为d是一个operator,所以它跟后面的g引导的命令是没有依赖关系的。当遇到g的时候,该字符配置了NV_NCH_ALW属性,也就是g之后必须再输入一个字符(即使不是一个合法的g命令)。

另外,diff模式下使用的][也是配置了NV_NCH_ALW选项。

outro

简言之: operator是一种特殊的cmd:特殊的地方在于它的信息是记录在单独的和常规cmd独立的存储位置。

一些看似基本甚至理所当然的功能,在实现的过程中可能都经过了权衡和妥协。

标签:cmd,get,int,dg,cap,vim,operator,nv,op
From: https://www.cnblogs.com/tsecer/p/18351381

相关文章

  • edge浏览器加载java插件的方法
    在MicrosoftEdge浏览器中直接加载Java插件并不是一个直接支持的功能,因为Edge是基于Chromium内核的浏览器,主要支持Web技术如HTML、CSS和JavaScript。Java插件(通常指的是Java小程序,使用Java编程语言编写的应用程序)主要用于在早期的InternetExplorer浏览器中运行,但在现代浏览器中已......
  • 支持S3协议的S3cmd工具简单使用
    本文分享自天翼云开发者社区《支持S3协议的S3cmd工具简单使用》,作者:付****健一:安装方法#wgethttp://nchc.dl.sourceforge.net/project/s3tools/s3cmd/1.0.0/s3cmd-1.0.0.tar.gz#tar-zxfs3cmd-1.0.0.tar.gz-C/usr/local/#mv/usr/local/s3cmd-1.0.0//usr/local......
  • @JsonAnyGetter 注解
    @JsonAnyGetter注解在Jackson中,@JsonAnyGetter注解用于指示Jackson在序列化过程中取得对象动态属性的方法。它的作用是将动态属性以键值对的形式包含在序列化结果中。1.1@JsonAnyGetter注解的要求使用@JsonAnyGetter注解的方法必须满足以下要求:方法必须是公共的方......
  • Nuget 管理器》》 error: NU1101: 找不到包 ViewFaceCore
    error:NU1101:找不到包ViewFaceCore错误解释:NU1101错误表示NuGet无法找到名为ViewFaceCore的包。这通常意味着包不存在于指定的源中,或者包名称拼写错误。解决方法:检查包名称:确保ViewFaceCore是正确的包名,没有拼写错误。检查源:确保你的NuGet配置包含了......
  • OpenCV图像滤波(7)cv::getDerivKernels() 函数的使用
    操作系统:ubuntu22.04OpenCV版本:OpenCV4.9IDE:VisualStudioCode编程语言:C++11算法描述函数返回用于计算空间图像导数的滤波系数。该函数计算并返回用于空间图像导数的滤波系数。当ksize=FILTER_SCHARR时,生成Scharr3x3核(参见Scharr)。否则,生成Sobel核(参见Sob......
  • Neo4j 实现一个简单的CMDB管理平台
    简介Neo4j是一个高性能的图形数据库管理系统,它使用图形模型来存储和查询数据。图形数据库与传统的关系型数据库不同,它们使用节点和边来表示数据实体和它们之间的关系,而不是使用表格和行,可以使用neo4j实现权限系统,知识图谱,cmdb等部署dockerrun-d--name=neo4j\--publis......
  • Linux-USB驱动笔记-Gadget Function驱动
    1、前言在Linux-USB驱动笔记(四)–USB整体框架中有説到GadgetFunction驱动,下面我们来具体看一下。GadgetFunction就是指设备的功能,比如作为U盘,需要文件存储的功能,则需要FileStorage驱动,这个驱动也称为Function驱动。2、GadgetFunction驱动Function驱动只是利用通用的API,并......
  • qt之QTableWidget按列遍历数据
    QObject::connect(ui->tableWidget_4,&QTableWidget::itemSelectionChanged,[=](){QList<QTableWidgetItem*>selectedItems=ui->tableWidget_4->selectedItems();if(!selectedItems.isEmpty()){in......
  • Edge实验性功能中文翻译
    平行下载启用平行下载以加速下载速度。-Mac,Windows,Linux,Android#enable-parallel-downloading已启用临时恢复M125标记临时恢复M125中过期的标记。这些标记将很快被删除。-Mac,Windows,Linux,Android#temporary-unexpire-flags-m125默认临时恢复M126标记......
  • [Typescript] tsconfig libs and target
    Intsconfigfile,youhave targetand libsconfiguration.Youalwaysneedtodefinea target,recommendedas es2022Specifyingthe lib optionalsoletsusdrilldownintothespecificfeaturesandlibrarieswewanttoincludeinourproject,whichwewill......