1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mathieu Lirzin <mthl@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix scripts edit)
21 #:use-module (guix ui)
22 #:use-module (guix scripts)
23 #:use-module (guix utils)
24 #:use-module (guix packages)
25 #:use-module (gnu packages)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-37)
32 (list (option '(#\h "help") #f #f
36 (option '(#\V "version") #f #f
38 (show-version-and-exit "guix edit")))))
41 (display (G_ "Usage: guix edit PACKAGE...
42 Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
45 -h, --help display this help and exit"))
47 -V, --version display version information and exit"))
49 (show-bug-report-information))
52 ;; XXX: It would be better to default to something more likely to be
53 ;; pre-installed on an average GNU system. Since Nano is not suited for
54 ;; editing Scheme, Emacs is used instead.
55 (make-parameter (or (getenv "VISUAL") (getenv "EDITOR") "emacs")))
57 (define (search-path* path file)
58 "Like 'search-path' but exit if FILE is not found."
59 (let ((absolute-file-name (search-path path file)))
60 (unless absolute-file-name
61 ;; Shouldn't happen unless somebody fiddled with the 'location' field.
62 (leave (G_ "file '~a' not found in search path ~s~%")
66 (define (package->location-specification package)
67 "Return the location specification for PACKAGE for a typical editor command
69 (let ((loc (package-location package)))
70 (list (string-append "+"
73 (search-path* %load-path (location-file loc)))))
76 (define (guix-edit . args)
77 (define (parse-arguments)
78 ;; Return the list of package names.
79 (args-fold* args %options
80 (lambda (opt name arg result)
81 (leave (G_ "~A: unrecognized option~%") name))
86 (let* ((specs (reverse (parse-arguments)))
87 (packages (map specification->package specs)))
88 (for-each (lambda (package)
89 (unless (package-location package)
90 (leave (G_ "source location of package '~a' is unknown~%")
91 (package-full-name package))))
96 (let ((file-names (append-map package->location-specification
98 ;; Use `system' instead of `exec' in order to sanely handle
99 ;; possible command line arguments in %EDITOR.
100 (exit (system (string-join (cons (%editor) file-names))))))
102 (let ((errno (system-error-errno args)))
103 (leave (G_ "failed to launch '~a': ~a~%")
104 (%editor) (strerror errno))))))))