Re: ksh equivalent to shell-expand-line

2018-10-13 Thread Hajime Edakawa
Forgive me for sending so many email.

I've rewritten code again one last time.  I'm not sure if this is correct.
I was able to learn shell through these emails. I really appreciate it.

Best regards,
Hajime Edakawa

Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.85
diff -u -p -r1.85 emacs.c
--- emacs.c 18 Jun 2018 17:03:58 -  1.85
+++ emacs.c 13 Oct 2018 06:04:46 -
@@ -201,6 +201,7 @@ static int  x_fold_lower(int);
 static int x_fold_upper(int);
 static int x_set_arg(int);
 static int x_comment(int);
+static int x_expand_alias_sub(int);
 #ifdef DEBUG
 static int x_debug_info(int);
 #endif
@@ -258,6 +259,7 @@ static const struct x_ftab x_ftab[] = {
{ x_fold_upper, "upcase-word",  XF_ARG },
{ x_set_arg,"set-arg",  XF_NOBIND },
{ x_comment,"comment",  0 },
+   { x_expand_alias_sub,   "expand-alias-substitute",  0 },
{ 0, 0, 0 },
 #ifdef DEBUG
{ x_debug_info, "debug-info",   0 },
@@ -1492,6 +1494,7 @@ x_init_emacs(void)
kb_add(x_comp_file, CTRL('['), CTRL('X'), 0);
kb_add(x_comp_list, CTRL('I'), 0);
kb_add(x_comp_list, CTRL('['), '=', 0);
+   kb_add(x_expand_alias_sub,  CTRL('['), CTRL('E'), 0);
kb_add(x_del_back,  CTRL('?'), 0);
kb_add(x_del_back,  CTRL('H'), 0);
kb_add(x_del_char,  CTRL('['), '[', '3', '~', 0);
/* delete */
@@ -1982,6 +1985,95 @@ x_comment(int c)
return KSTD;
 }

+static int
+x_expand_alias_sub(int c)
+{
+   struct tbl *tp;
+   Area *saved_atemp;
+   const char *sep = str_val(local("IFS", 0));
+   char s[2];
+   char *cp;
+   char *begin, *word;
+   char *tmp, *tok, *state;
+   char *input, *output;
+   int skip = 0;
+   int ch, bufsize;
+   size_t len = 0;
+
+   if (xep == xbuf) {
+   x_e_putc(BEL);
+   return KSTD;
+   }
+
+   cp = xbuf;
+   while (cp != xep && ctype(*cp, C_IFSWS))
+   skip += x_size(*cp++);
+
+   begin = cp;
+   if (cp != xep && letter(*cp)) {
+   cp++;
+   len++;
+   while (cp != xep && letnum(*cp)) {
+   cp++;
+   len++;
+   }
+   }
+
+   if (len > 0) {
+   word = str_nsave(begin, len, ATEMP);
+   tp = ktsearch(, word, hash(word));
+   if (tp && (tp->flag & ISSET)) {
+   tmp = str_save(tp->val.s, ATEMP);
+   tok = strtok_r(tmp, sep, );
+   if (tok && strcmp(word, tok)) {
+   x_goto(xbuf + skip);
+   x_delete(len, false);
+   x_ins(tp->val.s);
+   }
+   afree(tmp, ATEMP);
+   }
+   afree(word, ATEMP);
+   }
+
+   input = str_save(xbuf, ATEMP);
+   saved_atemp = ATEMP;
+   newenv(E_ERRH);
+   if (sigsetjmp(genv->jbuf, 0))
+   output = NULL;
+   else
+   output = str_save(substitute(input, 0), saved_atemp);
+   quitenv(NULL);
+
+   if (!output) {
+   x_e_putc(BEL);
+   return KSTD;
+   }
+
+   bufsize = x_size_str(xbuf);
+   x_goto(xbuf);
+   x_delete(bufsize, false);
+
+   s[0] = ' ';
+   s[1] = '\0';
+   while (isspace((unsigned char)*output)) {
+   output++;
+   x_ins(s);
+   }
+
+   while ((ch = *output++)) {
+   if (isprint(ch)) {
+   s[0] = ch;
+   s[1] = '\0';
+   x_ins(s);
+   } else if (ch == '\r' || ch == '\n') {
+   s[0] = ' ';
+   s[1] = '\0';
+   x_ins(s);
+   }
+   }
+
+   return KSTD;
+}

 /* NAME:
  *  x_prev_histword - recover word from prev command
>
> I send the email once again because missing tab space.
> I'm sorry that to send extra email.
>
> Index: emacs.c
> ===
> RCS file: /cvs/src/bin/ksh/emacs.c,v
> retrieving revision 1.85
> diff -u -p -r1.85 emacs.c
> --- emacs.c 18 Jun 2018 17:03:58 -  1.85
> +++ emacs.c 10 Oct 2018 23:26:49 -
> @@ -201,6 +201,7 @@ static int  x_fold_lower(int);
>  static int x_fold_upper(int);
>  static int x_set_arg(int);
>  static int x_comment(int);
> +static int x_expand_alias_sub(int);
>  #ifdef DEBUG
>  static int x_debug_info(int);
>  #endif
> @@ -258,6 +259,7 @@ static const struct x_ftab x_ftab[] = {
> { x_fold_upper, "upcase-word",  

Re: ksh equivalent to shell-expand-line

2018-10-10 Thread Hajime Edakawa
I send the email once again because missing tab space.
I'm sorry that to send extra email.

Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.85
diff -u -p -r1.85 emacs.c
--- emacs.c 18 Jun 2018 17:03:58 -  1.85
+++ emacs.c 10 Oct 2018 23:26:49 -
@@ -201,6 +201,7 @@ static int  x_fold_lower(int);
 static int x_fold_upper(int);
 static int x_set_arg(int);
 static int x_comment(int);
+static int x_expand_alias_sub(int);
 #ifdef DEBUG
 static int x_debug_info(int);
 #endif
@@ -258,6 +259,7 @@ static const struct x_ftab x_ftab[] = {
{ x_fold_upper, "upcase-word",  XF_ARG },
{ x_set_arg,"set-arg",  XF_NOBIND },
{ x_comment,"comment",  0 },
+   { x_expand_alias_sub,   "expand-alias-substitute",  0 },
{ 0, 0, 0 },
 #ifdef DEBUG
{ x_debug_info, "debug-info",   0 },
@@ -1492,6 +1494,7 @@ x_init_emacs(void)
kb_add(x_comp_file, CTRL('['), CTRL('X'), 0);
kb_add(x_comp_list, CTRL('I'), 0);
kb_add(x_comp_list, CTRL('['), '=', 0);
+   kb_add(x_expand_alias_sub,  CTRL('['), CTRL('E'), 0);
kb_add(x_del_back,  CTRL('?'), 0);
kb_add(x_del_back,  CTRL('H'), 0);
kb_add(x_del_char,  CTRL('['), '[', '3', '~', 0);
/* delete */
@@ -1982,6 +1985,73 @@ x_comment(int c)
return KSTD;
 }

+static int
+x_expand_alias_sub(int c)
+{
+   struct tbl *tp;
+   const char *sep = " \t\n";
+   char *cp;
+   char *word, *name;
+   char *buf, *tok, *state;
+   int skip = 0;
+   int size = 0;
+   size_t len = 0;
+
+   if (xep == xbuf)
+   goto fail;
+
+   cp = xbuf;
+   while (cp != xep && is_mfs(*cp))
+   skip += x_size(*cp++);
+
+   word = cp;
+   while (cp != xep && !is_mfs(*cp)) {
+   size += x_size(*cp++);
+   len++;
+   }
+
+   if (len > 0) {
+   name = strndup(word, len);
+   if (!name) {
+   bi_errorf("unable to allocate memory");
+   goto fail;
+   }
+
+   tp = ktsearch(, name, hash(name));
+   if (tp) {
+   buf = strdup(tp->val.s);
+   if (!buf) {
+   bi_errorf("unable to allocate memory");
+   goto fail;
+   }
+
+   tok = strtok_r(buf, sep, );
+   if (!tok) {
+   bi_errorf("strtok_r failed");
+   goto fail;
+   }
+
+   if (strcmp(name, tok)) {
+   x_goto(xbuf + skip);
+   x_delete(size, false);
+   x_ins(tp->val.s);
+   }
+   }
+   }
+
+   size = x_size_str(xbuf);
+   buf = substitute(xbuf, 0);
+
+   x_goto(xbuf);
+   x_delete(size, false);
+   x_ins(buf);
+   x_adjust();
+
+   return KSTD;
+fail:
+   x_e_putc(BEL);
+   return KSTD;
+}

 /* NAME:
  *  x_prev_histword - recover word from prev command

2018年10月11日(木) 10:07 Hajime Edakawa :
>
> Klemens Nanni wrote:
> >Thanks for your work.
>
> I'm the one who should be thanking OpenBSD developers.
> It's a real honor to hear that!
>
> >With `alias ls=ls\ -l' and successive expand-line invocations: will
> >`ls' be expanded over and over again?
>
> Maybe I fixed the problems that you pointed out.
>
> $ type ls
> ls is an alias for 'ls -CF'
> $ alias ll='ls -l'
> $ ll
> $ ls -l
> $ ls -l
> ...
>
> >This looks nice.
>
> I added some more example.
>
> $ path=/foo/bar/file.txt
> $ alias ll='ls -l'
> $ ll "$(echo $$)" $(( 1 + 1 )) ${path##*/} hoge
> $ ls -l "71788" 2 file.txt hoge
>
> >Did you test it in vi mode as well?
>
> Unfortunately, I've never use vi. I'm really sorry.
> I'd like to study vi sometime.
>
> >Your diff does not apply to -CURRENT (written against 6.3) and lacks
> >documentation updates to ksh(1).
>
> I've tried to add the diff of emacs.c and ksh.1 against -CURRENT.
> The name changes from shell-expand-line to expand-alias-substitute.
> And expand-alias-substitute code was modified.
>
> /* Excuse me if you not understand or you feel bad with my poor English */
>
> Best regards,
> Hajime Edakawa
>
> Index: ksh.1
> ===
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.201
> diff -u -p -r1.201 ksh.1
> --- ksh.1 18 Jun 2018 17:03:58 - 1.201
> +++ ksh.1 10 Oct 2018 23:26:57 -
> @@ -4819,6 +4819,10 @@ if alone on a line; otherwise acts as
>  Error (ring the bell).
>  .It 

Re: ksh equivalent to shell-expand-line

2018-10-10 Thread Hajime Edakawa
Klemens Nanni wrote:
>Thanks for your work.

I'm the one who should be thanking OpenBSD developers.
It's a real honor to hear that!

>With `alias ls=ls\ -l' and successive expand-line invocations: will
>`ls' be expanded over and over again?

Maybe I fixed the problems that you pointed out.

$ type ls
ls is an alias for 'ls -CF'
$ alias ll='ls -l'
$ ll
$ ls -l
$ ls -l
...

>This looks nice.

I added some more example.

$ path=/foo/bar/file.txt
$ alias ll='ls -l'
$ ll "$(echo $$)" $(( 1 + 1 )) ${path##*/} hoge
$ ls -l "71788" 2 file.txt hoge

>Did you test it in vi mode as well?

Unfortunately, I've never use vi. I'm really sorry.
I'd like to study vi sometime.

>Your diff does not apply to -CURRENT (written against 6.3) and lacks
>documentation updates to ksh(1).

I've tried to add the diff of emacs.c and ksh.1 against -CURRENT.
The name changes from shell-expand-line to expand-alias-substitute.
And expand-alias-substitute code was modified.

/* Excuse me if you not understand or you feel bad with my poor English */

Best regards,
Hajime Edakawa

Index: ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.201
diff -u -p -r1.201 ksh.1
--- ksh.1 18 Jun 2018 17:03:58 - 1.201
+++ ksh.1 10 Oct 2018 23:26:57 -
@@ -4819,6 +4819,10 @@ if alone on a line; otherwise acts as
 Error (ring the bell).
 .It exchange-point-and-mark: ^X^X
 Places the cursor where the mark is and sets the mark to where the cursor was.
+.It expand-alias-substitute: ^[^E
+Automatically expands a command alias and substitute parameter,
command, and arithmetic (see
+.Sx Substitution
+below).
 .It expand-file: ^[*
 Appends a
 .Ql *
Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.85
diff -u -p -r1.85 emacs.c
--- emacs.c 18 Jun 2018 17:03:58 - 1.85
+++ emacs.c 10 Oct 2018 23:26:49 -
@@ -201,6 +201,7 @@ static int x_fold_lower(int);
 static int x_fold_upper(int);
 static int x_set_arg(int);
 static int x_comment(int);
+static int x_expand_alias_sub(int);
 #ifdef DEBUG
 static int x_debug_info(int);
 #endif
@@ -258,6 +259,7 @@ static const struct x_ftab x_ftab[] = {
  { x_fold_upper, "upcase-word", XF_ARG },
  { x_set_arg, "set-arg", XF_NOBIND },
  { x_comment, "comment", 0 },
+ { x_expand_alias_sub, "expand-alias-substitute", 0 },
  { 0, 0, 0 },
 #ifdef DEBUG
  { x_debug_info, "debug-info", 0 },
@@ -1492,6 +1494,7 @@ x_init_emacs(void)
  kb_add(x_comp_file, CTRL('['), CTRL('X'), 0);
  kb_add(x_comp_list, CTRL('I'), 0);
  kb_add(x_comp_list, CTRL('['), '=', 0);
+ kb_add(x_expand_alias_sub, CTRL('['), CTRL('E'), 0);
  kb_add(x_del_back, CTRL('?'), 0);
  kb_add(x_del_back, CTRL('H'), 0);
  kb_add(x_del_char, CTRL('['), '[', '3', '~', 0); /* delete */
@@ -1982,6 +1985,73 @@ x_comment(int c)
  return KSTD;
 }

+static int
+x_expand_alias_sub(int c)
+{
+ struct tbl *tp;
+ const char *sep = " \t\n";
+ char *cp;
+ char *word, *name;
+ char *buf, *tok, *state;
+ int skip = 0;
+ int size = 0;
+ size_t len = 0;
+
+ if (xep == xbuf)
+ goto fail;
+
+ cp = xbuf;
+ while (cp != xep && is_mfs(*cp))
+ skip += x_size(*cp++);
+
+ word = cp;
+ while (cp != xep && !is_mfs(*cp)) {
+ size += x_size(*cp++);
+ len++;
+ }
+
+ if (len > 0) {
+ name = strndup(word, len);
+ if (!name) {
+ bi_errorf("unable to allocate memory");
+ goto fail;
+ }
+
+ tp = ktsearch(, name, hash(name));
+ if (tp) {
+ buf = strdup(tp->val.s);
+ if (!buf) {
+ bi_errorf("unable to allocate memory");
+ goto fail;
+ }
+
+ tok = strtok_r(buf, sep, );
+if (!tok) {
+ bi_errorf("strtok_r failed");
+ goto fail;
+ }
+
+ if (strcmp(name, tok)) {
+ x_goto(xbuf + skip);
+ x_delete(size, false);
+ x_ins(tp->val.s);
+ }
+ }
+ }
+
+ size = x_size_str(xbuf);
+ buf = substitute(xbuf, 0);
+
+ x_goto(xbuf);
+ x_delete(size, false);
+ x_ins(buf);
+ x_adjust();
+
+ return KSTD;
+fail:
+ x_e_putc(BEL);
+ return KSTD;
+}

 /* NAME:
  *  x_prev_histword - recover word from prev command
2018年10月11日(木) 2:52 Klemens Nanni :
>
> On Wed, Oct 10, 2018 at 08:58:43AM +0900, Hajime Edakawa wrote:
> > I have challenged to try to make shell_expand_line in ksh.
> > You can check it if you type M-C-e.
> Thanks for your work.
>
> > $ echo "$(echo a b)"
> > $ echo "a b"
> >
> > $ alias ll='ls -l'
> > $ ll $(echo a b) hoge "$(( 1 + 1 ))" ll bar
> > $ ls -l a b hoge "2" ll bar
> This looks nice.
>
> With `alias ls=ls\ -l' and successive expand-line invocations: will
> `ls' be expanded over and over again?
>
> Did you test it in vi mode as well?
>
> > To be honest, I'm not sure if this is correct.
> > I only like OpenBSD, That's why I'm so sorry if they're wrong
> Your diff does not apply to -CURRENT (written against 6.3) and lacks
> documentation updates to ksh(1).



Re: ksh equivalent to shell-expand-line

2018-10-10 Thread Hajime Edakawa
John Ankarström wrote:
>That sounds fabulous!  Thank you for your initiative!  The code looks
>interesting.

Many thanks for your reply message, it made my day!

>It might be interesting to take a look at the source of mksh[1], another
>ksh implementation, which has a similar binding called evaluate-region.

I appreciate your advice which was all new information for me!

Best regards,
Hajime Edakawa
2018年10月11日(木) 0:47 John Ankarström :
>
> Hajime Edakawa wrote:
> > Hello Mr. Ankarström,
> >
> > I have challenged to try to make shell_expand_line in ksh.
> > You can check it if you type M-C-e.
>
> That sounds fabulous!  Thank you for your initiative!  The code looks
> interesting.
>
> > To be honest, I'm not sure if this is correct.
> > I only like OpenBSD, That's why I'm so sorry if they're wrong
>
> It might be interesting to take a look at the source of mksh[1], another
> ksh implementation, which has a similar binding called evaluate-region.
>
> [1]: https://www.mirbsd.org/mksh.htm
>
> Best regards,
> John
>



Re: ksh equivalent to shell-expand-line

2018-10-10 Thread Klemens Nanni
On Wed, Oct 10, 2018 at 08:58:43AM +0900, Hajime Edakawa wrote:
> I have challenged to try to make shell_expand_line in ksh.
> You can check it if you type M-C-e.
Thanks for your work.

> $ echo "$(echo a b)"
> $ echo "a b"
> 
> $ alias ll='ls -l'
> $ ll $(echo a b) hoge "$(( 1 + 1 ))" ll bar
> $ ls -l a b hoge "2" ll bar
This looks nice.

With `alias ls=ls\ -l' and successive expand-line invocations: will
`ls' be expanded over and over again?

Did you test it in vi mode as well?

> To be honest, I'm not sure if this is correct.
> I only like OpenBSD, That's why I'm so sorry if they're wrong
Your diff does not apply to -CURRENT (written against 6.3) and lacks
documentation updates to ksh(1).



Re: ksh equivalent to shell-expand-line

2018-10-10 Thread John Ankarström

Hajime Edakawa wrote:

Hello Mr. Ankarström,

I have challenged to try to make shell_expand_line in ksh.
You can check it if you type M-C-e.


That sounds fabulous!  Thank you for your initiative!  The code looks 
interesting.



To be honest, I'm not sure if this is correct.
I only like OpenBSD, That's why I'm so sorry if they're wrong


It might be interesting to take a look at the source of mksh[1], another 
ksh implementation, which has a similar binding called evaluate-region.


[1]: https://www.mirbsd.org/mksh.htm

Best regards,
John



Re: ksh equivalent to shell-expand-line

2018-10-09 Thread Hajime Edakawa
Hello Mr. Ankarström,

I have challenged to try to make shell_expand_line in ksh.
You can check it if you type M-C-e.

$ echo "$(echo a b)"
$ echo "a b"

$ alias ll='ls -l'
$ ll $(echo a b) hoge "$(( 1 + 1 ))" ll bar
$ ls -l a b hoge "2" ll bar

To be honest, I'm not sure if this is correct.
I only like OpenBSD, That's why I'm so sorry if they're wrong

Best regards,
Hajime Edakawa

Index: emacs.c
===
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -r1.84 emacs.c
--- emacs.c 16 Jan 2018 17:17:18 -  1.84
+++ emacs.c 9 Oct 2018 22:47:44 -
@@ -195,6 +195,7 @@ static int  x_fold_lower(int);
 static int x_fold_upper(int);
 static int x_set_arg(int);
 static int x_comment(int);
+static int x_shell_expand_line(int);
 #ifdef DEBUG
 static int x_debug_info(int);
 #endif
@@ -251,6 +252,7 @@ static const struct x_ftab x_ftab[] = {
{ x_fold_upper, "upcase-word",  XF_ARG },
{ x_set_arg,"set-arg",  XF_NOBIND },
{ x_comment,"comment",  0 },
+   { x_shell_expand_line,  "shell-expand-line",0 },
{ 0, 0, 0 },
 #ifdef DEBUG
{ x_debug_info, "debug-info",   0 },
@@ -1467,6 +1469,7 @@ x_init_emacs(void)
kb_add(x_comp_file, CTRL('['), CTRL('X'), 0);
kb_add(x_comp_list, CTRL('I'), 0);
kb_add(x_comp_list, CTRL('['), '=', 0);
+   kb_add(x_shell_expand_line, CTRL('['), CTRL('E'), 0);
kb_add(x_del_back,  CTRL('?'), 0);
kb_add(x_del_back,  CTRL('H'), 0);
kb_add(x_del_char,  CTRL('['), '[', '3', '~', 0);
/* delete */
@@ -1700,6 +1703,48 @@ x_expand(int c)
return KSTD;
}
}
+   x_adjust();
+
+   return KSTD;
+}
+static int
+x_shell_expand_line(int c)
+{
+   int skip = 0, len = 0, size = 0;
+   char*cp, *word, *name, *buf;
+   struct tbl  *tp;
+
+   if (xep == xbuf) {
+   x_e_putc(BEL);
+   return KSTD;
+   }
+
+   cp = xbuf;
+   while (cp != xep && is_mfs(*cp))
+   skip += x_size(*cp++);
+
+   word = cp;
+   while (cp != xep && !is_mfs(*cp)) {
+   size += x_size(*cp++);
+   len++;
+   }
+
+   if (len > 0) {
+   name = strndup(word, len);
+   tp = ktsearch(, name, hash(name));
+   if (tp) {
+   x_goto(xbuf + skip);
+   x_delete(size, false);
+   x_ins(tp->val.s);
+   }
+   }
+
+   size = x_size_str(xbuf);
+   buf = substitute(xbuf, 0);
+
+   x_goto(xbuf);
+   x_delete(size, false);
+   x_ins(buf);
x_adjust();

return KSTD;
2018年10月7日(日) 4:41 John Ankarström :
>
> Hello again,
>
> Is there a way for ksh to expand a $(command substitution) without
> having to execute the entire line?
>
> bash provides this via shell-expand-line (bound to Ctrl-Alt-e by
> default), and ksh seems to have expand-file, but that only works for
> filenames.
>
> Any ideas?
>
> Best regards,
> John
>



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Tomasz Rola
On Sun, Oct 07, 2018 at 11:17:37PM +0200, John Ankarström wrote:
[...]
> 
> To which message is this a response?  It seems I haven't received
> it, but I'd like to read it.  Tomasz?

I have sent you a message offlist, see if you have it.

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread John Ankarström

Klemens Nanni wrote:

On Sun, Oct 07, 2018 at 07:30:15PM +0200, Tomasz Rola wrote:

Another trick may be executing the line with echo prepended - should
do all expansions and write what will be executed. I think it is not
going to work too well if for loop is being echoed, and other such
things, so perhaps quoting a command and echoing would do the job.

This will break any non-trivial construct including pipes, command lists,
loops, (nested) quoting, et al.



To which message is this a response?  It seems I haven't received it, 
but I'd like to read it.  Tomasz?


Best regards,
John



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Tomasz Rola
On Sun, Oct 07, 2018 at 07:47:46PM +0200, Klemens Nanni wrote:
> On Sun, Oct 07, 2018 at 07:30:15PM +0200, Tomasz Rola wrote:
> > Another trick may be executing the line with echo prepended - should
> > do all expansions and write what will be executed. I think it is not
> > going to work too well if for loop is being echoed, and other such
> > things, so perhaps quoting a command and echoing would do the job.
> This will break any non-trivial construct including pipes, command lists,
> loops, (nested) quoting, et al.

And let's not forget about redirections - any writing/appending inside
expansion will make echoing it even more non-trivial.

However, the same can be said about M-C-e in command prompt - how is
the shell going to know it should not expand this particular part,
because it calls a script which appends to / deletes from database?
And lets say it deletes not quite what we want? Because we are
prototyping on live command.

Which makes me say again, if this is such nontrivial, then I choose
writing a script.

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Klemens Nanni
On Sun, Oct 07, 2018 at 07:30:15PM +0200, Tomasz Rola wrote:
> Another trick may be executing the line with echo prepended - should
> do all expansions and write what will be executed. I think it is not
> going to work too well if for loop is being echoed, and other such
> things, so perhaps quoting a command and echoing would do the job.
This will break any non-trivial construct including pipes, command lists,
loops, (nested) quoting, et al.



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Tomasz Rola
On Sun, Oct 07, 2018 at 10:13:16AM +0200, Otto Moerbeek wrote:
> On Sun, Oct 07, 2018 at 08:48:52AM +0200, Tomasz Rola wrote:
> 
> > On Sun, Oct 07, 2018 at 12:03:31AM +0200, Klemens Nanni wrote:
> > > On Sat, Oct 06, 2018 at 09:38:42PM +0200, John Ankarström wrote:
> > [...]
> > > And yet, it disregards quoting and will errornously expand the following
> > > example into multiple words instead of one:
> > > 
> > >   bash-4.4$ echo "$(echo a b)"
> > >   bash-4.4$ echo a b
> > 
> > Just in case it matters to anybody:
> > 
> >   $ echo "$(echo a b)"
> > a b
> >   $   bash --version
> > GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
> > 
> > Looks like something changed in between?
> 
> This is not about executing the line, it's abouty expanding using Ctrl-Alt-e

I can see now (side note to myself: do not ever never again reply to
emails before going to sleep at morning, sorry).

So, can this problem be described as that OP cannot expand this stuff
in his memory (this jello ram between the ears), because it has got
too complicated? Perhaps this can be helped by writing things into a
proper script? I performed a lot of $() things in cli but never
learned about M-C-e, so I assume I never did things that required this
trick. Hence a script suggestion.

Another trick may be executing the line with echo prepended - should
do all expansions and write what will be executed. I think it is not
going to work too well if for loop is being echoed, and other such
things, so perhaps quoting a command and echoing would do the job.

Seems like it works in bash as I hoped:

==>$ echo "for i in $(seq 3); do circle ${i}; done"
for i in 1
2
3; do circle ; done

HTH
-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread John Ankarström

Klemens Nanni wrote:

On Sat, Oct 06, 2018 at 09:38:42PM +0200, John Ankarström wrote:

Is there a way for ksh to expand a $(command substitution) without having to
execute the entire line?

No.


That's too bad.


bash provides this via shell-expand-line (bound to Ctrl-Alt-e by default),

 From bash(1):

shell-expand-line (M-C-e)
Expand the line as the shell does.  This performs alias and
history expansion as well as all of the shell word expansions.

And yet, it disregards quoting and will errornously expand the following
example into multiple words instead of one:

bash-4.4$ echo "$(echo a b)"
bash-4.4$ echo a b


There is a discussion about this behavior on the bug-bash mailing list:

https://lists.gnu.org/archive/html/bug-bash/2016-11/msg00010.html

Turns out to be a relatively simple fix, but the old behavior will 
remain the default (as of 2016).



and ksh seems to have expand-file, but that only works for filenames.

We have no other expanding functions.


Would there be any interest among the pdksh developers in a function 
like shell-expand-line (but which works properly), if somebody like me 
did the work?


Best regards,
John



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Otto Moerbeek
On Sun, Oct 07, 2018 at 08:48:52AM +0200, Tomasz Rola wrote:

> On Sun, Oct 07, 2018 at 12:03:31AM +0200, Klemens Nanni wrote:
> > On Sat, Oct 06, 2018 at 09:38:42PM +0200, John Ankarström wrote:
> [...]
> > And yet, it disregards quoting and will errornously expand the following
> > example into multiple words instead of one:
> > 
> > bash-4.4$ echo "$(echo a b)"
> > bash-4.4$ echo a b
> 
> Just in case it matters to anybody:
> 
>   $ echo "$(echo a b)"
> a b
>   $   bash --version
> GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
> 
> Looks like something changed in between?

This is not about executing the line, it's abouty expanding using Ctrl-Alt-e

-Otto



Re: ksh equivalent to shell-expand-line

2018-10-07 Thread Tomasz Rola
On Sun, Oct 07, 2018 at 12:03:31AM +0200, Klemens Nanni wrote:
> On Sat, Oct 06, 2018 at 09:38:42PM +0200, John Ankarström wrote:
[...]
> And yet, it disregards quoting and will errornously expand the following
> example into multiple words instead of one:
> 
>   bash-4.4$ echo "$(echo a b)"
>   bash-4.4$ echo a b

Just in case it matters to anybody:

  $ echo "$(echo a b)"
a b
  $   bash --version
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

Looks like something changed in between?

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **



Re: ksh equivalent to shell-expand-line

2018-10-06 Thread Klemens Nanni
On Sat, Oct 06, 2018 at 09:38:42PM +0200, John Ankarström wrote:
> Is there a way for ksh to expand a $(command substitution) without having to
> execute the entire line?
No.

> bash provides this via shell-expand-line (bound to Ctrl-Alt-e by default),
>From bash(1):

shell-expand-line (M-C-e)
Expand the line as the shell does.  This performs alias and
history expansion as well as all of the shell word expansions.

And yet, it disregards quoting and will errornously expand the following
example into multiple words instead of one:

bash-4.4$ echo "$(echo a b)"
bash-4.4$ echo a b

> and ksh seems to have expand-file, but that only works for filenames.
We have no other expanding functions.



ksh equivalent to shell-expand-line

2018-10-06 Thread John Ankarström

Hello again,

Is there a way for ksh to expand a $(command substitution) without 
having to execute the entire line?


bash provides this via shell-expand-line (bound to Ctrl-Alt-e by 
default), and ksh seems to have expand-file, but that only works for 
filenames.


Any ideas?

Best regards,
John