edit.scm 4.2 KB

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