gnu: surgescript: Update to 0.5.4.4.
[jackhill/guix/guix.git] / guix / scripts / edit.scm
CommitLineData
39bee8a2 1;;; GNU Guix --- Functional package management for GNU
3794ce93 2;;; Copyright © 2015, 2016, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
b16dbd13 3;;; Copyright © 2015 Mathieu Lirzin <mthl@gnu.org>
3c8396b5 4;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
39bee8a2
LC
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)
88981dd3 23 #:use-module (guix scripts)
3c8396b5 24 #:use-module ((guix scripts build) #:select (%standard-build-options))
39bee8a2 25 #:use-module (guix utils)
39bee8a2
LC
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
3c8396b5 33 (list (find (lambda (option)
34 (member "load-path" (option-names option)))
35 %standard-build-options)
36 (option '(#\h "help") #f #f
39bee8a2
LC
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)
69daee23 45 (display (G_ "Usage: guix edit PACKAGE...
6237b9fa 46Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
3c8396b5 47 (newline)
48 (display (G_ "
49 -L, --load-path=DIR prepend DIR to the package module search path"))
39bee8a2 50 (newline)
69daee23 51 (display (G_ "
39bee8a2 52 -h, --help display this help and exit"))
69daee23 53 (display (G_ "
39bee8a2
LC
54 -V, --version display version information and exit"))
55 (newline)
56 (show-bug-report-information))
57
58(define %editor
47f82b31
RG
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")))
39bee8a2
LC
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.
69daee23 68 (leave (G_ "file '~a' not found in search path ~s~%")
39bee8a2
LC
69 file path))
70 absolute-file-name))
71
ee8099f5
LC
72(define (location->location-specification location)
73 "Return the location specification for LOCATION for a typical editor command
67c920fa 74line."
ee8099f5
LC
75 (list (string-append "+"
76 (number->string
77 (location-line location)))
78 (search-path* %load-path (location-file location))))
67c920fa 79
39bee8a2 80\f
3794ce93
LC
81(define-command (guix-edit . args)
82 (category packaging)
83 (synopsis "view and edit package definitions")
84
00677a7e
LC
85 (define (parse-arguments)
86 ;; Return the list of package names.
87 (args-fold* args %options
88 (lambda (opt name arg result)
69daee23 89 (leave (G_ "~A: unrecognized option~%") name))
00677a7e
LC
90 cons
91 '()))
92
39bee8a2 93 (with-error-handling
ee8099f5
LC
94 (let* ((specs (reverse (parse-arguments)))
95 (locations (map specification->location specs)))
650f1615
LC
96
97 (catch 'system-error
98 (lambda ()
ee8099f5
LC
99 (let ((file-names (append-map location->location-specification
100 locations)))
b16dbd13
ML
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))))))
650f1615
LC
104 (lambda args
105 (let ((errno (system-error-errno args)))
69daee23 106 (leave (G_ "failed to launch '~a': ~a~%")
650f1615 107 (%editor) (strerror errno))))))))