-
-
Notifications
You must be signed in to change notification settings - Fork 251
[WIP] smart-parens-mode #1916
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
base: main
Are you sure you want to change the base?
[WIP] smart-parens-mode #1916
Changes from 8 commits
62f7a9e
3fd0263
820ff72
b0f1888
01b3659
968c48c
486b70b
9fb1a18
d19c72d
f880f12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| * Smart Parens Mode | ||
|
|
||
| ** Future Features | ||
| - When selecting text and adding a opening character, close the pair character at after the text selected |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (defsystem "lem-smart-parens-mode" | ||
| :serial t | ||
| :depends-on ("lem/core") | ||
| :components ((:file "smart-parens-mode"))) |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||
| (defpackage :smart-parens-mode | ||||
| (:use :cl :lem) | ||||
| (:export :smart-parens-mode)) | ||||
|
|
||||
| (in-package :smart-parens-mode) | ||||
|
daninus14 marked this conversation as resolved.
Outdated
|
||||
|
|
||||
| (define-minor-mode smart-parens-mode | ||||
| (:name "smart-parens-mode" | ||||
| :keymap *smart-parens-keymap*)) | ||||
|
|
||||
| ;;; From paredit | ||||
| (defun bolp (point) | ||||
| (zerop (point-charpos point))) | ||||
|
daninus14 marked this conversation as resolved.
Outdated
|
||||
|
|
||||
| (defun eolp (point) | ||||
|
daninus14 marked this conversation as resolved.
Outdated
|
||||
| (let ((len (length (line-string point)))) | ||||
| (or (zerop len) | ||||
| (>= (point-charpos point) | ||||
| (1- len))))) | ||||
|
|
||||
| (defun integer-char-p (char) | ||||
| (< (char-code #\0) (char-code char) (char-code #\9))) | ||||
|
|
||||
| (defun sharp-literal-p (char point) | ||||
| (with-point ((p point)) | ||||
| (character-offset p -1) | ||||
| (and (character-at p) | ||||
| (char-equal (character-at p) char) | ||||
| (eql (character-at p -1) #\#)))) | ||||
|
|
||||
| (defun sharp-n-literal-p (char point) | ||||
| (with-point ((p point)) | ||||
| (character-offset p -1) | ||||
| (when (char-equal char (character-at p)) | ||||
| (character-offset p -1) | ||||
| (skip-chars-backward p #'integer-char-p) | ||||
| (and (integer-char-p (character-at p)) | ||||
| (eql (character-at p -1) #\#))))) | ||||
|
|
||||
| (defparameter *non-space-following-chars* | ||||
| '(#\Space #\( #\' #\` #\, #\[ #\{)) | ||||
|
|
||||
| (defparameter *non-space-preceding-chars* | ||||
| '(#\Space #\) #\] #\})) | ||||
|
|
||||
| (defun non-space-following-context-p (&optional (p (current-point))) | ||||
| (or (bolp p) | ||||
| (find (character-at p -1) | ||||
| *non-space-following-chars*) | ||||
| (eql (character-at p -1) #\#) | ||||
| (and (eql (character-at p -1) #\@) | ||||
| (eql (character-at p -2) #\,)) | ||||
| (sharp-literal-p #\' p) | ||||
| (sharp-literal-p #\. p) | ||||
| (sharp-literal-p #\S p) | ||||
| (sharp-literal-p #\C p) | ||||
| (sharp-literal-p #\+ p) | ||||
| (sharp-literal-p #\- p) | ||||
| (sharp-n-literal-p #\A p) | ||||
| (sharp-n-literal-p #\= p))) | ||||
|
|
||||
| (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) | ||||
|
Comment on lines
+62
to
+93
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. for example
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, do you mean that instead of using (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 I don't know lem well enough. What function or macro would I pass this
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lem/extensions/c-mode/c-mode.lisp Line 13 in 4072439
syntax-table is defined here in C, for example.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example, like this |
||||
Uh oh!
There was an error while loading. Please reload this page.