edit.scm 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. (define-module (guix scripts edit)
  20. #:use-module (guix ui)
  21. #:use-module (guix scripts)
  22. #:use-module (guix utils)
  23. #:use-module (gnu packages)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-37)
  26. #:export (%editor
  27. guix-edit))
  28. (define %options
  29. (list (option '(#\h "help") #f #f
  30. (lambda args
  31. (show-help)
  32. (exit 0)))
  33. (option '(#\V "version") #f #f
  34. (lambda args
  35. (show-version-and-exit "guix edit")))))
  36. (define (show-help)
  37. (display (G_ "Usage: guix edit PACKAGE...
  38. Start $VISUAL or $EDITOR to edit the definitions of PACKAGE...\n"))
  39. (newline)
  40. (display (G_ "
  41. -h, --help display this help and exit"))
  42. (display (G_ "
  43. -V, --version display version information and exit"))
  44. (newline)
  45. (show-bug-report-information))
  46. (define %editor
  47. ;; XXX: It would be better to default to something more likely to be
  48. ;; pre-installed on an average GNU system. Since Nano is not suited for
  49. ;; editing Scheme, Emacs is used instead.
  50. (make-parameter (or (getenv "VISUAL") (getenv "EDITOR") "emacs")))
  51. (define (search-path* path file)
  52. "Like 'search-path' but exit if FILE is not found."
  53. (let ((absolute-file-name (search-path path file)))
  54. (unless absolute-file-name
  55. ;; Shouldn't happen unless somebody fiddled with the 'location' field.
  56. (leave (G_ "file '~a' not found in search path ~s~%")
  57. file path))
  58. absolute-file-name))
  59. (define (location->location-specification location)
  60. "Return the location specification for LOCATION for a typical editor command
  61. line."
  62. (list (string-append "+"
  63. (number->string
  64. (location-line location)))
  65. (search-path* %load-path (location-file location))))
  66. (define (guix-edit . args)
  67. (define (parse-arguments)
  68. ;; Return the list of package names.
  69. (args-fold* args %options
  70. (lambda (opt name arg result)
  71. (leave (G_ "~A: unrecognized option~%") name))
  72. cons
  73. '()))
  74. (with-error-handling
  75. (let* ((specs (reverse (parse-arguments)))
  76. (locations (map specification->location specs)))
  77. (catch 'system-error
  78. (lambda ()
  79. (let ((file-names (append-map location->location-specification
  80. locations)))
  81. ;; Use `system' instead of `exec' in order to sanely handle
  82. ;; possible command line arguments in %EDITOR.
  83. (exit (system (string-join (cons (%editor) file-names))))))
  84. (lambda args
  85. (let ((errno (system-error-errno args)))
  86. (leave (G_ "failed to launch '~a': ~a~%")
  87. (%editor) (strerror errno))))))))