unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* shell-CLI for Emacs
@ 2013-08-23 20:46 Emanuel Berg
  0 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2013-08-23 20:46 UTC (permalink / raw)
  To: help-gnu-emacs

I have done some experimentation on the "Emacs-shell-CLI" I've
been thinking about (as opposed to the current "M-x-CLI").

I would be very happy if you offered your comments on this idea
(and code).

To try out the below code:

1. Hit M-i to bring up the prompt string.
2. Type "man emacs".
3. Hit RET.

(defun string-to-cmd (str)
  (interactive "s $> ")
  (let*((cmd-and-args (split-string str " "))
        (cmd  (car cmd-and-args))
        (args (cdr cmd-and-args)))
    (eval `(,(read cmd) ,@args) )))
(define-key (current-global-map) (kbd "M-i") 'string-to-cmd)

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
@ 2013-08-23 22:02 Barry OReilly
  0 siblings, 0 replies; 8+ messages in thread
From: Barry OReilly @ 2013-08-23 22:02 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun string-to-cmd (str)
>   (interactive "s $> ")
>   (let*((cmd-and-args (split-string str " "))
>         (cmd  (car cmd-and-args))
>         (args (cdr cmd-and-args)))
>     (eval `(,(read cmd) ,@args) )))

Clearer than (eval `(,(read cmd) ,@args) ) is (apply (read cmd) args)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
       [not found] <mailman.664.1377295372.10748.help-gnu-emacs@gnu.org>
@ 2013-08-23 22:42 ` Emanuel Berg
  2013-08-24  0:37   ` Thien-Thi Nguyen
       [not found]   ` <mailman.670.1377304488.10748.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Emanuel Berg @ 2013-08-23 22:42 UTC (permalink / raw)
  To: help-gnu-emacs

Barry OReilly <gundaetiapo@gmail.com> writes:

> Clearer than (eval `(,(read cmd) ,@args) ) is (apply (read cmd)
> args)

Absolutely! I didn't know of apply.

I realized a major problem with the interface is that a lot of
commands act differently depending on if there is a region or
not. An example is replace-string. With the function in the OP,
the region is ignored! Very confusing, as intuitively, once set,
you expect it to delimit the search-and-replace.

I worked around that with an, eh, interface to replace-string.

But that's getting tedious if it has to be done for a lot of
functions...

(defun rs (from to)
  (interactive)
  (let*((scope (if mark-active
                   (list (region-beginning) (region-end))
                   (list (point-min) (point-max)) ))
        (start (car   scope))
        (end   (nth 1 scope)) )
  (save-excursion
    (replace-string from to t start end) )))

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
  2013-08-23 22:42 ` shell-CLI for Emacs Emanuel Berg
@ 2013-08-24  0:37   ` Thien-Thi Nguyen
       [not found]   ` <mailman.670.1377304488.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Thien-Thi Nguyen @ 2013-08-24  0:37 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1249 bytes --]

() Emanuel Berg <embe8573@student.uu.se>
() Sat, 24 Aug 2013 00:42:50 +0200

   But that's getting tedious if it has to be done for a lot of
   functions...

Maybe you can play w/ ‘call-interactively’ and ‘unread-command-events’.
Split the line as before but instead of ‘apply’ing the command to the
args, push the args (character by character, in reverse order) onto
‘unread-command-events’, preceding each arg (set) with ‘C-j’ (newline).
Here's a test command:

 (defun zow (string symbol number)
   (interactive "sString: \nSSymbol: \nnNumber: ")
   (message "got: %S" (list string symbol number)))

Invoked like:

 zow not quite 42

it should display:

 got: ("not" quite 42)

in the echo area.  Of course, this technique loses w/ certain (stealthy)
‘interactive’ specs, regions, stateful (history var) defaults, args that
include a space, etc, but it might be fun to try, anyway.  That's the
primary benefit of all DWIM hacking, after all, for both you and Emacs!

-- 
Thien-Thi Nguyen
   GPG key: 4C807502
   (if you're human and you know it)
      read my lisp: (responsep (questions 'technical)
                               (not (via 'mailing-list)))
                     => nil

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
       [not found]   ` <mailman.670.1377304488.10748.help-gnu-emacs@gnu.org>
@ 2013-08-24  3:30     ` Emanuel Berg
  2013-08-26  0:56     ` Emanuel Berg
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2013-08-24  3:30 UTC (permalink / raw)
  To: help-gnu-emacs

Thien-Thi Nguyen <ttn@gnu.org> writes:

> Maybe you can play w/ ‘call-interactively’ and
> ‘unread-command-events’.  Split the line as before but instead
> of ‘apply’ing the command to the args, push the args (character
> by character, in reverse order) onto ‘unread-command-events’,
> preceding each arg (set) with ‘C-j’ (newline).

Yes... that was a good idea. Impressive to say the least.

(require 'cl)
(defvar *ps* " $> ")

(defun string-to-cmd (str)
  (interactive (list (read-string *ps*)))
  (let*((cmd-and-args (split-string str " "))
        (cmd  (read (car cmd-and-args)))
        (args (cdr cmd-and-args)))
    (dolist (arg (nreverse args))
      (push 10 unread-command-events)
      (dolist (n (reverse (string-to-list arg)))
        (push n unread-command-events) ))
    (call-interactively cmd) ))
(define-key (current-global-map) (kbd "M-i") 'string-to-cmd)

Works for your zow test function, 'man emacs', and you don't have
to rewrite replace-string.

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
       [not found]   ` <mailman.670.1377304488.10748.help-gnu-emacs@gnu.org>
  2013-08-24  3:30     ` Emanuel Berg
@ 2013-08-26  0:56     ` Emanuel Berg
  2013-08-26  4:03       ` Yuri Khan
  2013-08-26  5:38       ` Thien-Thi Nguyen
  1 sibling, 2 replies; 8+ messages in thread
From: Emanuel Berg @ 2013-08-26  0:56 UTC (permalink / raw)
  To: help-gnu-emacs

Thien-Thi Nguyen <ttn@gnu.org> writes:

> Of course, this technique loses w/ certain (stealthy)
> ‘interactive’ specs ‘interactive’ specs, regions ...

replace-string works with and without a region, when I did exactly
as you told me. But perhaps there is more to regions?

> stateful (history var) defaults

What's that? :)

> args that include a space

I got that solved. Now,

replace-string 'Hello all!' 'Dear all,'

works.

The code [1] got somewhat complicated...

> That's the primary benefit of all DWIM hacking, after all, for
> both you and Emacs!

DWIM, is that what I'm doing? Why did you say that? I'm to read
the Wikipedia article tonight - never heard of it, but intuitively
I like it.

Some people take pride in memorizing and understanding complex
command, drawing oh's and ah's from the crowd: what wizardry! I
think that's just silly. In a nutshell, this is what my system
looks like:

add_dic () { sudo tar -xvjf $1 -C /usr/share/stardict/dic; }

That's why Captain Picard say "Engage!", and not "Set the
thrusters to blaha blaha, align the ship to etc. etc., then..."

(Perhaps that's unrelated to DWIM.)

[1]

(require 'cl)
(defvar *ps* " $> ")

(defun string-to-cmd (str)
  (interactive (list (read-string *ps*)))
  (let*((cmd  (read (car (split-string str " "))))
        (args (cdr (make-arg-list (string-to-list str)))) )
    (dolist (arg (nreverse args))
      (push 13 unread-command-events) ; 13 is RET
      (dolist (n (reverse (string-to-list arg)))
        (push n unread-command-events) ))
    (call-interactively cmd) ))
(define-key (current-global-map) (kbd "M-i") 'string-to-cmd)

(defun make-arg-list (chars)
  (interactive)
  (if chars
      (let ((WS  39) ; whitespace ( )
            (SQM 32) ;      quote (')
            (c  (car chars))
            (cs (cdr chars)) )
        (if (eq c WS) (make-word cs '() WS)
          (if (eq c SQM) (make-arg-list cs)
            (make-word chars '() SQM)) ))
  '() ))

(defun make-word (chars wd sep)
  (interactive)
  (if chars
      (let ((c  (car chars))
            (cs (cdr chars)))
        (if (eq c sep) (cons wd (make-arg-list cs))
          (make-word cs (append wd (list c)) sep)))
    (list wd) ))

-- 
Emanuel Berg - programmer (hire me! CV below)
computer projects: http://user.it.uu.se/~embe8573
internet activity: http://home.student.uu.se/embe8573


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
  2013-08-26  0:56     ` Emanuel Berg
@ 2013-08-26  4:03       ` Yuri Khan
  2013-08-26  5:38       ` Thien-Thi Nguyen
  1 sibling, 0 replies; 8+ messages in thread
From: Yuri Khan @ 2013-08-26  4:03 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs@gnu.org

On Mon, Aug 26, 2013 at 7:56 AM, Emanuel Berg <embe8573@student.uu.se> wrote:

>> That's the primary benefit of all DWIM hacking, after all, for
>> both you and Emacs!
>
> DWIM, is that what I'm doing? Why did you say that? I'm to read
> the Wikipedia article tonight - never heard of it, but intuitively
> I like it.
>
> add_dic () { sudo tar -xvjf $1 -C /usr/share/stardict/dic; }

What you are doing here is just programming: replacing a long precise
command with a shorter but still precise command.

DWIM is a technique of replacing a precise command with one that is in
some way imprecise. You misspell a command, something behind the
scenes corrects it for you (seen in zsh). You don’t specify a
parameter, it picks a default that is sensible for some people in some
cases, but not universally (e.g. git push).

You should be reading not Wikipedia but The Jargon File:
http://www.catb.org/~esr/jargon/html/D/DWIM.html . It explains well
why one man’s Do What I Mean is another’s Damn Warren’s Infernal
Machine.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: shell-CLI for Emacs
  2013-08-26  0:56     ` Emanuel Berg
  2013-08-26  4:03       ` Yuri Khan
@ 2013-08-26  5:38       ` Thien-Thi Nguyen
  1 sibling, 0 replies; 8+ messages in thread
From: Thien-Thi Nguyen @ 2013-08-26  5:38 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1207 bytes --]

() Emanuel Berg <embe8573@student.uu.se>
() Mon, 26 Aug 2013 02:56:35 +0200

   But perhaps there is more to regions?

Perhaps.  Emacs is very dynamic and although recently it
has taken a large step away from that nature (lexbind),
i suspect there remains a good bit of irreducible magic.

This is not a very precise answer, but that's all i can say.

   > stateful (history var) defaults

   What's that? :)

(info "(emacs) Minibuffer History")
(info "(emacs) Repetition")

   > That's the primary benefit of all DWIM hacking, after all,
   > for both you and Emacs!

   DWIM, is that what I'm doing?

When you apply heuristics to input, you are doing DWIM hacking.
Initially, you think "this is algorithm, no mere heuristic", but
then a bit later, "The code got somewhat complicated..."  :-D

   Why did you say that?

It tickles me to imagine Emacs having fun (sometimes).  A very
patient friend, slinging bytes across the gap, i appreciate it.

-- 
Thien-Thi Nguyen
   GPG key: 4C807502
   (if you're human and you know it)
      read my lisp: (responsep (questions 'technical)
                               (not (via 'mailing-list)))
                     => nil

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-08-26  5:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.664.1377295372.10748.help-gnu-emacs@gnu.org>
2013-08-23 22:42 ` shell-CLI for Emacs Emanuel Berg
2013-08-24  0:37   ` Thien-Thi Nguyen
     [not found]   ` <mailman.670.1377304488.10748.help-gnu-emacs@gnu.org>
2013-08-24  3:30     ` Emanuel Berg
2013-08-26  0:56     ` Emanuel Berg
2013-08-26  4:03       ` Yuri Khan
2013-08-26  5:38       ` Thien-Thi Nguyen
2013-08-23 22:02 Barry OReilly
  -- strict thread matches above, loose matches on Subject: below --
2013-08-23 20:46 Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).