Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[WIP] smart-parens-mode#1916

Draft
daninus14 wants to merge 10 commits intolem-project:main from
daninus14:main
Draft

[WIP] smart-parens-mode #1916
daninus14 wants to merge 10 commits intolem-project:main from
daninus14:main

Conversation

@daninus14
Copy link

@daninus14 daninus14 commented Aug 13, 2025

not currently working, this is just a draft

Copy link
Collaborator

vindarel commented Aug 13, 2025
edited
Loading

(following Discord:) I pull the branch, start Lem, start the mode with M-x smart-parens-mode and typing a ( inserts the pair. It works! (except it inserts too many closing parens)

Copy link
Author

Hm. smart-parens-mode is not showing up for me. What is your workflow? I was doing a make build after each change in the code. Are you running it from slime? How do I run it locally without building?

Copy link
Member

cxxxr commented Aug 13, 2025

I tried it, and make was successful.
The feature in this mode are also working.

Copy link
Member

cxxxr commented Aug 13, 2025

Screencast.from.2025年08月14日.01-39-12.webm

Copy link
Member

@cxxxr cxxxr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to contribute to lem.
I have made a few comments.

daninus14 reacted with thumbs up emoji
Comment on lines 62 to 99
(defun editor-insert-pair (open close)
(let ((p (current-point)))
(cond ((in-string-or-comment-p p)
(insert-character p open))
((syntax-escape-point-p p 0)
(insert-character p open))
(t
(unless (non-space-following-context-p p)
(insert-character p #\Space))
(insert-character p open)
(insert-character p close)
(unless (or (eolp p) (find (character-at p) *non-space-preceding-chars*))
(insert-character p #\Space)
(character-offset p -1))
(character-offset p -1)))))

;;; smart parens specific code

(define-command smart-parens-insert-paren () ()
(editor-insert-pair #\( #\)))

(define-command smart-parens-insert-bracket () ()
(editor-insert-pair #\[ #\]))

(define-command smart-parens-insert-brace () ()
(editor-insert-pair #\{ #\}))

(define-command smart-parens-insert-double-quote () ()
(editor-insert-pair #\" #\"))

(define-command smart-parens-insert-single-quote () ()
(editor-insert-pair #\' #\'))

(define-key *smart-parens-keymap* "\"" 'smart-parens-insert-double-quote)
(define-key *smart-parens-keymap* "'" 'smart-parens-insert-single-quote)
(define-key *smart-parens-keymap* "(" 'smart-parens-insert-paren)
(define-key *smart-parens-keymap* "[" 'smart-parens-insert-bracket)
(define-key *smart-parens-keymap* "{" 'smart-parens-insert-brace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with this implementation is that it implicitly relies on C-like syntax.
It would be better to refer to the syntax-table for each mode.

for example

CL-USER> (lem:syntax-open-paren-char-p #\()
(#\( . #\))
CL-USER> (lem:syntax-open-paren-char-p #\[)
(#\[ . #\])
CL-USER> (lem:syntax-open-paren-char-p #\{)
(#\{ . #\})

Copy link
Author

@daninus14 daninus14 Aug 13, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, do you mean that instead of using define-key I should use some function which gets the currently inserted character like this:

(defun close-paren (given-char)
 (when (lem:syntax-open-paren-char-p given-char)
 (apply #'editor-insert-pair (lem:syntax-open-paren-char-p given-char))))

That seems like it would only work for the parens and not the " and ' quote characters.

I don't know lem well enough. What function or macro would I pass this close-paren function to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:paren-pairs '((#\( . #\))

syntax-table is defined here in C, for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example, like this

(defmethod execute ((mode smart-parens-mode) (command self-insert) argument)
 (let ((c (get-self-insert-char)))
 (alexandria:when-let (pair (syntax-open-paren-char-p c))
 (editor-insert-pair (car pair) (cdr pair))
 (return-from execute))
 (when (syntax-string-quote-char-p c)
 (editor-insert-pair c c)
 (return-from execute))
 (call-next-method)))

Copy link
Author

Ok, I realize I doubt I'll ever get to change this code. This is a stylistic issue for an optional mode, where the code works. It's based on code of existing modes. While I understand it's better to have your desired style in the code base, considering this is not even part of the main code base and it's an additional mode, following the style of other modes, I think it's reasonable to ask to accept the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@cxxxr cxxxr cxxxr left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /