Klimi's new dotfiles with stow.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

121 рядки
4.7 KiB

5 роки тому
  1. (require 'slime)
  2. (require 'tramp)
  3. (eval-when-compile (require 'cl)) ; lexical-let
  4. (define-slime-contrib slime-tramp
  5. "Filename translations for tramp"
  6. (:authors "Marco Baringer <mb@bese.it>")
  7. (:license "GPL")
  8. (:on-load
  9. (setq slime-to-lisp-filename-function #'slime-tramp-to-lisp-filename)
  10. (setq slime-from-lisp-filename-function #'slime-tramp-from-lisp-filename)))
  11. (defcustom slime-filename-translations nil
  12. "Assoc list of hostnames and filename translation functions.
  13. Each element is of the form (HOSTNAME-REGEXP TO-LISP FROM-LISP).
  14. HOSTNAME-REGEXP is a regexp which is applied to the connection's
  15. slime-machine-instance. If HOSTNAME-REGEXP maches then the
  16. corresponding TO-LISP and FROM-LISP functions will be used to
  17. translate emacs filenames and lisp filenames.
  18. TO-LISP will be passed the filename of an emacs buffer and must
  19. return a string which the underlying lisp understandas as a
  20. pathname. FROM-LISP will be passed a pathname as returned by the
  21. underlying lisp and must return something that emacs will
  22. understand as a filename (this string will be passed to
  23. find-file).
  24. This list will be traversed in order, so multiple matching
  25. regexps are possible.
  26. Example:
  27. Assuming you run emacs locally and connect to slime running on
  28. the machine 'soren' and you can connect with the username
  29. 'animaliter':
  30. (push (list \"^soren$\"
  31. (lambda (emacs-filename)
  32. (subseq emacs-filename (length \"/ssh:animaliter@soren:\")))
  33. (lambda (lisp-filename)
  34. (concat \"/ssh:animaliter@soren:\" lisp-filename)))
  35. slime-filename-translations)
  36. See also `slime-create-filename-translator'."
  37. :type '(repeat (list :tag "Host description"
  38. (regexp :tag "Hostname regexp")
  39. (function :tag "To lisp function")
  40. (function :tag "From lisp function")))
  41. :group 'slime-lisp)
  42. (defun slime-find-filename-translators (hostname)
  43. (cond ((cdr (cl-assoc-if (lambda (regexp) (string-match regexp hostname))
  44. slime-filename-translations)))
  45. (t (list #'identity #'identity))))
  46. (defun slime-make-tramp-file-name (username remote-host lisp-filename)
  47. "Tramp compatability function.
  48. Handles the signature of `tramp-make-tramp-file-name' changing
  49. over time."
  50. (cond
  51. ((>= emacs-major-version 26)
  52. ;; Emacs 26 requires the method to be provided and the signature of
  53. ;; `tramp-make-tramp-file-name' has changed.
  54. (tramp-make-tramp-file-name (tramp-find-method nil username remote-host)
  55. username
  56. nil
  57. remote-host
  58. nil
  59. lisp-filename))
  60. ((boundp 'tramp-multi-methods)
  61. (tramp-make-tramp-file-name nil nil
  62. username
  63. remote-host
  64. lisp-filename))
  65. (t
  66. (tramp-make-tramp-file-name nil
  67. username
  68. remote-host
  69. lisp-filename))))
  70. (cl-defun slime-create-filename-translator (&key machine-instance
  71. remote-host
  72. username)
  73. "Creates a three element list suitable for push'ing onto
  74. slime-filename-translations which uses Tramp to load files on
  75. hostname using username. MACHINE-INSTANCE is a required
  76. parameter, REMOTE-HOST defaults to MACHINE-INSTANCE and USERNAME
  77. defaults to (user-login-name).
  78. MACHINE-INSTANCE is the value returned by slime-machine-instance,
  79. which is just the value returned by cl:machine-instance on the
  80. remote lisp. REMOTE-HOST is the fully qualified domain name (or
  81. just the IP) of the remote machine. USERNAME is the username we
  82. should login with.
  83. The functions created here expect your tramp-default-method or
  84. tramp-default-method-alist to be setup correctly."
  85. (lexical-let ((remote-host (or remote-host machine-instance))
  86. (username (or username (user-login-name))))
  87. (list (concat "^" machine-instance "$")
  88. (lambda (emacs-filename)
  89. (tramp-file-name-localname
  90. (tramp-dissect-file-name emacs-filename)))
  91. `(lambda (lisp-filename)
  92. (slime-make-tramp-file-name
  93. ,username
  94. ,remote-host
  95. lisp-filename)))))
  96. (defun slime-tramp-to-lisp-filename (filename)
  97. (funcall (if (slime-connected-p)
  98. (first (slime-find-filename-translators (slime-machine-instance)))
  99. 'identity)
  100. (expand-file-name filename)))
  101. (defun slime-tramp-from-lisp-filename (filename)
  102. (funcall (second (slime-find-filename-translators (slime-machine-instance)))
  103. filename))
  104. (provide 'slime-tramp)