Skip to content

Commit

Permalink
Promote parinfer-rust-buffer-replace-strategy to defcustom (#90)
Browse files Browse the repository at this point in the history
* Upgrade parinfer-rust-buffer-replace-strategy to defcustom

The speed gained from choosing fast is probably worth it to some users
that this should be moved to officially supported rather than just
hinted at.

* Switch references parinfer-rust-enabled to parinfer-rust-mode

`parinfer-rust-enabled` was used when I understood things less

* Fix bug where parinfer-rust-mode wouldn't disable itself
  • Loading branch information
justinbarclay authored May 4, 2024
1 parent 1b37b9d commit 30d0ea5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
5 changes: 2 additions & 3 deletions parinfer-rust-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

;;; Code:
(eval-when-compile
(defvar parinfer-rust-enabled)
(defvar parinfer-rust--mode)
(defvar parinfer-rust-dim-parens))
(require 'url)
Expand Down Expand Up @@ -186,7 +185,7 @@ This search is bound to occur before LIMIT."

(defun parinfer-rust--dim-parens ()
"Apply paren dimming if appropriate."
(if (and parinfer-rust-enabled
(if (and parinfer-rust-mode
(not (string-equal parinfer-rust--mode "paren"))
parinfer-rust-dim-parens)
(font-lock-add-keywords
Expand Down Expand Up @@ -239,7 +238,7 @@ mode to better emulate users."
(defun parinfer-rust--defer-loading (&rest _)
"Defer loading of `parinfer-rust-mode' until the buffer is in focus."
;; This is a parinfer enabled buffer that started in the background and has now been moved to the foreground
(when (and parinfer-rust-enabled
(when (and parinfer-rust-mode
(eq (current-buffer)
(window-buffer (selected-window))))
(remove-hook 'window-selection-change-functions #'parinfer-rust--defer-loading t)
Expand Down
50 changes: 27 additions & 23 deletions parinfer-rust-mode.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;;; parinfer-rust-mode.el --- An interface for the parinfer-rust library -*- lexical-binding: t; -*-

;; Copyright (C) 2019-2023 Justin Barclay
;; Copyright (C) 2019-2024 Justin Barclay

;; Author: Justin Barclay <[email protected]>
;; URL: https://github.com/justinbarclay/parinfer-rust-mode
;; Version: 0.8.6
;; Version: 0.8.7
;; Package-Requires: ((emacs "26.1") (track-changes "1.1"))
;; Keywords: lisp tools

Expand Down Expand Up @@ -229,6 +229,22 @@ against and is known to be api compatible.")
:type 'boolean
:group 'parinfer-rust-mode)

(defcustom parinfer-rust-buffer-replace-strategy 'safe
"The strategy to use when replacing the buffer's text.
When set to `safe' the buffer is replaced using the slower but more
fastiduous `replace-buffer-contents'.
When set to `fast' the buffer is replaced using `delete-region'.
For more info on why the default is `replace-buffer-contents', see Info
node `(elisp)Replacing'"
:type '(radio (const :tag "Safe" safe)
(const :tag "Fast" fast))
:group 'parinfer-rust-mode)

(define-obsolete-variable-alias 'parinfer-rust--buffer-replace-strategy 'parinfer-rust-buffer-replace-strategy "0.8.7")

;;;;;;;;;;;;;;;;;;;;;;;;;
;; Setup
;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -269,18 +285,9 @@ against and is known to be api compatible.")
A curated list of pairs consisting of a command and the mode the
command should be run in.")

(defvar parinfer-rust--buffer-replace-strategy 'safe
"The strategy to use when replacing the buffer with the new text.
When set to `'safe' the buffer is replaced using the slower but more fastiduous `replace-buffer-contents'.
When set to `'fast' the buffer is replaced using `delete-region'.
For more info on why the default is `replace-buffer-contents', see Info node `(elisp)Replacing'")

;;;;;;;;;;;;;;;;;;;;;;;;;
;; Local State
;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar-local parinfer-rust-enabled nil "Tracks if parinfer has been enabled.")
(defvar-local parinfer-rust--in-debug nil
"When enabled, outputs the response input and output of the parinfer response to a file.")
(defvar-local parinfer-rust--mode "paren"
Expand Down Expand Up @@ -430,7 +437,7 @@ CHANGES."
(switch-to-buffer new-buf)
(insert replacement-string)
(switch-to-buffer current)
(if (eq parinfer-rust--buffer-replace-strategy
(if (eq parinfer-rust-buffer-replace-strategy
'fast)
(progn
(delete-region (point-min)
Expand Down Expand Up @@ -513,7 +520,6 @@ Checks if MODE is a valid Parinfer mode, and uses

(defun parinfer-rust-mode-setup ()
"Enable Parinfer."
(setq-local parinfer-rust-enabled t)
(parinfer-rust--detect-troublesome-modes)
(parinfer-rust--set-default-state)
(setq-local parinfer-rust--mode parinfer-rust-preferred-mode)
Expand All @@ -537,7 +543,6 @@ Checks if MODE is a valid Parinfer mode, and uses
(when parinfer-rust--change-tracker
(track-changes-unregister parinfer-rust--change-tracker)
(setq-local parinfer-rust--change-tracker nil))
(setq-local parinfer-rust-enabled nil)
(remove-hook 'first-change-hook #'parinfer-rust--check-for-issues t)
(parinfer-rust--dim-parens))

Expand All @@ -553,19 +558,19 @@ This includes stopping tracking of all changes."
(defun parinfer-rust-mode-enable ()
"Enable Parinfer."
;; Make sure the library is installed at the appropriate location or offer to download it
(if (parinfer-rust--check-for-library parinfer-rust-supported-versions
(if (and (parinfer-rust--check-for-library parinfer-rust-supported-versions
parinfer-rust-library
parinfer-rust--lib-name
parinfer-rust-auto-download)
(require 'parinfer-rust parinfer-rust-library t)
(parinfer-rust--check-version parinfer-rust-supported-versions
(parinfer-rust-version)
parinfer-rust-library
parinfer-rust--lib-name))
(progn
(require 'parinfer-rust parinfer-rust-library t)
;; Check version and prompt to download latest version if out of date Problem: Emacs can't
;; reload dynamic libraries, which means that if we download a new library the user has to
;; restart Emacs for changes to take effect.
(parinfer-rust--check-version parinfer-rust-supported-versions
(parinfer-rust-version)
parinfer-rust-library
parinfer-rust--lib-name)
(parinfer-rust-mode-setup)
(cond ((or (eq 'defer parinfer-rust-check-before-enable)
buffer-read-only)
Expand All @@ -580,7 +585,7 @@ This includes stopping tracking of all changes."
(parinfer-rust--execute)))))
(progn
(message "Unable to load library parinfer-rust disabling parinfer-rust-mode")
(parinfer-rust-mode-disable))))
(parinfer-rust-mode -1))))

;;;###autoload
(defun parinfer-rust-switch-mode ()
Expand Down Expand Up @@ -617,15 +622,14 @@ not available."
:init-value nil
:keymap parinfer-rust-mode-map
(cond
(parinfer-rust-enabled ;; FIXME: Why?
((not parinfer-rust-mode)
(parinfer-rust-mode-disable))
;; Don't do anything if the buffer is not selected
;; TODO: Come up with a better way to defer and disable loading
;; Defer waits for window selection change and disabled waits for a change event
;; there is also the idea of deferring the running of parinfer vs deferring the loading
((not (eq (current-buffer)
(window-buffer (selected-window))))
(setq-local parinfer-rust-enabled t)
(add-hook 'window-selection-change-functions #'parinfer-rust--defer-loading nil t))
(t
(parinfer-rust-mode-enable))))
Expand Down
2 changes: 1 addition & 1 deletion test/test-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
:lighter " parinfer"
:init-value nil
:keymap parinfer-rust-mode-map
(if parinfer-rust-enabled
(if parinfer-rust-mode
(parinfer-rust-mode-disable)
(progn
(parinfer-rust--check-version parinfer-rust-supported-versions
Expand Down

0 comments on commit 30d0ea5

Please sign in to comment.