unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH 0/3] emacs: Improve post processing of popup arguments.
@ 2015-11-05  8:39 Alex Kost
  2015-11-05  8:39 ` [PATCH 1/3] " Alex Kost
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Alex Kost @ 2015-11-05  8:39 UTC (permalink / raw)
  To: guix-devel

Hello, this patchset is for adjusting "M-x guix" for the recent
environment/container changes.  'magit-popup' returns a simple list of
arguments, and this list needs to be "post-processed".  Previously a
very simple post-processing (moving the rest "-- ..." arguments to the
end of the arg list) was enough, but now (for a rather complicated 'guix
environment' command: positional "--ad-hoc", shell command after '--')
it should be improved.  So this is the improvement.

Patches:

[PATCH 1/3] emacs: Improve post processing of popup arguments.
[PATCH 2/3] emacs: Adjust 'guix container' popup.
[PATCH 3/3] emacs: Adjust 'guix environment' popup.

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

* [PATCH 1/3] emacs: Improve post processing of popup arguments.
  2015-11-05  8:39 [PATCH 0/3] emacs: Improve post processing of popup arguments Alex Kost
@ 2015-11-05  8:39 ` Alex Kost
  2015-11-05  8:39 ` [PATCH 2/3] emacs: Adjust 'guix container' popup Alex Kost
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Alex Kost @ 2015-11-05  8:39 UTC (permalink / raw)
  To: guix-devel

* emacs/guix-command.el (guix-command-switches,
  guix-command-rest-arg-regexp): New variables.
  (guix-command-post-processors,
  guix-command-post-process-matching-args,
  guix-command-post-process-rest-single,
  guix-command-post-process-rest-multiple,
  guix-command-post-process-rest-multiple-leave,
  guix-command-post-process-package-args): New functions.
  (guix-command-post-process-rest-multiple): Take 2 arguments.
  (guix-command-define-popup-action): Adjust accordingly.
* emacs/guix-utils.el (guix-modify): New function.
---
 emacs/guix-command.el | 99 ++++++++++++++++++++++++++++++++++++++++++---------
 emacs/guix-utils.el   | 11 ++++++
 2 files changed, 94 insertions(+), 16 deletions(-)

diff --git a/emacs/guix-command.el b/emacs/guix-command.el
index f8a6df7..ea29461 100644
--- a/emacs/guix-command.el
+++ b/emacs/guix-command.el
@@ -465,28 +465,94 @@ to be modified."
   "Return actions from ARGUMENTS."
   (cl-remove-if-not #'guix-command-argument-action? arguments))
 
-(defun guix-command-post-process-args (args)
-  "Adjust appropriately command line ARGS returned from popup command."
-  ;; XXX We need to split "--install foo bar" and similar strings into
-  ;; lists of strings.  But some commands (e.g., 'guix hash') accept a
-  ;; file name as the 'rest' argument, and as file names may contain
-  ;; spaces, splitting by spaces will break such names.  For example, the
-  ;; following argument: "-- /tmp/file with spaces" will be transformed
-  ;; into the following list: ("--" "/tmp/file" "with" "spaces") instead
-  ;; of the wished ("--" "/tmp/file with spaces").
-  (let* (rest
-         (rx (rx string-start
-                 (or "-- " "--install " "--remove ")))
+\f
+;;; Post processing popup arguments
+
+(defvar guix-command-post-processors
+  '(("hash"
+     guix-command-post-process-rest-single)
+    ("package"
+     guix-command-post-process-package-args)
+    ("system"
+     guix-command-post-process-rest-single))
+  "Alist of guix commands and functions for post-processing
+a list of arguments returned from popup interface.
+Each function is called on the returned arguments in turn.")
+
+(defvar guix-command-rest-arg-regexp
+  (rx string-start "-- " (group (+ any)))
+  "Regexp to match a string with the 'rest' arguments.")
+
+(defun guix-command-replace-args (args predicate modifier)
+  "Replace arguments matching PREDICATE from ARGS.
+Call MODIFIER on each argument matching PREDICATE and append the
+returned list of strings to the end of ARGS.  Remove the original
+arguments."
+  (let* ((rest nil)
          (args (mapcar (lambda (arg)
-                         (if (string-match-p rx arg)
-                             (progn (push (split-string arg) rest)
-                                    nil)
+                         (if (funcall predicate arg)
+                             (progn
+                               (push (funcall modifier arg) rest)
+                               nil)
                            arg))
                        args)))
     (if rest
         (apply #'append (delq nil args) rest)
       args)))
 
+(cl-defun guix-command-post-process-matching-args (args regexp
+                                                   &key group split?)
+  "Modify arguments from ARGS matching REGEXP by moving them to
+the end of ARGS list.  If SPLIT? is non-nil, split matching
+arguments into multiple subarguments."
+  (guix-command-replace-args
+   args
+   (lambda (arg)
+     (string-match regexp arg))
+   (lambda (arg)
+     (let ((val (match-string (or group 0) arg))
+           (fun (if split? #'split-string #'list)))
+       (funcall fun val)))))
+
+(defun guix-command-post-process-rest-single (args)
+  "Modify ARGS by moving '-- ARG' argument to the end of ARGS list."
+  (guix-command-post-process-matching-args
+   args guix-command-rest-arg-regexp
+   :group 1))
+
+(defun guix-command-post-process-rest-multiple (args)
+  "Modify ARGS by splitting '-- ARG ...' into multiple subarguments
+and moving them to the end of ARGS list.
+Remove '-- ' string."
+  (guix-command-post-process-matching-args
+   args guix-command-rest-arg-regexp
+   :group 1
+   :split? t))
+
+(defun guix-command-post-process-rest-multiple-leave (args)
+  "Modify ARGS by splitting '-- ARG ...' into multiple subarguments
+and moving them to the end of ARGS list.
+Leave '--' string as a separate argument."
+  (guix-command-post-process-matching-args
+   args guix-command-rest-arg-regexp
+   :split? t))
+
+(defun guix-command-post-process-package-args (args)
+  "Adjust popup ARGS for 'guix package' command."
+  (guix-command-post-process-matching-args
+   args (rx string-start (or "--install " "--remove ") (+ any))
+   :split? t))
+
+(defun guix-command-post-process-args (commands args)
+  "Adjust popup ARGS for guix COMMANDS."
+  (let* ((command (car commands))
+         (processors
+          (append (guix-assoc-value guix-command-post-processors commands)
+                  (guix-assoc-value guix-command-post-processors command))))
+    (guix-modify args
+                 (or processors
+                     (list #'guix-command-post-process-rest-multiple)))))
+
 \f
 ;;; 'Execute' actions
 
@@ -642,7 +708,8 @@ EXECUTOR function is called with the current command line arguments."
        ,doc
        (interactive (,arguments-fun))
        (,executor (append ',commands
-                          (guix-command-post-process-args args))))))
+                          (guix-command-post-process-args
+                           ',commands args))))))
 
 (defun guix-command-generate-popup-actions (actions &optional commands)
   "Generate 'popup' commands from ACTIONS arguments for guix COMMANDS."
diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el
index d1f088b..5f3f3ec 100644
--- a/emacs/guix-utils.el
+++ b/emacs/guix-utils.el
@@ -226,6 +226,17 @@ single argument."
      (while (re-search-forward ,regexp nil t)
        ,@body)))
 
+(defun guix-modify (object modifiers)
+  "Apply MODIFIERS to OBJECT.
+OBJECT is passed as an argument to the first function from
+MODIFIERS list, the returned result is passed to the second
+function from the list and so on.  Return result of the last
+modifier call."
+  (if (null modifiers)
+      object
+    (guix-modify (funcall (car modifiers) object)
+                 (cdr modifiers))))
+
 \f
 ;;; Alist accessors
 
-- 
2.5.0

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

* [PATCH 2/3] emacs: Adjust 'guix container' popup.
  2015-11-05  8:39 [PATCH 0/3] emacs: Improve post processing of popup arguments Alex Kost
  2015-11-05  8:39 ` [PATCH 1/3] " Alex Kost
@ 2015-11-05  8:39 ` Alex Kost
  2015-11-05  8:39 ` [PATCH 3/3] emacs: Adjust 'guix environment' popup Alex Kost
  2015-11-08 21:16 ` [PATCH 0/3] emacs: Improve post processing of popup arguments Ludovic Courtès
  3 siblings, 0 replies; 6+ messages in thread
From: Alex Kost @ 2015-11-05  8:39 UTC (permalink / raw)
  To: guix-devel

* emacs/guix-command.el (guix-command-improve-action-argument): Use "C"
  key for 'container' action to distinguish it from 'challenge'.
  (guix-command-rest-argument): Add positional arguments.
---
 emacs/guix-command.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/emacs/guix-command.el b/emacs/guix-command.el
index ea29461..5e5cff6 100644
--- a/emacs/guix-command.el
+++ b/emacs/guix-command.el
@@ -131,7 +131,8 @@ to be modified."
 
 (guix-command-define-argument-improver
     guix-command-improve-action-argument
-  '(("graph"       :char ?G)
+  '(("container"   :char ?C)
+    ("graph"       :char ?G)
     ("environment" :char ?E)
     ("publish"     :char ?u)
     ("pull"        :char ?P)
@@ -369,6 +370,8 @@ to be modified."
                 '("archive" "build" "challenge" "edit" "environment"
                   "graph" "lint" "refresh"))
         (argument :doc "Packages" :fun 'guix-read-package-names-string))
+       ((equal commands '("container" "exec"))
+        (argument :doc "PID Command [Args...]"))
        ((string= command "download")
         (argument :doc "URL"))
        ((string= command "gc")
-- 
2.5.0

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

* [PATCH 3/3] emacs: Adjust 'guix environment' popup.
  2015-11-05  8:39 [PATCH 0/3] emacs: Improve post processing of popup arguments Alex Kost
  2015-11-05  8:39 ` [PATCH 1/3] " Alex Kost
  2015-11-05  8:39 ` [PATCH 2/3] emacs: Adjust 'guix container' popup Alex Kost
@ 2015-11-05  8:39 ` Alex Kost
  2015-11-08 21:16 ` [PATCH 0/3] emacs: Improve post processing of popup arguments Ludovic Courtès
  3 siblings, 0 replies; 6+ messages in thread
From: Alex Kost @ 2015-11-05  8:39 UTC (permalink / raw)
  To: guix-devel

* emacs/guix-command.el (guix-command-improve-environment-argument): Use
  "E" for '--expose' and "S" for '--share' to distinguish them from
  '--expression' and '--system' accordingly.  Make '--ad-hoc' an option
  taking packages instead of a simple switch.
  (guix-command-rest-argument): Complete shell command instead of
  packages for '--' argument.
  (guix-command-additional-arguments): Add fake 'packages' argument.
  New variable.
  (guix-command-additional-arguments): Use it.
  (guix-command-post-process-environment-packages,
  guix-command-post-process-environment-ad-hoc): New functions.
  (guix-command-post-processors): Add "environment" command with the new
  argument processors.
---
 emacs/guix-command.el | 45 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/emacs/guix-command.el b/emacs/guix-command.el
index 5e5cff6..43fdfc3 100644
--- a/emacs/guix-command.el
+++ b/emacs/guix-command.el
@@ -196,7 +196,11 @@ to be modified."
 
 (guix-command-define-argument-improver
     guix-command-improve-environment-argument
-  '(("--exec" :fun read-shell-command)
+  '(("--ad-hoc"
+     :name "--ad-hoc " :fun guix-read-package-names-string
+     :switch? nil :option? t)
+    ("--expose" :char ?E)
+    ("--share" :char ?S)
     ("--load" :fun guix-read-file-name)))
 
 (guix-command-define-argument-improver
@@ -367,13 +371,15 @@ to be modified."
     (let ((command (car commands)))
       (cond
        ((member command
-                '("archive" "build" "challenge" "edit" "environment"
+                '("archive" "build" "challenge" "edit"
                   "graph" "lint" "refresh"))
         (argument :doc "Packages" :fun 'guix-read-package-names-string))
        ((equal commands '("container" "exec"))
         (argument :doc "PID Command [Args...]"))
        ((string= command "download")
         (argument :doc "URL"))
+       ((string= command "environment")
+        (argument :doc "Command [Args...]" :fun 'read-shell-command))
        ((string= command "gc")
         (argument :doc "Paths" :fun 'guix-read-file-name))
        ((member command '("hash" "system"))
@@ -387,10 +393,22 @@ to be modified."
              (string= command "import"))
         (argument :doc "Package name"))))))
 
+(defvar guix-command-additional-arguments
+  `((("environment")
+     ,(guix-command-make-argument
+       :name "packages " :char ?p :option? t
+       :doc "build inputs of the specified packages"
+       :fun 'guix-read-package-names-string)))
+  "Alist of guix commands and additional arguments for them.
+These are 'fake' arguments that are not presented in 'guix' shell
+commands.")
+
 (defun guix-command-additional-arguments (&optional commands)
   "Return additional arguments for COMMANDS."
   (let ((rest-arg (guix-command-rest-argument commands)))
-    (and rest-arg (list rest-arg))))
+    (append (guix-assoc-value guix-command-additional-arguments
+                              commands)
+            (and rest-arg (list rest-arg)))))
 
 ;; Ideally only `guix-command-arguments' function should exist with the
 ;; contents of `guix-command-all-arguments', but we need to make a
@@ -472,7 +490,11 @@ to be modified."
 ;;; Post processing popup arguments
 
 (defvar guix-command-post-processors
-  '(("hash"
+  '(("environment"
+     guix-command-post-process-environment-packages
+     guix-command-post-process-environment-ad-hoc
+     guix-command-post-process-rest-multiple-leave)
+    ("hash"
      guix-command-post-process-rest-single)
     ("package"
      guix-command-post-process-package-args)
@@ -546,6 +568,21 @@ Leave '--' string as a separate argument."
    args (rx string-start (or "--install " "--remove ") (+ any))
    :split? t))
 
+(defun guix-command-post-process-environment-packages (args)
+  "Adjust popup ARGS for specified packages of 'guix environment'
+command."
+  (guix-command-post-process-matching-args
+   args (rx string-start "packages " (group (+ any)))
+   :group 1
+   :split? t))
+
+(defun guix-command-post-process-environment-ad-hoc (args)
+  "Adjust popup ARGS for '--ad-hoc' argument of 'guix environment'
+command."
+  (guix-command-post-process-matching-args
+   args (rx string-start "--ad-hoc " (+ any))
+   :split? t))
+
 (defun guix-command-post-process-args (commands args)
   "Adjust popup ARGS for guix COMMANDS."
   (let* ((command (car commands))
-- 
2.5.0

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

* Re: [PATCH 0/3] emacs: Improve post processing of popup arguments.
  2015-11-05  8:39 [PATCH 0/3] emacs: Improve post processing of popup arguments Alex Kost
                   ` (2 preceding siblings ...)
  2015-11-05  8:39 ` [PATCH 3/3] emacs: Adjust 'guix environment' popup Alex Kost
@ 2015-11-08 21:16 ` Ludovic Courtès
  2015-11-12 18:24   ` Alex Kost
  3 siblings, 1 reply; 6+ messages in thread
From: Ludovic Courtès @ 2015-11-08 21:16 UTC (permalink / raw)
  To: Alex Kost; +Cc: guix-devel

Alex Kost <alezost@gmail.com> skribis:

> Hello, this patchset is for adjusting "M-x guix" for the recent
> environment/container changes.  'magit-popup' returns a simple list of
> arguments, and this list needs to be "post-processed".  Previously a
> very simple post-processing (moving the rest "-- ..." arguments to the
> end of the arg list) was enough, but now (for a rather complicated 'guix
> environment' command: positional "--ad-hoc", shell command after '--')
> it should be improved.  So this is the improvement.

Good catch.

> [PATCH 1/3] emacs: Improve post processing of popup arguments.
> [PATCH 2/3] emacs: Adjust 'guix container' popup.
> [PATCH 3/3] emacs: Adjust 'guix environment' popup.

On a cursory look it LGTM.  I don’t grasp the details of #1 but I
trust you.

Thank you!

Ludo’.

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

* Re: [PATCH 0/3] emacs: Improve post processing of popup arguments.
  2015-11-08 21:16 ` [PATCH 0/3] emacs: Improve post processing of popup arguments Ludovic Courtès
@ 2015-11-12 18:24   ` Alex Kost
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Kost @ 2015-11-12 18:24 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guix-devel

Ludovic Courtès (2015-11-09 00:16 +0300) wrote:

> Alex Kost <alezost@gmail.com> skribis:
>
>> Hello, this patchset is for adjusting "M-x guix" for the recent
>> environment/container changes.  'magit-popup' returns a simple list of
>> arguments, and this list needs to be "post-processed".  Previously a
>> very simple post-processing (moving the rest "-- ..." arguments to the
>> end of the arg list) was enough, but now (for a rather complicated 'guix
>> environment' command: positional "--ad-hoc", shell command after '--')
>> it should be improved.  So this is the improvement.
>
> Good catch.
>
>> [PATCH 1/3] emacs: Improve post processing of popup arguments.
>> [PATCH 2/3] emacs: Adjust 'guix container' popup.
>> [PATCH 3/3] emacs: Adjust 'guix environment' popup.
>
> On a cursory look it LGTM.  I don’t grasp the details of #1 but I
> trust you.

OK, thanks, I forgot the details of #1 already :-)  The point was to
make a general final processing of arguments (returned by magit-popup).

> Thank you!

And thank you!  Pushed.

-- 
Alex

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

end of thread, other threads:[~2015-11-12 18:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-05  8:39 [PATCH 0/3] emacs: Improve post processing of popup arguments Alex Kost
2015-11-05  8:39 ` [PATCH 1/3] " Alex Kost
2015-11-05  8:39 ` [PATCH 2/3] emacs: Adjust 'guix container' popup Alex Kost
2015-11-05  8:39 ` [PATCH 3/3] emacs: Adjust 'guix environment' popup Alex Kost
2015-11-08 21:16 ` [PATCH 0/3] emacs: Improve post processing of popup arguments Ludovic Courtès
2015-11-12 18:24   ` Alex Kost

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

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).