1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2019 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 (gnu packages)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-37)
31 (list (option '(#\h "help") #f #f
35 (option '(#\V "version") #f #f
37 (show-version-and-exit "guix edit")))))
40 (display (G_ "Usage: guix edit PACKAGE...
41 Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
44 -h, --help display this help and exit"))
46 -V, --version display version information and exit"))
48 (show-bug-report-information))
51 ;; XXX: It would be better to default to something more likely to be
52 ;; pre-installed on an average GNU system. Since Nano is not suited for
53 ;; editing Scheme, Emacs is used instead.
54 (make-parameter (or (getenv "VISUAL") (getenv "EDITOR") "emacs")))
56 (define (search-path* path file)
57 "Like 'search-path' but exit if FILE is not found."
58 (let ((absolute-file-name (search-path path file)))
59 (unless absolute-file-name
60 ;; Shouldn't happen unless somebody fiddled with the 'location' field.
61 (leave (G_ "file '~a' not found in search path ~s~%")
65 (define (location->location-specification location)
66 "Return the location specification for LOCATION for a typical editor command
68 (list (string-append "+"
70 (location-line location)))
71 (search-path* %load-path (location-file location))))
74 (define (guix-edit . args)
75 (define (parse-arguments)
76 ;; Return the list of package names.
77 (args-fold* args %options
78 (lambda (opt name arg result)
79 (leave (G_ "~A: unrecognized option~%") name))
84 (let* ((specs (reverse (parse-arguments)))
85 (locations (map specification->location specs)))
89 (let ((file-names (append-map location->location-specification
91 ;; Use `system' instead of `exec' in order to sanely handle
92 ;; possible command line arguments in %EDITOR.
93 (exit (system (string-join (cons (%editor) file-names))))))
95 (let ((errno (system-error-errno args)))
96 (leave (G_ "failed to launch '~a': ~a~%")
97 (%editor) (strerror errno))))))))