guile-emacs.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; guile-emacs.scm --- Guile Emacs interface
  2. ;; Copyright (C) 2001, 2010 Keisuke Nishida <kxn30@po.cwru.edu>
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free
  15. ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. ;;;; 02111-1307 USA
  17. ;;; Code:
  18. (use-modules (ice-9 regex))
  19. (use-modules (ice-9 channel))
  20. (use-modules (ice-9 session))
  21. (use-modules (ice-9 documentation))
  22. ;;;
  23. ;;; Emacs Lisp channel
  24. ;;;
  25. (define (emacs-lisp-channel)
  26. (define (native-type? x)
  27. (or (integer? x) (symbol? x) (string? x) (pair? x) (vector? x)))
  28. (define (emacs-lisp-print ch val)
  29. (cond
  30. ((unspecified? val))
  31. ((eq? val #t) (channel-print-value ch 't))
  32. ((or (eq? val #f) (null? val)) (channel-print-value ch 'nil))
  33. ((native-type? val) (channel-print-value ch val))
  34. (else (channel-print-token ch val))))
  35. (channel-open (make-object-channel emacs-lisp-print)))
  36. ;;;
  37. ;;; Scheme channel
  38. ;;;
  39. (define (emacs-scheme-channel)
  40. (define (print ch val) (channel-print-value ch (object->string val)))
  41. (channel-open (make-object-channel print)))
  42. ;;;
  43. ;;; for guile-import and guile-import-module
  44. ;;;
  45. (define (guile-emacs-export-procedure name proc docs)
  46. (define (procedure-args proc)
  47. (let ((source (procedure-source proc)))
  48. (if source
  49. ;; formals -> emacs args
  50. (let loop ((formals (cadr source)))
  51. (cond
  52. ((null? formals) '())
  53. ((symbol? formals) `(&rest ,formals))
  54. (else (cons (car formals) (loop (cdr formals))))))
  55. ;; arity -> emacs args
  56. (let* ((arity (procedure-minimum-arity proc))
  57. (nreqs (car arity))
  58. (nopts (cadr arity))
  59. (restp (caddr arity)))
  60. (define (nsyms n)
  61. (if (= n 0) '() (cons (gensym "a") (nsyms (1- n)))))
  62. (append! (nsyms nreqs)
  63. (if (> nopts 0) (cons '&optional (nsyms nopts)) '())
  64. (if restp (cons '&rest (nsyms 1)) '()))))))
  65. (define (procedure-call name args)
  66. (let ((restp (memq '&rest args))
  67. (args (delq '&rest (delq '&optional args))))
  68. (if restp
  69. `('apply ',name ,@args)
  70. `(',name ,@args))))
  71. (let ((args (procedure-args proc))
  72. (docs (and docs (object-documentation proc))))
  73. `(defun ,name ,args
  74. ,@(if docs (list docs) '())
  75. (guile-lisp-flat-eval ,@(procedure-call (procedure-name proc) args)))))
  76. (define (guile-emacs-export proc-name func-name docs)
  77. (let ((proc (module-ref (current-module) proc-name)))
  78. (guile-emacs-export-procedure func-name proc docs)))
  79. (define (guile-emacs-export-procedures module-name docs)
  80. (define (module-public-procedures name)
  81. (hash-fold (lambda (s v d)
  82. (let ((val (variable-ref v)))
  83. (if (procedure? val) (acons s val d) d)))
  84. '() (module-obarray (resolve-interface name))))
  85. `(progn ,@(map (lambda (n+p)
  86. (guile-emacs-export-procedure (car n+p) (cdr n+p) docs))
  87. (module-public-procedures module-name))))
  88. ;;;
  89. ;;; for guile-scheme-complete-symbol
  90. ;;;
  91. (define (guile-emacs-complete-alist str)
  92. (sort! (apropos-fold (lambda (module name val data)
  93. (cons (list (symbol->string name)
  94. (cond ((procedure? val) " <p>")
  95. ((macro? val) " <m>")
  96. (else "")))
  97. data))
  98. '() (string-append "^" (regexp-quote str))
  99. apropos-fold-all)
  100. (lambda (p1 p2) (string<? (car p1) (car p2)))))
  101. ;;;
  102. ;;; for guile-scheme-apropos
  103. ;;;
  104. (define (guile-emacs-apropos regexp)
  105. (with-output-to-string (lambda () (apropos regexp))))
  106. ;;;
  107. ;;; for guile-scheme-describe
  108. ;;;
  109. (define (guile-emacs-describe sym)
  110. (object-documentation (eval sym (current-module))))
  111. ;;;
  112. ;;; Guile 1.4 compatibility
  113. ;;;
  114. (define object->string
  115. (if (defined? 'object->string)
  116. object->string
  117. (lambda (x) (format #f "~S" x))))
  118. ;;; guile-emacs.scm ends here