[patch] getcmdwintype()

84 views
Skip to first unread message

Jacob Niehus

unread,
Jul 25, 2014, 3:04:09 PM7/25/14
to vim...@googlegroups.com
I think this is a useful function to allow different mappings depending on the type of command window (ex, search, input, etc.). I can add documentation if people agree.

-Jake

diff --git a/src/eval.c b/src/eval.c
--- a/src/eval.c
+++ b/src/eval.c
@@ -554,6 +554,7 @@
static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
@@ -7984,6 +7985,7 @@
{"getcmdline", 0, 0, f_getcmdline},
{"getcmdpos", 0, 0, f_getcmdpos},
{"getcmdtype", 0, 0, f_getcmdtype},
+ {"getcmdwintype", 0, 0, f_getcmdwintype},
{"getcurpos", 0, 0, f_getcurpos},
{"getcwd", 0, 0, f_getcwd},
{"getfontname", 0, 1, f_getfontname},
@@ -11503,6 +11505,26 @@
}

/*
+ * "getcmdwintype()" function
+ */
+ static void
+f_getcmdwintype(argvars, rettv)
+ typval_T *argvars UNUSED;
+ typval_T *rettv;
+{
+ rettv->v_type = VAR_STRING;
+ rettv->vval.v_string = NULL;
+#ifdef FEAT_CMDWIN
+ rettv->vval.v_string = alloc(2);
+ if (rettv->vval.v_string != NULL)
+ {
+ rettv->vval.v_string[0] = cmdwin_type;
+ rettv->vval.v_string[1] = NUL;
+ }
+#endif
+}
+
+/*
* "getcwd()" function
*/
static void

vim-7.4.383-getcmdwintype.patch

Jacob Niehus

unread,
Jul 25, 2014, 3:35:00 PM7/25/14
to vim...@googlegroups.com
I should add that I know it's possible to do maps based on the CmdwinEnter autocommand, but here's an example where this is useful. I have a map to delete a history entry from the command line window. The way to do that now is to have several autocommands to modify the map with which type of history it should delete from like this:

autocmd CmdwinEnter : nnoremap DD :call histdel(':', ...
autocmd CmdwinEnter / nnoremap DD :call histdel('/', ...
autocmd CmdwinEnter @ nnoremap DD :call histdel('@', ...
etc.


This function removes the need for that autocommand completely in this case, allowing:

nnoremap DD :call histdel(getcmdwintype(), ...

James McCoy

unread,
Jul 25, 2014, 8:05:21 PM7/25/14
to vim...@googlegroups.com
On Fri, Jul 25, 2014 at 12:04:09PM -0700, Jacob Niehus wrote:
> I think this is a useful function to allow different mappings
> depending on the type of command window (ex, search, input, etc.). I
> can add documentation if people agree.

This is just reimplementing getcmdtype().

Cheers,
--
James
GPG Key: 4096R/331BA3DB 2011-12-05 James McCoy <jame...@jamessan.com>

Павлов Николай Александрович

unread,
Jul 25, 2014, 8:10:58 PM7/25/14
to vim...@googlegroups.com, James McCoy
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
>--
>--
>You received this message from the "vim_dev" maillist.
>Do not top-post! Type your reply below the text you are replying to.
>For more information, visit http://www.vim.org/maillist.php
>
>---
>You received this message because you are subscribed to the Google
>Groups "vim_dev" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to vim_dev+u...@googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.

No. This is for q/ and q:. getcmdtype() returns empty string in normal mode in these windows (checked with

nmap <expr> e ':echomsg "'.string(getcmdtype()).'"<cr>'

).
-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJT0vIGGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSviiKD/99zGYDUC57IQwTs7lH85NvMnNJxhIns0uiqrvJUjQME64YfvKAbByo
9SGAFhcJTQtKexydJXjGN0bo6jefOWuONmFay3mW0BlgCRZFK6qC9iUFajjhGc6o
p3j6tkjEkna19a+dR/YNzTxwbnyNCu1LX0vqgR48E5aVneuoQ5GRVpHDpCJy+Ibx
JhnAdFBloaFeLwf3b+c64cQmMIXkNk+vKb8V97bd5Dauw3FoVAS6/Gp0TlRIwhTm
Zc6tQTDzdiPXIxexIjA4y2bH8iqyZc+Oj0aZ434Rs1OAVpre/gfOB73SGEuuFPfG
5LlwgL65a6wzFW5rk6YW4hm8kMadzyUu/l0huxm8hjTmtu4mh/DXd91hdANNyNKP
DO3qS0bbpAunUK+WNtRC9kTk+grYShJlIDBSA/MEbJs5VhbQi8322cgMyF1bJzKQ
tTtm+lotozY8Dqsi9O0lmAMMXA6ry0u1VNIWwqdDF35fg/AN3WIOrN2Hjy08J3nK
wbzO5xFyj1QLmrfx3ZiAzvshOLU0UpKSo/xecHaoAt1VhaI/bYmlflZKCCtT7N3a
fZ7//bnsHfWVJC5MBD0sKtMOtwHmOSBEaeMDbWfmhCrY+rs1Du/zuA45ZmqMe5Tf
QvJw4iiXf9BNjd1MkEogVDNhCi/y7NxMqJZxNZ3kPvRRVmPhlVdFtw==
=zMyM
-----END PGP SIGNATURE-----

Jacob Niehus

unread,
Jul 25, 2014, 8:18:22 PM7/25/14
to vim...@googlegroups.com, jame...@jamessan.com
Kind of, except this is for the command line window, not the command line. getcmdtype() doesn't give the type of command line window you're in, and it shouldn't because you can be in the command line window and use the command line for a different type of command. In that case the two functions will return different results (e.g. running an ex command while editing a search pattern).

Павлов Николай Александрович

unread,
Jul 25, 2014, 8:18:49 PM7/25/14
to vim...@googlegroups.com, Jacob Niehus
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

I think there is at least one compiler or static analyzer among used to compile or check Vim which will complain here. You should move = NULL to #else.

>+ if (rettv->vval.v_string != NULL)
>+ {
>+ rettv->vval.v_string[0] = cmdwin_type;
>+ rettv->vval.v_string[1] = NUL;
>+ }

If I am not mistaking the whole alloc+if sequence may be replaced with vim_strnsave(&cmdwin_type, 1). Though this is kinda hack, you should probably ignore the suggestion.

>+#endif
>+}
>+
>+/*
> * "getcwd()" function
> */
> static void

-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJT0vPfGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvo5+D/93YSMDrtzhOiZBlKc+2FbHQBOSU0rvrOx5Vkex39f/mk+AmeLJH1tv
wAgQG9iHIVlTIV6FyhwhmP1Muq6NuejlVSUQMnIJEhEscw+1trRIasP8xq8/EyyW
TpiTVQkvzRR0glBM1kg3rh+KhUIKy7dqGTjtxFDSVd00BzLG5365q7roLeCrF0ep
j6pGbz0vP6obtU00++tVOJErb5Uzk5OkgixWDweqAzg7ri/Hu+piUGCTIILuBx2V
m05DQAmdTs9SySZQSXeuGhcbVlP8iZkMp/MPMSYQf/Wb9VvdMVimeBUeLMUyC3Bs
u1F5hgRrgV735ra6hzwdAlTP1EiaB/hD2dKqw+QQmwpD9u/xDlnGC25emlWpBGbx
BbN/pdSiAbBz7LTUDFoLX+mFEUYq8XbuxI/qqowzm7CcVUCVWx5l1Ykcfzca1P/w
fMeuP3IWfU4z7igCapd5r2PsMHCxWJOzdeTjvsh+y/EBKryV5H4QJMkrwb24VqfA
l6NSu11mWB10SKzoIZn/c8xEFkYlIUxiH6eQjaqqSCMCWwIgcHeKy9orkZ+JF4tl
nkh+ee+0vser/Dje8HPguG8N5L+v+3zA6IZtRHDzj5TROq6Z7Fnm04E4Ky4YXTK9
4HfLT7L4kzZlG/xW2wo5iQvjsMmol+smowa72Ck5ltA/b9lE2Lz2PA==
=oV3V
-----END PGP SIGNATURE-----

Bram Moolenaar

unread,
Jul 26, 2014, 8:23:52 AM7/26/14
to Jacob Niehus, vim...@googlegroups.com

Jacob Niehus wrote:

> I think this is a useful function to allow different mappings depending
> on the type of command window (ex, search, input, etc.). I can add
> documentation if people agree.

I suppose it could be useful.

For adding new features, it's actually a good idea to write the
documentation first. Explaining something to other users may reveal a
flaw in how it works.


--
BLACK KNIGHT: I'm invincible!
ARTHUR: You're a looney.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Andy Wokula

unread,
Jul 26, 2014, 9:40:31 AM7/26/14
to vim...@googlegroups.com
Am 25.07.2014 21:04, schrieb Jacob Niehus:
> I think this is a useful function to allow different mappings
> depending on the type of command window (ex, search, input, etc.). I
> can add documentation if people agree.
>
> -Jake

You can already check expand("<afile>") on CmdWinEnter.

Seems to be buggy for `q/', for me it returns `\' then.

--
Andy

Jacob Niehus

unread,
Jul 26, 2014, 1:29:56 PM7/26/14
to vim...@googlegroups.com, anw...@yahoo.de
> Seems to be buggy for `q/', for me it returns `\' then.
>
>
>
> --
>
> Andy

Andy,

My function is buggy or expand("<afile>") is buggy? I tried:

au CmdwinEnter * let g:foo = expand('<afile>')

and

au CmdwinEnter * let g:foo = getcmdwintype()

both gave the same result ('/'). Anyway I think there's still value in the function because it can check if the current buffer is the command-line window and doesn't require an autocommand.

Here's an updated patch that updates the docs and syntax file.

vim-7.4.383-getcmdwintype-with-docs.patch
Reply all
Reply to author
Forward
0 new messages