guix-location.el 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ;;; guix-location.el --- Package locations
  2. ;; Copyright © 2016–2017 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides the code to work with locations of Guix packages.
  18. ;;; Code:
  19. (require 'cl-lib)
  20. (require 'guix-repl)
  21. (require 'guix-read)
  22. (require 'guix-guile)
  23. (defun guix-package-location (id-or-name)
  24. "Return location of a package with ID-OR-NAME.
  25. For the meaning of location, see `guix-find-location'."
  26. (guix-eval-read (guix-make-guile-expression
  27. 'package-location-string id-or-name)))
  28. ;;;###autoload
  29. (defun guix-find-location (location &optional directory)
  30. "Go to LOCATION of a package.
  31. LOCATION is a string of the form:
  32. \"FILE:LINE:COLUMN\"
  33. If FILE is relative, it is considered to be relative to
  34. DIRECTORY (if it is specified and exists).
  35. Interactively, prompt for LOCATION. With prefix argument, prompt
  36. for DIRECTORY as well."
  37. (interactive
  38. (list (guix-read-package-location)
  39. (guix-read-directory)))
  40. (cl-multiple-value-bind (file line column)
  41. (split-string location ":")
  42. (let* ((file-name (expand-file-name file (or directory
  43. guix-directory)))
  44. (file-name (if (file-exists-p file-name)
  45. file-name
  46. (guix-eval-read
  47. (guix-make-guile-expression
  48. 'search-load-path file)))))
  49. (unless file-name ; not found in Guile %load-path
  50. (error "Package location file not found: %s" file))
  51. (find-file file-name))
  52. (when (and line column)
  53. (let ((line (string-to-number line))
  54. (column (string-to-number column)))
  55. (goto-char (point-min))
  56. (forward-line (- line 1))
  57. (move-to-column column)
  58. (recenter 1)))))
  59. ;;;###autoload
  60. (defun guix-edit (id-or-name &optional directory)
  61. "Edit (go to location of) package with ID-OR-NAME.
  62. See `guix-find-location' for the meaning of package location and
  63. DIRECTORY.
  64. Interactively, with prefix argument, prompt for DIRECTORY."
  65. (interactive
  66. (list (guix-read-package-name)
  67. (guix-read-directory)))
  68. (let ((loc (guix-package-location id-or-name)))
  69. (if loc
  70. (guix-find-location loc directory)
  71. (message "Couldn't find package location."))))
  72. (provide 'guix-location)
  73. ;;; guix-location.el ends here