gnu: surgescript: Update to 0.5.4.4.
[jackhill/guix/guix.git] / guix / scripts / edit.scm
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>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
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)
29 #:export (%editor
30 guix-edit))
31
32 (define %options
33 (list (find (lambda (option)
34 (member "load-path" (option-names option)))
35 %standard-build-options)
36 (option '(#\h "help") #f #f
37 (lambda args
38 (show-help)
39 (exit 0)))
40 (option '(#\V "version") #f #f
41 (lambda args
42 (show-version-and-exit "guix edit")))))
43
44 (define (show-help)
45 (display (G_ "Usage: guix edit PACKAGE...
46 Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
47 (newline)
48 (display (G_ "
49 -L, --load-path=DIR prepend DIR to the package module search path"))
50 (newline)
51 (display (G_ "
52 -h, --help display this help and exit"))
53 (display (G_ "
54 -V, --version display version information and exit"))
55 (newline)
56 (show-bug-report-information))
57
58 (define %editor
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")))
62
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~%")
69 file path))
70 absolute-file-name))
71
72 (define (location->location-specification location)
73 "Return the location specification for LOCATION for a typical editor command
74 line."
75 (list (string-append "+"
76 (number->string
77 (location-line location)))
78 (search-path* %load-path (location-file location))))
79
80 \f
81 (define-command (guix-edit . args)
82 (category packaging)
83 (synopsis "view and edit package definitions")
84
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))
90 cons
91 '()))
92
93 (with-error-handling
94 (let* ((specs (reverse (parse-arguments)))
95 (locations (map specification->location specs)))
96
97 (catch 'system-error
98 (lambda ()
99 (let ((file-names (append-map location->location-specification
100 locations)))
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))))))
104 (lambda args
105 (let ((errno (system-error-errno args)))
106 (leave (G_ "failed to launch '~a': ~a~%")
107 (%editor) (strerror errno))))))))