1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015, 2016, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mathieu Lirzin <mthl@gnu.org>
4 ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix scripts edit)
22 #:use-module (guix ui)
23 #:use-module (guix scripts)
24 #:use-module ((guix scripts build) #:select (%standard-build-options))
25 #:use-module (guix utils)
26 #:use-module (gnu packages)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-37)
33 (list (find (lambda (option)
34 (member "load-path" (option-names option)))
35 %standard-build-options)
36 (option '(#\h "help") #f #f
40 (option '(#\V "version") #f #f
42 (show-version-and-exit "guix edit")))))
45 (display (G_ "Usage: guix edit PACKAGE...
46 Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
49 -L, --load-path=DIR prepend DIR to the package module search path"))
52 -h, --help display this help and exit"))
54 -V, --version display version information and exit"))
56 (show-bug-report-information))
59 ;; Nano is sensible default, as it is installed by base system.
60 ;; For development, user can set custom value for $EDITOR.
61 (make-parameter (or (getenv "VISUAL") (getenv "EDITOR") "nano")))
63 (define (search-path* path file)
64 "Like 'search-path' but exit if FILE is not found."
65 (let ((absolute-file-name (search-path path file)))
66 (unless absolute-file-name
67 ;; Shouldn't happen unless somebody fiddled with the 'location' field.
68 (leave (G_ "file '~a' not found in search path ~s~%")
72 (define (location->location-specification location)
73 "Return the location specification for LOCATION for a typical editor command
75 (list (string-append "+"
77 (location-line location)))
78 (search-path* %load-path (location-file location))))
81 (define-command (guix-edit . args)
83 (synopsis "view and edit package definitions")
85 (define (parse-arguments)
86 ;; Return the list of package names.
87 (args-fold* args %options
88 (lambda (opt name arg result)
89 (leave (G_ "~A: unrecognized option~%") name))
94 (let* ((specs (reverse (parse-arguments)))
95 (locations (map specification->location specs)))
99 (let ((file-names (append-map location->location-specification
101 ;; Use `system' instead of `exec' in order to sanely handle
102 ;; possible command line arguments in %EDITOR.
103 (exit (system (string-join (cons (%editor) file-names))))))
105 (let ((errno (system-error-errno args)))
106 (leave (G_ "failed to launch '~a': ~a~%")
107 (%editor) (strerror errno))))))))