;;; -*- lexical-binding: t -*-

(require 'syd/base)
(require 'syd/handle)


;;; Pairs

(with-eval-after-load 'evil-surround
  ;; In Elisp, `' is a much more common pair than ``.
  (syd-add-hook 'emacs-lisp-mode-hook
    (defun syd-elisp-evil-surround-pairs-h ()
      (push '(?` . ("`" . "'")) evil-surround-pairs-alist))))


;;; Ielm

;; Keep junk out of `user-emacs-directory'.
(setq ielm-history-file-name
      (file-name-concat syd-data-dir "ielm-history.eld"))


;;; Define handlers

(defun syd-elisp-open-repl ()
  (interactive)
  (pop-to-buffer
   (or (get-buffer "*ielm*")
       (progn (ielm) ; Creates the *ielm* buffer.
              (let ((b (get-buffer "*ielm*")))
                ;; We leave it to the enclosing `pop-to-buffer' to display the
                ;; buffer.
                (bury-buffer b)
                b)))))

(defun syd-elisp-lookup-documentation (identifier)
  "Lookup IDENTIFIER with `describe-symbol'"
  ;; HACK: Much to my frustration, `describe-symbol' has no defined return
  ;; value.  To test if the call was successful or not, we check if any window
  ;; is displaying the help buffer.  This breaks if
  ;; `syd-emacs-lisp-lookup-documentation' is called while the help buffer is
  ;; already open.
  (describe-symbol (intern identifier))
  (let ((buffer (get-buffer (help-buffer))))
    (and (get-buffer-window-list buffer)
         buffer)))

;; (handle '(emacs-lisp-mode lisp-interaction-mode
;;           inferior-emacs-lisp-mode)
;;         :repl #'syd-elisp-open-repl)



(provide 'syd/emacs-lisp)
