edit.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015, 2016, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Mathieu Lirzin <mthl@gnu.org>
  4. ;;; Copyright © 2020, 2021 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. (define-module (guix scripts edit)
  21. #:use-module (guix ui)
  22. #:use-module (guix scripts)
  23. #:use-module ((guix scripts build) #:select (%standard-build-options))
  24. #:use-module (guix utils)
  25. #:use-module (gnu packages)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-37)
  28. #:export (%editor
  29. guix-edit))
  30. (define %options
  31. (list (find (lambda (option)
  32. (member "load-path" (option-names option)))
  33. %standard-build-options)
  34. (option '(#\h "help") #f #f
  35. (lambda args
  36. (show-help)
  37. (exit 0)))
  38. (option '(#\V "version") #f #f
  39. (lambda args
  40. (show-version-and-exit "guix edit")))))
  41. (define (show-help)
  42. (display (G_ "Usage: guix edit PACKAGE...
  43. Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
  44. (newline)
  45. (display (G_ "
  46. -L, --load-path=DIR prepend DIR to the package module search path"))
  47. (newline)
  48. (display (G_ "
  49. -h, --help display this help and exit"))
  50. (display (G_ "
  51. -V, --version display version information and exit"))
  52. (newline)
  53. (show-bug-report-information))
  54. (define %editor
  55. ;; Nano is sensible default, as it is installed by base system.
  56. ;; For development, user can set custom value for $EDITOR.
  57. (make-parameter (or (getenv "VISUAL") (getenv "EDITOR") "nano")))
  58. (define (search-path* path file)
  59. "Like 'search-path' but exit if FILE is not found."
  60. (let ((absolute-file-name (search-path path file)))
  61. (unless absolute-file-name
  62. ;; Shouldn't happen unless somebody fiddled with the 'location' field.
  63. (leave (G_ "file '~a' not found in search path ~s~%")
  64. file path))
  65. absolute-file-name))
  66. (define (location->location-specification location)
  67. "Return the location specification for LOCATION for a typical editor command
  68. line."
  69. (list (string-append "+"
  70. (number->string
  71. (location-line location)))
  72. (search-path* %load-path (location-file location))))
  73. (define-command (guix-edit . args)
  74. (category packaging)
  75. (synopsis "view and edit package definitions")
  76. (define (parse-arguments)
  77. ;; Return the list of package names.
  78. (parse-command-line args %options (list (list))
  79. #:build-options? #f
  80. #:argument-handler cons))
  81. (with-error-handling
  82. (let* ((specs (reverse (parse-arguments)))
  83. (locations (map specification->location specs)))
  84. (when (null? specs)
  85. (leave (G_ "no packages specified, nothing to edit~%")))
  86. (catch 'system-error
  87. (lambda ()
  88. (let ((file-names (append-map location->location-specification
  89. locations)))
  90. ;; Use `system' instead of `exec' in order to sanely handle
  91. ;; possible command line arguments in %EDITOR.
  92. (exit (system (string-join (cons (%editor) file-names))))))
  93. (lambda args
  94. (let ((errno (system-error-errno args)))
  95. (leave (G_ "failed to launch '~a': ~a~%")
  96. (%editor) (strerror errno))))))))