Skip to content

Commit

Permalink
Merge pull request #1105 from techee/vim_more_ex_commands
Browse files Browse the repository at this point in the history
vimode: add some more ex commands
  • Loading branch information
frlan committed Sep 29, 2021
2 parents 002938a + 21835b7 commit b7eb47d
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 72 deletions.
12 changes: 12 additions & 0 deletions vimode/README
Expand Up @@ -550,16 +550,28 @@ a new command, please do not forget to update the table below.::
tag command action
------------------------------------------------------------------------------
:& :& repeat last ":substitute"
:< :< shift lines one 'shiftwidth' left
:> :> shift lines one 'shiftwidth' right
:copy :co[py] copy lines
:cquit :cq[uit] quit Vim with an error code
:delete :d[elete] delete lines
:exit :exi[t] same as ":xit"
:join :j[oin] join lines
:move :m[ove] move lines
:put :pu[t] insert contents of register in the text
:quit :q[uit] quit current window (when one window quit Vim)
:quitall :quita[ll] quit Vim
:qall :qa[ll] quit Vim
:redo :red[o] redo one undone change
:substitute :s[ubstitute] find and replace text
:t :t same as ":copy"
:undo :u[ndo] undo last change(s)
:update :up[date] write buffer if modified
:write :w[rite] write to a file
:wall :wa[ll] write all (changed) buffers
:wq :wq write to a file and quit window or Vim
:wqall :wqa[ll] write all changed buffers and quit Vim
:xit :x[it] write if buffer changed and quit window or Vim
:xall :xa[ll] same as ":wqall"
:yank :y[ank] yank lines into a register
:~ :~ repeat last ":substitute"
4 changes: 2 additions & 2 deletions vimode/src/Makefile.am
Expand Up @@ -36,8 +36,8 @@ vi_srcs = \
cmds/special.c \
cmds/edit.h \
cmds/edit.c \
excmds/excmds.h \
excmds/excmds.c
cmds/excmds.h \
cmds/excmds.c

vimode_la_SOURCES = \
backends/backend-geany.c \
Expand Down
2 changes: 1 addition & 1 deletion vimode/src/cmd-params.c
Expand Up @@ -26,7 +26,7 @@ void cmd_params_init(CmdParams *param, ScintillaObject *sci,

param->num = num;
param->num_present = num_present;
param->last_kp = g_slist_nth_data(kpl, 0);
param->last_kp = kpl != NULL ? g_slist_nth_data(kpl, 0) : NULL;
param->is_operator_cmd = is_operator_cmd;

param->sel_start = sel_start;
Expand Down
163 changes: 163 additions & 0 deletions vimode/src/cmds/excmds.c
@@ -0,0 +1,163 @@
/*
* Copyright 2018 Jiri Techet <techet@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "cmds/excmds.h"
#include "cmds/edit.h"
#include "utils.h"

void excmd_save(CmdContext *c, ExCmdParams *p)
{
c->cb->on_save(p->force);
}


void excmd_save_all(CmdContext *c, ExCmdParams *p)
{
c->cb->on_save_all(p->force);
}


void excmd_quit(CmdContext *c, ExCmdParams *p)
{
c->cb->on_quit(p->force);
}


void excmd_save_quit(CmdContext *c, ExCmdParams *p)
{
if (c->cb->on_save(p->force))
c->cb->on_quit(p->force);
}


void excmd_save_all_quit(CmdContext *c, ExCmdParams *p)
{
if (c->cb->on_save_all(p->force))
c->cb->on_quit(p->force);
}


void excmd_repeat_subst(CmdContext *c, ExCmdParams *p)
{
const gchar *flags = p->param1;
if (!flags)
flags = "g";
perform_substitute(c->sci, c->substitute_text, p->range_from, p->range_to, flags);
}


void excmd_repeat_subst_orig_flags(CmdContext *c, ExCmdParams *p)
{
perform_substitute(c->sci, c->substitute_text, p->range_from, p->range_to, NULL);
}


static void prepare_cmd_params(CmdParams *params, CmdContext *c, ExCmdParams *p)
{
gint start = SSM(c->sci, SCI_POSITIONFROMLINE, p->range_from, 0);
SET_POS(c->sci, start, TRUE);
cmd_params_init(params, c->sci, p->range_to - p->range_from + 1, FALSE, NULL, FALSE, 0, 0);
}


void excmd_yank(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_copy_line(c, &params);
}


void excmd_put(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_paste_after(c, &params);
}


void excmd_undo(CmdContext *c, ExCmdParams *p)
{
SSM(c->sci, SCI_UNDO, 0, 0);
}


void excmd_redo(CmdContext *c, ExCmdParams *p)
{
SSM(c->sci, SCI_REDO, 0, 0);
}


void excmd_shift_left(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_unindent(c, &params);
}


void excmd_shift_right(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_indent(c, &params);
}


void excmd_delete(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_delete_line(c, &params);
}

void excmd_join(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
prepare_cmd_params(&params, c, p);
cmd_join_lines(c, &params);
}


void excmd_copy(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
gint dest = SSM(c->sci, SCI_POSITIONFROMLINE, p->dest, 0);
excmd_yank(c, p);
SET_POS(c->sci, dest, TRUE);
cmd_params_init(&params, c->sci, 1, FALSE, NULL, FALSE, 0, 0);
cmd_paste_after(c, &params);
}


void excmd_move(CmdContext *c, ExCmdParams *p)
{
CmdParams params;
gint dest;

if (p->dest >= p->range_from && p->dest <= p->range_to)
return;

excmd_delete(c, p);
if (p->dest > p->range_to)
p->dest -= p->range_to - p->range_from + 1;
dest = SSM(c->sci, SCI_POSITIONFROMLINE, p->dest, 0);
SET_POS(c->sci, dest, TRUE);
cmd_params_init(&params, c->sci, 1, FALSE, NULL, FALSE, 0, 0);
cmd_paste_after(c, &params);
}
14 changes: 12 additions & 2 deletions vimode/src/excmds/excmds.h → vimode/src/cmds/excmds.h
Expand Up @@ -16,8 +16,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef __VIMODE_EXCMDS_EXCMDS_H__
#define __VIMODE_EXCMDS_EXCMDS_H__
#ifndef __VIMODE_CMDS_EXCMDS_H__
#define __VIMODE_CMDS_EXCMDS_H__

#include "excmd-params.h"
#include "context.h"
Expand All @@ -29,5 +29,15 @@ void excmd_save_quit(CmdContext *c, ExCmdParams *p);
void excmd_save_all_quit(CmdContext *c, ExCmdParams *p);
void excmd_repeat_subst(CmdContext *c, ExCmdParams *p);
void excmd_repeat_subst_orig_flags(CmdContext *c, ExCmdParams *p);
void excmd_yank(CmdContext *c, ExCmdParams *p);
void excmd_put(CmdContext *c, ExCmdParams *p);
void excmd_undo(CmdContext *c, ExCmdParams *p);
void excmd_redo(CmdContext *c, ExCmdParams *p);
void excmd_shift_left(CmdContext *c, ExCmdParams *p);
void excmd_shift_right(CmdContext *c, ExCmdParams *p);
void excmd_delete(CmdContext *c, ExCmdParams *p);
void excmd_join(CmdContext *c, ExCmdParams *p);
void excmd_copy(CmdContext *c, ExCmdParams *p);
void excmd_move(CmdContext *c, ExCmdParams *p);

#endif
2 changes: 2 additions & 0 deletions vimode/src/excmd-params.h
Expand Up @@ -30,6 +30,8 @@ typedef struct
/* ex range start and end */
gint range_from;
gint range_to;
/* "address" destination for copy/move */
gint dest;
} ExCmdParams;

typedef void (*ExCmd)(CmdContext *c, ExCmdParams *p);
Expand Down
33 changes: 32 additions & 1 deletion vimode/src/excmd-runner.c
Expand Up @@ -18,7 +18,7 @@

#include "excmd-runner.h"
#include "excmd-params.h"
#include "excmds/excmds.h"
#include "cmds/excmds.h"
#include "utils.h"

#include <string.h>
Expand Down Expand Up @@ -64,8 +64,35 @@ ExCmdDef ex_cmds[] = {
{excmd_repeat_subst, "s"},
{excmd_repeat_subst, "substitute"},
{excmd_repeat_subst, "&"},
{excmd_repeat_subst, "~"},
{excmd_repeat_subst_orig_flags, "&&"},

{excmd_yank, "yank"},
{excmd_yank, "y"},
{excmd_put, "put"},
{excmd_put, "pu"},

{excmd_undo, "undo"},
{excmd_undo, "u"},
{excmd_redo, "redo"},
{excmd_redo, "red"},

{excmd_shift_left, "<"},
{excmd_shift_right, ">"},

{excmd_delete, "delete"},
{excmd_delete, "d"},

{excmd_join, "join"},
{excmd_join, "j"},

{excmd_copy, "copy"},
{excmd_copy, "co"},
{excmd_copy, "t"},

{excmd_move, "move"},
{excmd_move, "m"},

{NULL, NULL}
};

Expand Down Expand Up @@ -413,7 +440,11 @@ static void perform_simple_ex_cmd(CmdContext *ctx, const gchar *cmd)
ExCmdDef *def = &ex_cmds[i];
if (strcmp(def->name, cmd_name) == 0)
{
if (def->cmd == excmd_copy || def->cmd == excmd_move)
parse_ex_range(&(params.param1), ctx, &(params.dest), &(params.dest));
SSM(ctx->sci, SCI_BEGINUNDOACTION, 0, 0);
def->cmd(ctx, &params);
SSM(ctx->sci, SCI_ENDUNDOACTION, 0, 0);
break;
}
}
Expand Down
66 changes: 0 additions & 66 deletions vimode/src/excmds/excmds.c

This file was deleted.

0 comments on commit b7eb47d

Please sign in to comment.