summaryrefslogtreecommitdiff
path: root/elisp/geiser-compile.el
blob: b1a07ff2b661b8d60c7e588630961f52302b97d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;;; geiser-compile.el -- compile/load scheme files  -*- lexical-binding: t; -*-

;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2016, 2018, 2021 Jose Antonio Ortega Ruiz

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.

;; Start date: Wed Feb 11, 2009 00:16


;;; Code:

(require 'geiser-debug)
(require 'geiser-autodoc)
(require 'geiser-eval)
(require 'geiser-base)
(require 'geiser-repl)


;;; Auxiliary functions:

(defun geiser-compile--buffer/path (&optional path)
  (let ((path (or path (read-file-name "Scheme file: " nil nil t))))
    (let ((buffer (find-file-noselect path)))
      (when (and (buffer-modified-p buffer)
                 (y-or-n-p "Save buffer? "))
        (save-buffer buffer))
      (cons buffer path))))

(defun geiser-compile--display-result (title ret)
  (if (not (geiser-eval--retort-error ret))
      (message "%s done" title)
    (message ""))
  (geiser-debug--display-retort title ret))

(defun geiser-compile--file-op (path compile-p msg)
  (let* ((b/p (geiser-compile--buffer/path path))
         (path (cdr b/p))
         (msg (format "%s %s ..." msg path))
         (code `(,(if compile-p :comp-file :load-file) ,path))
         (cont (lambda (ret) (geiser-compile--display-result msg ret))))
    (message msg)
    (geiser-autodoc--clean-cache)
    (geiser-eval--send code cont)))

(defun geiser-compile--ensure-repl (force)
  (when (or force
            (and (not (geiser-repl--repl-buffer-p))
                 (yes-or-no-p "No REPL is running: start it?")))
    (geiser-repl-restart-repl)))


;;; User commands:

(defun geiser-compile-file (path)
  "Compile and load Scheme file."
  (interactive "FScheme file: ")
  (geiser-compile--file-op path t "Compiling"))

(defun geiser-compile-current-buffer (&optional restart)
  "Compile and load current Scheme file.

With prefix, restart REPL before compiling the file."
  (interactive "P")
  (geiser-compile--ensure-repl restart)
  (geiser-compile-file (buffer-file-name (current-buffer))))

(defun geiser-load-file (path)
  "Load Scheme file."
  (interactive "FScheme file: ")
  (geiser-compile--ensure-repl nil)
  (geiser-compile--file-op (file-local-name (expand-file-name path)) nil "Loading"))

(defun geiser-load-current-buffer (&optional restart)
  "Load current Scheme file.

With prefix, restart REPL before loading the file."
  (interactive "P")
  (geiser-compile--ensure-repl restart)
  (geiser-load-file (buffer-file-name (current-buffer))))

;;;###autoload
(defun geiser-add-to-load-path (path)
  "Add a new directory to running Scheme's load path.
When called interactively, this function will ask for the path to
add, defaulting to the current buffer's directory."
  (interactive "DDirectory to add: ")
  (let* ((c `(:eval (:ge add-to-load-path ,(expand-file-name path))))
         (r (geiser-eval--send/result c)))
    (message "%s%s added to load path" path (if r "" " couldn't be"))))


(provide 'geiser-compile)