Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of the reader customizable #4

Open
sellout opened this issue May 18, 2023 · 1 comment
Open

Make use of the reader customizable #4

sellout opened this issue May 18, 2023 · 1 comment

Comments

@sellout
Copy link

sellout commented May 18, 2023

Rather than simply a progn at the end of elisp-reader, how about making it possible to enable/disable, then it could maybe be scoped for reading certain things.

(defvar er--original-read (symbol-function 'read))
(defvar er--original-read-from-string (symbol-function 'read-from-string))
(defvar er--original-load-read-function load-read-function)

(defun er-enable ()
  (interactive)
  (setq er--original-read (symbol-function 'read)
        er--original-read-from-string (symbol-function 'read-from-string)
        er--original-load-read-function load-read-function)
  (fset 'read (symbol-function 'er-read))
  (fset 'read-from-string (symbol-function 'er-read-from-string))
  (setq load-read-function (symbol-function 'er-read)))

(defun er-disable ()
  (interactive)
  (fset 'read er-original-read)
  (fset 'read-from-string er--original-read-from-string)
  (setq load-read-function er--original-load-read-function))

(defcustom er-enabled nil
  "Whether to use the extensible elisp-reader when reading elisp."
  :set
  (lambda (option new-value)
    (if new-value (er-enable) (er-disable))))
@nagy
Copy link

nagy commented May 16, 2024

Unfortunately, disabling of the reader is still sometimes needed yet. For example, when I attempt to inspect the variable browse-url-handlers with the helpful package, I receive an error when I have this elisp reader implementation active.

To add to your functions above, I did:

(defun er-enabled-p ()
  (eq (symbol-function 'read) (symbol-function 'er-read)))

(defun without-er-reader-a (orig-fn &rest args)
  "An advice to disable elisp-reader for this function only.

To be used with `advice-add' and `:around'."
  (let ((was-enabled (er-enabled-p)))
    (unwind-protect
        (progn
          (when was-enabled (er-disable))
          (apply orig-fn args))
      (when was-enabled (er-enable)))))

;; `helpful-update' has some problems with the new elisp reader.
(advice-add 'helpful-update :around #'without-er-reader-a)

This disables the reader only for this particular function.

Just for reference, this is the stacktrace, that I receive but this is unrelated to this bug report:

Stacktrace
Debugger entered--Lisp error: (error "Unexpected error whilst reading /nix/store/rajkyxn1ssi2pi17wf0imw7s289f3bg5-emacs-gtk3-29.3/share/emacs/29.3/lisp/net/browse-url.el.gz position 69913: (error End of file during parsing)")
  (error "Unexpected error whilst reading %s position %s: %s" "/nix/store/rajkyxn1ssi2pi17wf0imw7s289f3bg5-emacs-gtk3-29.3/share/emacs/29.3/lisp/net/browse-url.el.gz" 69913 (error "End of file during parsing"))
  (elisp-refs--read-all-buffer-forms # nil)
  (elisp-refs--read-and-find # browse-url-handlers elisp-refs--variable-p)
  (#f(compiled-function (buf) #) #)
  (elisp-refs--search-1 (#) #f(compiled-function (buf) #))
  (helpful--reference-positions browse-url-handlers nil #)
  (helpful--calculate-references browse-url-handlers nil "/nix/store/rajkyxn1ssi2pi17wf0imw7s289f3bg5-emacs-gtk3-29.3/share/emacs/29.3/lisp/net/browse-url.el.gz")
  (#)
  (apply # nil)
  (progn (apply orig-fn args))
  (unwind-protect (progn (apply orig-fn args)))
  (let ((was-enabled (er-enabled-p))) (unwind-protect (progn (apply orig-fn args))))
  (without-er-reader #)
  (apply without-er-reader # nil)
  (helpful-update)
  (helpful--update-and-switch-buffer browse-url-handlers nil)
  (helpful-variable browse-url-handlers)
  (funcall-interactively helpful-variable browse-url-handlers)
  (command-execute helpful-variable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants