Skip to content

Commit

Permalink
Fix #514: Unconfuse sly-package-fu--create-new-import-from
Browse files Browse the repository at this point in the history
This function was trying to go again to the relevant DEFPACKAGE
definition, except that point IS already at such a place.  Apparently,
sly-current-package returns nil in some situations and so this
needless repetition was actually harmful.

Charles Jackson <[email protected]>, discovered this
problem and put forth a fix.  This is a simplified version of that
fix.

* contrib/sly-package-fu.el
(sly-package-fu--create-new-import-from): Don't go to source definition again.
(sly-package-fu--add-or-update-import-from-form): Clean up comments.
(sly-package-fu--search-import-from): Remove spurious newline.
  • Loading branch information
joaotavora committed Jun 3, 2022
1 parent f411da2 commit b501b43
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions contrib/sly-package-fu.el
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,15 @@ symbol in the Lisp image if possible."
;;

(defun sly-package-fu--search-import-from (package)

(let* ((normalized-package (sly-package-fu--normalize-name package))
(regexp (format "(:import-from[ \t']*\\(:\\|#:\\)?%s"
(regexp-quote (regexp-quote normalized-package)))))
(re-search-forward regexp nil t)))


(defun sly-package-fu--create-new-import-from (package symbol)
(sly-goto-package-source-definition (sly-current-package))
"Add new :IMPORT-FROM subform for PACKAGE. Add SYMBOL.
Assumes point just before start of DEFPACKAGE form"
(forward-sexp)
;; Now, search last :import-from or :use form
(cond
Expand Down Expand Up @@ -391,7 +391,7 @@ package. For example above, return \"with-gensyms\"."
symbol)))
(simple-symbol (sly-cl-symbol-name symbol)))
(save-excursion
;; First go to package definition form
;; First go to just before relevant DEFPACKAGE form
;;
(sly-goto-package-source-definition (sly-current-package))

Expand All @@ -402,17 +402,18 @@ package. For example above, return \"with-gensyms\"."
,(sly-current-package)
,package))
(if (sly-package-fu--search-import-from package)
;; If the (:import-from PACKAGE... ) subform exists, attempt
;; to insert SYMBOL (qualified as simple-symbol) there.
;; If specific (:IMPORT-FROM PACKAGE... ) subform exists,
;; attempt to insert package-less SYMBOL there.
(let ((imported-symbols (mapcar #'sly-package-fu--normalize-name
(sly-package-fu--read-symbols))))
(unless (cl-member simple-symbol
imported-symbols
:test 'cl-equalp)
(sly-package-fu--insert-symbol simple-symbol)
(when sly-package-fu-save-file (save-buffer))))
;; Else add a new :import-from PACKAGE subform, after the last
;; existing :import-from or :use subform.
;; Else, point is unmoved. Add a new (:IMPORT-FROM PACKAGE)
;; subform after any other existing :IMPORT-FROM or :USE
;; subforms.
(sly-package-fu--create-new-import-from package
simple-symbol)
(when sly-package-fu-save-file (save-buffer)))
Expand Down

0 comments on commit b501b43

Please sign in to comment.