edit: Use 'specification->location' to read information from the cache.
[jackhill/guix/guix.git] / guix / scripts / edit.scm
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>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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)
27 #:export (%editor
28 guix-edit))
29
30 (define %options
31 (list (option '(#\h "help") #f #f
32 (lambda args
33 (show-help)
34 (exit 0)))
35 (option '(#\V "version") #f #f
36 (lambda args
37 (show-version-and-exit "guix edit")))))
38
39 (define (show-help)
40 (display (G_ "Usage: guix edit PACKAGE...
41 Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
42 (newline)
43 (display (G_ "
44 -h, --help display this help and exit"))
45 (display (G_ "
46 -V, --version display version information and exit"))
47 (newline)
48 (show-bug-report-information))
49
50 (define %editor
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")))
55
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~%")
62 file path))
63 absolute-file-name))
64
65 (define (location->location-specification location)
66 "Return the location specification for LOCATION for a typical editor command
67 line."
68 (list (string-append "+"
69 (number->string
70 (location-line location)))
71 (search-path* %load-path (location-file location))))
72
73 \f
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))
80 cons
81 '()))
82
83 (with-error-handling
84 (let* ((specs (reverse (parse-arguments)))
85 (locations (map specification->location specs)))
86
87 (catch 'system-error
88 (lambda ()
89 (let ((file-names (append-map location->location-specification
90 locations)))
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))))))
94 (lambda args
95 (let ((errno (system-error-errno args)))
96 (leave (G_ "failed to launch '~a': ~a~%")
97 (%editor) (strerror errno))))))))