emacs: Improve buffer procedures.
[jackhill/guix/guix.git] / emacs / guix-backend.el
index 46d0f06..93d0ed1 100644 (file)
@@ -121,6 +121,28 @@ This REPL is used for receiving information only if
 (defvar guix-internal-repl-buffer-name "*Guix Internal REPL*"
   "Default name of an internal Guix REPL buffer.")
 
+(defvar guix-before-repl-operation-hook nil
+  "Hook run before executing an operation in Guix REPL.")
+
+(defvar guix-after-repl-operation-hook
+  '(guix-repl-operation-success-message)
+  "Hook run after executing successful operation in Guix REPL.")
+
+(defvar guix-repl-operation-p nil
+  "Non-nil, if current operation is performed by `guix-eval-in-repl'.
+This internal variable is used to distinguish Guix operations
+from operations performed in Guix REPL by a user.")
+
+(defvar guix-repl-operation-type nil
+  "Type of the current operation performed by `guix-eval-in-repl'.
+This internal variable is used to define what actions should be
+executed after the current operation succeeds.
+See `guix-eval-in-repl' for details.")
+
+(defun guix-repl-operation-success-message ()
+  "Message telling about successful Guix operation."
+  (message "Guix operation has been performed."))
+
 (defun guix-get-guile-program (&optional internal)
   "Return a value suitable for `geiser-guile-binary'."
   (if (or internal
@@ -184,12 +206,9 @@ this address (it should be defined by
       (geiser-impl--set-buffer-implementation impl)
       (geiser-repl--autodoc-mode -1)
       (goto-char (point-max))
-      (let* ((prompt-re (geiser-repl--prompt-regexp impl))
-             (deb-prompt-re (geiser-repl--debugger-prompt-regexp impl))
-             (prompt (geiser-con--combined-prompt prompt-re deb-prompt-re)))
-        (or prompt-re
-            (error "Oh no! Guix REPL in the buffer '%s' has not been started"
-                   (buffer-name buffer)))
+      (let ((prompt (geiser-con--combined-prompt
+                     geiser-guile--prompt-regexp
+                     geiser-guile--debugger-prompt-regexp)))
         (geiser-repl--save-remote-data address)
         (geiser-repl--start-scheme impl address prompt)
         (geiser-repl--quit-setup)
@@ -199,18 +218,42 @@ this address (it should be defined by
         (setq geiser-repl--connection
               (geiser-con--make-connection
                (get-buffer-process (current-buffer))
-               prompt-re
-               deb-prompt-re))
+               geiser-guile--prompt-regexp
+               geiser-guile--debugger-prompt-regexp))
         (geiser-repl--startup impl address)
         (geiser-repl--autodoc-mode 1)
         (geiser-company--setup geiser-repl-company-p)
         (add-hook 'comint-output-filter-functions
-                  'geiser-repl--output-filter
+                  'guix-repl-output-filter
                   nil t)
         (set-process-query-on-exit-flag
          (get-buffer-process (current-buffer))
          geiser-repl-query-on-kill-p)))))
 
+(defun guix-repl-output-filter (str)
+  "Filter function suitable for `comint-output-filter-functions'.
+This is a replacement for `geiser-repl--output-filter'."
+  (cond
+   ((string-match-p geiser-guile--prompt-regexp str)
+    (geiser-autodoc--disinhibit-autodoc)
+    (when guix-repl-operation-p
+      (setq guix-repl-operation-p nil)
+      (run-hooks 'guix-after-repl-operation-hook)
+      ;; Run hooks specific to the current operation type.
+      (when guix-repl-operation-type
+        (let ((type-hook (intern
+                          (concat "guix-after-"
+                                  (symbol-name guix-repl-operation-type)
+                                  "-hook"))))
+          (setq guix-repl-operation-type nil)
+          (and (boundp type-hook)
+               (run-hooks type-hook))))))
+   ((string-match geiser-guile--debugger-prompt-regexp str)
+    (setq guix-repl-operation-p nil)
+    (geiser-con--connection-set-debugging geiser-repl--connection
+                                          (match-beginning 0))
+    (geiser-autodoc--disinhibit-autodoc))))
+
 (defun guix-get-repl-buffer (&optional internal)
   "Return Guix REPL buffer; start REPL if needed.
 If INTERNAL is non-nil, return an additional internal REPL."
@@ -245,6 +288,9 @@ additional internal REPL if it exists."
 \f
 ;;; Evaluating expressions
 
+(defvar guix-operation-buffer nil
+  "Buffer from which the latest Guix operation was performed.")
+
 (defun guix-make-guile-expression (fun &rest args)
   "Return string containing a guile expression for calling FUN with ARGS."
   (format "(%S %s)" fun
@@ -286,8 +332,18 @@ Return elisp expression of the first result value of evaluation."
          (replace-regexp-in-string
           "#t" "t" (car (guix-eval str wrap))))))
 
-(defun guix-eval-in-repl (str)
-  "Switch to Guix REPL and evaluate STR with guile expression there."
+(defun guix-eval-in-repl (str &optional operation-buffer operation-type)
+  "Switch to Guix REPL and evaluate STR with guile expression there.
+If OPERATION-BUFFER is non-nil, it should be a buffer from which
+the current operation was performed.
+
+If OPERATION-TYPE is non-nil, it should be a symbol.  After
+successful executing of the current operation,
+`guix-after-OPERATION-TYPE-hook' is called."
+  (run-hooks 'guix-before-repl-operation-hook)
+  (setq guix-repl-operation-p t
+        guix-repl-operation-type operation-type
+        guix-operation-buffer operation-buffer)
   (let ((repl (guix-get-repl-buffer)))
     (with-current-buffer repl
       (delete-region (geiser-repl--last-prompt-end) (point-max))