diff options
author | Mark H Weaver <mhw@netris.org> | 2015-06-10 17:50:27 -0400 |
---|---|---|
committer | Mark H Weaver <mhw@netris.org> | 2015-06-10 17:50:27 -0400 |
commit | 14928016556300a6763334d4279c3d117902caaf (patch) | |
tree | d0dc262b14164b82f97dd6e896ca9e93a1fabeea /emacs | |
parent | 1511e0235525358abb52cf62abeb9457605b5093 (diff) | |
parent | 57cd353d87d6e9e6e882327be70b4d7b5ce863ba (diff) | |
download | guix-14928016556300a6763334d4279c3d117902caaf.tar.gz |
Merge branch 'master' into core-updates
Diffstat (limited to 'emacs')
-rw-r--r-- | emacs/guix-base.el | 27 | ||||
-rw-r--r-- | emacs/guix-list.el | 42 | ||||
-rw-r--r-- | emacs/guix-main.scm | 3 | ||||
-rw-r--r-- | emacs/guix-pcomplete.el | 392 | ||||
-rw-r--r-- | emacs/guix-utils.el | 33 |
5 files changed, 476 insertions, 21 deletions
diff --git a/emacs/guix-base.el b/emacs/guix-base.el index 5129c87a5d..851ee895b9 100644 --- a/emacs/guix-base.el +++ b/emacs/guix-base.el @@ -439,6 +439,7 @@ following keywords are available: (define-key map (kbd "r") 'guix-history-forward) (define-key map (kbd "g") 'revert-buffer) (define-key map (kbd "R") 'guix-redisplay-buffer) + (define-key map (kbd "M") 'guix-apply-manifest) (define-key map (kbd "C-c C-z") 'guix-switch-to-repl))))) (put 'guix-define-buffer-type 'lisp-indent-function 'defun) @@ -1022,6 +1023,32 @@ Ask a user with PROMPT for continuing an operation." :dry-run? (or guix-dry-run 'f)) nil 'source-download))) +;;;###autoload +(defun guix-apply-manifest (profile file &optional operation-buffer) + "Apply manifest from FILE to PROFILE. +This function has the same meaning as 'guix package --manifest' command. +See Info node `(guix) Invoking guix package' for details. + +Interactively, use the current profile and prompt for manifest +FILE. With a prefix argument, also prompt for PROFILE." + (interactive + (let* ((default-profile (or guix-profile guix-current-profile)) + (profile (if current-prefix-arg + (guix-profile-prompt) + default-profile)) + (file (read-file-name "File with manifest: ")) + (buffer (and guix-profile (current-buffer)))) + (list profile file buffer))) + (when (or (not guix-operation-confirm) + (y-or-n-p (format "Apply manifest from '%s' to profile '%s'? " + file profile))) + (guix-eval-in-repl + (guix-make-guile-expression + 'guix-package + (concat "--profile=" profile) + (concat "--manifest=" file)) + operation-buffer))) + ;;; Pull diff --git a/emacs/guix-list.el b/emacs/guix-list.el index 600f2bd9bd..279de818c6 100644 --- a/emacs/guix-list.el +++ b/emacs/guix-list.el @@ -292,13 +292,11 @@ See `guix-list-marked' for the meaning of ARGS." See `guix-list-get-marked' for details." (mapcar #'car (apply #'guix-list-get-marked mark-names))) -(defun guix-list-mark (mark-name &optional advance &rest args) +(defun guix-list--mark (mark-name &optional advance &rest args) "Put a mark on the current line. Also add the current entry to `guix-list-marked' using its ID and ARGS. MARK-NAME is a symbol from `guix-list-mark-alist'. -If ADVANCE is non-nil, move forward by one line after marking. -Interactively, put a general mark and move to the next line." - (interactive '(general t)) +If ADVANCE is non-nil, move forward by one line after marking." (let ((id (guix-list-current-id))) (if (eq mark-name 'empty) (setq guix-list-marked (assq-delete-all id guix-list-marked)) @@ -310,12 +308,21 @@ Interactively, put a general mark and move to the next line." (tabulated-list-put-tag (guix-list-get-mark-string mark-name) advance)) -(defun guix-list-mark-all (mark-name) +(defun guix-list-mark (&optional arg) + "Mark the current line and move to the next line. +With ARG, mark all lines." + (interactive "P") + (if arg + (guix-list-mark-all) + (guix-list--mark 'general t))) + +(defun guix-list-mark-all (&optional mark-name) "Mark all lines with MARK-NAME mark. MARK-NAME is a symbol from `guix-list-mark-alist'. Interactively, put a general mark on all lines." - (interactive '(general)) - (guix-list-for-each-line #'guix-list-mark mark-name)) + (interactive) + (or mark-name (setq mark-name 'general)) + (guix-list-for-each-line #'guix-list--mark mark-name)) (defun guix-list-unmark (&optional arg) "Unmark the current line and move to the next line. @@ -323,13 +330,13 @@ With ARG, unmark all lines." (interactive "P") (if arg (guix-list-unmark-all) - (guix-list-mark 'empty t))) + (guix-list--mark 'empty t))) (defun guix-list-unmark-backward () "Move up one line and unmark it." (interactive) (forward-line -1) - (guix-list-mark 'empty)) + (guix-list--mark 'empty)) (defun guix-list-unmark-all () "Unmark all lines." @@ -360,7 +367,6 @@ Same as `tabulated-list-sort', but also restore marks after sorting." (define-key map (kbd "RET") 'guix-list-describe) (define-key map (kbd "m") 'guix-list-mark) (define-key map (kbd "*") 'guix-list-mark) - (define-key map (kbd "M") 'guix-list-mark-all) (define-key map (kbd "u") 'guix-list-unmark) (define-key map (kbd "DEL") 'guix-list-unmark-backward) (define-key map [remap tabulated-list-sort] 'guix-list-sort) @@ -417,7 +423,7 @@ This macro defines the following functions: ,(concat "Put '" mark-name-str "' mark and move to the next line.\n" "Also add the current entry to `guix-list-marked'.") (interactive) - (guix-list-mark ',mark-name t)))) + (guix-list--mark ',mark-name t)))) marks-val) (defun ,init-fun () @@ -531,7 +537,7 @@ AVAILABLE list, otherwise mark all DEFAULT outputs." (guix-completing-read-multiple prompt available nil t) default))) - (apply #'guix-list-mark mark t outputs))) + (apply #'guix-list--mark mark t outputs))) (defun guix-package-list-mark-install (&optional arg) "Mark the current package for installation and move to the next line. @@ -606,7 +612,7 @@ accept an entry as argument." (interactive) (guix-list-mark-package-upgrades (lambda (entry) - (apply #'guix-list-mark + (apply #'guix-list--mark 'upgrade nil (guix-get-installed-outputs entry))))) @@ -661,7 +667,7 @@ The specification is suitable for `guix-process-package-actions'." (installed (guix-get-key-val entry 'installed))) (if installed (user-error "This output is already installed") - (guix-list-mark 'install t)))) + (guix-list--mark 'install t)))) (defun guix-output-list-mark-delete () "Mark the current output for deletion and move to the next line." @@ -670,7 +676,7 @@ The specification is suitable for `guix-process-package-actions'." (let* ((entry (guix-list-current-entry)) (installed (guix-get-key-val entry 'installed))) (if installed - (guix-list-mark 'delete t) + (guix-list--mark 'delete t) (user-error "This output is not installed")))) (defun guix-output-list-mark-upgrade () @@ -683,13 +689,13 @@ The specification is suitable for `guix-process-package-actions'." (user-error "This output is not installed")) (when (or (guix-get-key-val entry 'obsolete) (y-or-n-p "This output is not obsolete. Try to upgrade it anyway? ")) - (guix-list-mark 'upgrade t)))) + (guix-list--mark 'upgrade t)))) (defun guix-output-list-mark-upgrades () "Mark all obsolete package outputs for upgrading." (interactive) (guix-list-mark-package-upgrades - (lambda (_) (guix-list-mark 'upgrade)))) + (lambda (_) (guix-list--mark 'upgrade)))) (defun guix-output-list-execute () "Perform actions on the marked outputs." @@ -850,7 +856,7 @@ With ARG, mark all generations for deletion." (interactive "P") (if arg (guix-list-mark-all 'delete) - (guix-list-mark 'delete t))) + (guix-list--mark 'delete t))) (defun guix-generation-list-execute () "Delete marked generations." diff --git a/emacs/guix-main.scm b/emacs/guix-main.scm index b1662fbb80..c6e4a8259b 100644 --- a/emacs/guix-main.scm +++ b/emacs/guix-main.scm @@ -878,7 +878,8 @@ OUTPUTS is a list of package outputs (may be an empty list)." (format #t (N_ "~a package in profile~%" "~a packages in profile~%" count) - count)))))))))) + count) + (display-search-paths entries profile)))))))))) (define (delete-generations* profile generations) "Delete GENERATIONS from PROFILE. diff --git a/emacs/guix-pcomplete.el b/emacs/guix-pcomplete.el new file mode 100644 index 0000000000..fa71dd5e21 --- /dev/null +++ b/emacs/guix-pcomplete.el @@ -0,0 +1,392 @@ +;;; guix-pcomplete.el --- Functions for completing guix commands -*- lexical-binding: t -*- + +;; Copyright © 2015 Alex Kost <alezost@gmail.com> + +;; This file is part of GNU Guix. + +;; GNU Guix 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 3 of the License, or +;; (at your option) any later version. + +;; GNU Guix 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, see <http://www.gnu.org/licenses/>. + +;;; Commentary: + +;; This file provides completions for "guix" command that may be used in +;; `shell', `eshell' and wherever `pcomplete' works. + +;;; Code: + +(require 'pcomplete) +(require 'pcmpl-unix) +(require 'cl-lib) +(require 'guix-utils) + + +;;; Regexps for parsing various "guix ..." outputs + +(defvar guix-pcomplete-parse-package-regexp + (rx bol (group (one-or-more (not blank)))) + "Regexp used to find names of the packages.") + +(defvar guix-pcomplete-parse-command-regexp + (rx bol " " + (group wordchar (one-or-more (or wordchar "-")))) + "Regexp used to find guix commands. +'Command' means any option not prefixed with '-'. For example, +guix subcommand, system action, importer, etc.") + +(defvar guix-pcomplete-parse-long-option-regexp + (rx (or " " ", ") + (group "--" (one-or-more (or wordchar "-")) + (zero-or-one "="))) + "Regexp used to find long options.") + +(defvar guix-pcomplete-parse-short-option-regexp + (rx bol (one-or-more blank) + "-" (group (not (any "- ")))) + "Regexp used to find short options.") + +(defvar guix-pcomplete-parse-linter-regexp + (rx bol "- " (group (one-or-more (or wordchar "-")))) + "Regexp used to find 'lint' checkers.") + +(defvar guix-pcomplete-parse-regexp-group 1 + "Parenthesized expression of regexps used to find commands and +options.") + + +;;; Non-receivable completions + +(defvar guix-pcomplete-systems + '("x86_64-linux" "i686-linux" "armhf-linux" "mips64el-linux") + "List of supported systems.") + +(defvar guix-pcomplete-hash-formats + '("nix-base32" "base32" "base16" "hex" "hexadecimal") + "List of supported hash formats.") + +(defvar guix-pcomplete-refresh-subsets + '("core" "non-core") + "List of supported 'refresh' subsets.") + +(defvar guix-pcomplete-key-policies + '("interactive" "always" "never") + "List of supported key download policies.") + + +;;; Interacting with guix + +(defcustom guix-pcomplete-guix-program (executable-find "guix") + "Name of the 'guix' program. +It is used to find guix commands, options, packages, etc." + :type 'file + :group 'pcomplete + :group 'guix) + +(defun guix-pcomplete-run-guix (&rest args) + "Run `guix-pcomplete-guix-program' with ARGS. +Insert the output to the current buffer." + (apply #'call-process + guix-pcomplete-guix-program nil t nil args)) + +(defun guix-pcomplete-run-guix-and-search (regexp &optional group + &rest args) + "Run `guix-pcomplete-guix-program' with ARGS and search for matches. +Return a list of strings matching REGEXP. +GROUP specifies a parenthesized expression used in REGEXP." + (with-temp-buffer + (apply #'guix-pcomplete-run-guix args) + (goto-char (point-min)) + (let (result) + (while (re-search-forward regexp nil t) + (push (match-string-no-properties group) result)) + (nreverse result)))) + +(defmacro guix-pcomplete-define-options-finder (name docstring regexp + &optional filter) + "Define function NAME to receive guix options and commands. + +The defined function takes an optional COMMAND argument. This +function will run 'guix COMMAND --help' (or 'guix --help' if +COMMAND is nil) using `guix-pcomplete-run-guix-and-search' and +return its result. + +If FILTER is specified, it should be a function. The result is +passed to this FILTER as argument and the result value of this +function call is returned." + (declare (doc-string 2) (indent 1)) + `(guix-memoized-defun ,name (&optional command) + ,docstring + (let* ((args '("--help")) + (args (if command (cons command args) args)) + (res (apply #'guix-pcomplete-run-guix-and-search + ,regexp guix-pcomplete-parse-regexp-group args))) + ,(if filter + `(funcall ,filter res) + 'res)))) + +(guix-pcomplete-define-options-finder guix-pcomplete-commands + "If COMMAND is nil, return a list of available guix commands. +If COMMAND is non-nil (it should be a string), return available +subcommands, actions, etc. for this guix COMMAND." + guix-pcomplete-parse-command-regexp) + +(guix-pcomplete-define-options-finder guix-pcomplete-long-options + "Return a list of available long options for guix COMMAND." + guix-pcomplete-parse-long-option-regexp) + +(guix-pcomplete-define-options-finder guix-pcomplete-short-options + "Return a string with available short options for guix COMMAND." + guix-pcomplete-parse-short-option-regexp + (lambda (list) + (mapconcat #'identity list ""))) + +(guix-memoized-defun guix-pcomplete-all-packages () + "Return a list of all available Guix packages." + (guix-pcomplete-run-guix-and-search + guix-pcomplete-parse-package-regexp + guix-pcomplete-parse-regexp-group + "package" "--list-available")) + +(guix-memoized-defun guix-pcomplete-installed-packages (&optional profile) + "Return a list of Guix packages installed in PROFILE." + (let* ((args (and profile + (list (concat "--profile=" profile)))) + (args (append '("package" "--list-installed") args))) + (apply #'guix-pcomplete-run-guix-and-search + guix-pcomplete-parse-package-regexp + guix-pcomplete-parse-regexp-group + args))) + +(guix-memoized-defun guix-pcomplete-lint-checkers () + "Return a list of all available lint checkers." + (guix-pcomplete-run-guix-and-search + guix-pcomplete-parse-linter-regexp + guix-pcomplete-parse-regexp-group + "lint" "--list-checkers")) + + +;;; Completing + +(defvar guix-pcomplete-option-regexp (rx string-start "-") + "Regexp to match an option.") + +(defvar guix-pcomplete-long-option-regexp (rx string-start "--") + "Regexp to match a long option.") + +(defvar guix-pcomplete-long-option-with-arg-regexp + (rx string-start + (group "--" (one-or-more any)) "=" + (group (zero-or-more any))) + "Regexp to match a long option with its argument. +The first parenthesized group defines the option and the second +group - the argument.") + +(defvar guix-pcomplete-short-option-with-arg-regexp + (rx string-start + (group "-" (not (any "-"))) + (group (zero-or-more any))) + "Regexp to match a short option with its argument. +The first parenthesized group defines the option and the second +group - the argument.") + +(defun guix-pcomplete-match-option () + "Return non-nil, if the current argument is an option." + (pcomplete-match guix-pcomplete-option-regexp 0)) + +(defun guix-pcomplete-match-long-option () + "Return non-nil, if the current argument is a long option." + (pcomplete-match guix-pcomplete-long-option-regexp 0)) + +(defun guix-pcomplete-match-long-option-with-arg () + "Return non-nil, if the current argument is a long option with value." + (pcomplete-match guix-pcomplete-long-option-with-arg-regexp 0)) + +(defun guix-pcomplete-match-short-option-with-arg () + "Return non-nil, if the current argument is a short option with value." + (pcomplete-match guix-pcomplete-short-option-with-arg-regexp 0)) + +(defun guix-pcomplete-long-option-arg (option args) + "Return a long OPTION's argument from a list of arguments ARGS." + (let* ((re (concat "\\`" option "=\\(.*\\)")) + (args (cl-member-if (lambda (arg) + (string-match re arg)) + args)) + (cur (car args))) + (when cur + (match-string-no-properties 1 cur)))) + +(defun guix-pcomplete-short-option-arg (option args) + "Return a short OPTION's argument from a list of arguments ARGS." + (let* ((re (concat "\\`" option "\\(.*\\)")) + (args (cl-member-if (lambda (arg) + (string-match re arg)) + args)) + (cur (car args))) + (when cur + (let ((arg (match-string-no-properties 1 cur))) + (if (string= "" arg) + (cadr args) ; take the next arg + arg))))) + +(defun guix-pcomplete-complete-comma-args (entries) + "Complete comma separated arguments using ENTRIES." + (let ((index pcomplete-index)) + (while (= index pcomplete-index) + (let* ((args (if (or (guix-pcomplete-match-long-option-with-arg) + (guix-pcomplete-match-short-option-with-arg)) + (pcomplete-match-string 2 0) + (pcomplete-arg 0))) + (input (if (string-match ".*,\\(.*\\)" args) + (match-string-no-properties 1 args) + args))) + (pcomplete-here* entries input))))) + +(defun guix-pcomplete-complete-command-arg (command) + "Complete argument for guix COMMAND." + (cond + ((member command + '("archive" "build" "environment" "lint" "refresh")) + (while t + (pcomplete-here (guix-pcomplete-all-packages)))) + (t (pcomplete-here* (pcomplete-entries))))) + +(defun guix-pcomplete-complete-option-arg (command option &optional input) + "Complete argument for COMMAND's OPTION. +INPUT is the current partially completed string." + (cl-flet ((option? (short long) + (or (string= option short) + (string= option long))) + (command? (&rest commands) + (member command commands)) + (complete (entries) + (pcomplete-here entries input nil t)) + (complete* (entries) + (pcomplete-here* entries input t))) + (cond + ((option? "-L" "--load-path") + (complete* (pcomplete-dirs))) + ((string= "--key-download" option) + (complete* guix-pcomplete-key-policies)) + + ((command? "package") + (cond + ;; For '--install[=]' and '--remove[=]', try to complete a package + ;; name (INPUT) after the "=" sign, and then the rest packages + ;; separated with spaces. + ((option? "-i" "--install") + (complete (guix-pcomplete-all-packages)) + (while (not (guix-pcomplete-match-option)) + (pcomplete-here (guix-pcomplete-all-packages)))) + ((option? "-r" "--remove") + (let* ((profile (or (guix-pcomplete-short-option-arg + "-p" pcomplete-args) + (guix-pcomplete-long-option-arg + "--profile" pcomplete-args))) + (profile (and profile (expand-file-name profile)))) + (complete (guix-pcomplete-installed-packages profile)) + (while (not (guix-pcomplete-match-option)) + (pcomplete-here (guix-pcomplete-installed-packages profile))))) + ((string= "--show" option) + (complete (guix-pcomplete-all-packages))) + ((option? "-p" "--profile") + (complete* (pcomplete-dirs))) + ((option? "-m" "--manifest") + (complete* (pcomplete-entries))))) + + ((and (command? "archive" "build") + (option? "-s" "--system")) + (complete* guix-pcomplete-systems)) + + ((and (command? "build") + (option? "-r" "--root")) + (complete* (pcomplete-entries))) + + ((and (command? "environment") + (option? "-l" "--load")) + (complete* (pcomplete-entries))) + + ((and (command? "hash" "download") + (option? "-f" "--format")) + (complete* guix-pcomplete-hash-formats)) + + ((and (command? "lint") + (option? "-c" "--checkers")) + (guix-pcomplete-complete-comma-args + (guix-pcomplete-lint-checkers))) + + ((and (command? "publish") + (option? "-u" "--user")) + (complete* (pcmpl-unix-user-names))) + + ((and (command? "refresh") + (option? "-s" "--select")) + (complete* guix-pcomplete-refresh-subsets))))) + +(defun guix-pcomplete-complete-options (command) + "Complete options (with their arguments) for guix COMMAND." + (while (guix-pcomplete-match-option) + (let ((index pcomplete-index)) + (if (guix-pcomplete-match-long-option) + + ;; Long options. + (if (guix-pcomplete-match-long-option-with-arg) + (let ((option (pcomplete-match-string 1 0)) + (arg (pcomplete-match-string 2 0))) + (guix-pcomplete-complete-option-arg + command option arg)) + + (pcomplete-here* (guix-pcomplete-long-options command)) + ;; We support '--opt arg' style (along with '--opt=arg'), + ;; because 'guix package --install/--remove' may be used this + ;; way. So try to complete an argument after the option has + ;; been completed. + (unless (guix-pcomplete-match-option) + (guix-pcomplete-complete-option-arg + command (pcomplete-arg 0 -1)))) + + ;; Short options. + (let ((arg (pcomplete-arg 0))) + (if (> (length arg) 2) + ;; Support specifying an argument after a short option without + ;; spaces (for example, '-L/tmp/foo'). + (guix-pcomplete-complete-option-arg + command + (substring-no-properties arg 0 2) + (substring-no-properties arg 2)) + (pcomplete-opt (guix-pcomplete-short-options command)) + (guix-pcomplete-complete-option-arg + command (pcomplete-arg 0 -1))))) + + ;; If there were no completions, move to the next argument and get + ;; out if the last argument is achieved. + (when (= index pcomplete-index) + (if (= pcomplete-index pcomplete-last) + (throw 'pcompleted nil) + (pcomplete-next-arg)))))) + +;;;###autoload +(defun pcomplete/guix () + "Completion for `guix'." + (let ((commands (guix-pcomplete-commands))) + (pcomplete-here* (cons "--help" commands)) + (let ((command (pcomplete-arg 'first 1))) + (when (member command commands) + (guix-pcomplete-complete-options command) + (let ((subcommands (guix-pcomplete-commands command))) + (when subcommands + (pcomplete-here* subcommands))) + (guix-pcomplete-complete-options command) + (guix-pcomplete-complete-command-arg command))))) + +(provide 'guix-pcomplete) + +;;; guix-pcomplete.el ends here diff --git a/emacs/guix-utils.el b/emacs/guix-utils.el index 823c646610..dc0c58a114 100644 --- a/emacs/guix-utils.el +++ b/emacs/guix-utils.el @@ -1,6 +1,6 @@ -;;; guix-utils.el --- General utility functions +;;; guix-utils.el --- General utility functions -*- lexical-binding: t -*- -;; Copyright © 2014 Alex Kost <alezost@gmail.com> +;; Copyright © 2014, 2015 Alex Kost <alezost@gmail.com> ;; This file is part of GNU Guix. @@ -170,6 +170,35 @@ accessed with KEYS." "Same as `diff', but use `guix-diff-switches' as default." (diff old new (or switches guix-diff-switches) no-async)) + +;;; Memoizing + +(defun guix-memoize (function) + "Return a memoized version of FUNCTION." + (let ((cache (make-hash-table :test 'equal))) + (lambda (&rest args) + (let ((result (gethash args cache 'not-found))) + (if (eq result 'not-found) + (let ((result (apply function args))) + (puthash args result cache) + result) + result))))) + +(defmacro guix-memoized-defun (name arglist docstring &rest body) + "Define a memoized function NAME. +See `defun' for the meaning of arguments." + (declare (doc-string 3) (indent 2)) + `(defalias ',name + (guix-memoize (lambda ,arglist ,@body)) + ;; Add '(name args ...)' string with real arglist to the docstring, + ;; because *Help* will display '(name &rest ARGS)' for a defined + ;; function (since `guix-memoize' returns a lambda with '(&rest + ;; args)'). + ,(format "(%S %s)\n\n%s" + name + (mapconcat #'symbol-name arglist " ") + docstring))) + (provide 'guix-utils) ;;; guix-utils.el ends here |