<commit_before>#|
link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
|#

(defpackage :lem-paredit-mode
  (:use :cl
        :lem
        :lem-vi-mode.word)
  (:export :paredit-mode))
(in-package :lem-paredit-mode)

(define-minor-mode paredit-mode
    (:name "paredit"
     :keymap *paredit-mode-keymap*))

(defun move-to-forward-open-paren (p)
  (loop while (and (not (syntax-open-paren-char-p (character-at p)))
                   (/= (point-charpos p) 0))
        do (character-offset p -1)))

(defun move-to-word-end (q)
  (loop while (not (syntax-space-char-p (character-at q)))
        do (character-offset q 1)))

(defun move-to-closed-paren (p)
  (scan-lists p 1 0))

(defun backward-open-paren-char-p (p)
  (with-point ((q p))
    (skip-whitespace-backward q)
    (syntax-open-paren-char-p (character-at q))))

(defun yank-with-space (yank-point)
  (unless (backward-open-paren-char-p yank-point)
    (move-point (current-point) yank-point))
  (insert-character yank-point #\space)
  (yank))

(defun delete-backward-whitespace (point-end)
  (with-point ((point-start point-end))
    (skip-whitespace-backward point-start)
    (delete-between-points point-start point-end)))

(define-command paredit-insert-paren () ()
  (let ((p (current-point)))
    (dolist (c '(#\( #\)))
      (insert-character p c))
    (character-offset p -1)))

(define-command paredit-backward-delete (&optional (n 1)) ("p")
  (when (< 0 n)
    (with-point ((p (current-point)))
      (character-offset p -1)
      (case (character-at p)
        (#\( (when (char= (character-at p 1) #\))
               (delete-next-char)
               (delete-previous-char)))
        (#\) (backward-char))
        (otherwise
         (delete-previous-char))))
    (paredit-backward-delete (1- n))))

(define-command paredit-slurp () ()
  (with-point ((origin (current-point))
               (p (current-point)))
    (move-to-forward-open-paren p)
    (when (syntax-open-paren-char-p (character-at p))
      (move-to-closed-paren p)
      (character-offset p -1)
      (with-point ((yank-point p))
        (character-offset p 1)
        (skip-whitespace-forward p)
        (when (not (syntax-closed-paren-char-p (character-at p)))
          (with-point ((kill-end p))
            (if (syntax-open-paren-char-p (character-at p))
                (scan-lists kill-end 1 0)
                (move-to-word-end kill-end))
            (kill-ring-new)
            (kill-region p kill-end)
            (delete-backward-whitespace p))
          (yank-with-space yank-point))))
    (move-point (current-point) origin)))

(define-command paredit-barf () ()
  (with-point ((origin (current-point))
               (p (current-point)))
    (move-to-forward-open-paren p)
    (when (syntax-open-paren-char-p (character-at p))
      (move-to-closed-paren p)
        (character-offset p -1)
        (with-point ((kill-begin p))
          (character-offset p -1)
          (if (syntax-closed-paren-char-p (character-at p))
              (scan-lists kill-begin -1 0)
              (backward-word-begin kill-begin 1 t))
          (character-offset p 1)
          (kill-ring-new)
          (kill-region kill-begin p)
          (with-point ((yank-point kill-begin))
            (skip-whitespace-backward yank-point)
            (delete-backward-whitespace kill-begin)
            (character-offset yank-point 1)
            (yank-with-space yank-point))))
    (move-point (current-point) origin)))

(loop for (k . f) in '(("(" . paredit-insert-paren)
                       ("Backspace" . paredit-backward-delete)
                       ("C-Right" . paredit-slurp)
                       ("C-Left" . paredit-barf))
      do (define-key *paredit-mode-keymap* k f))<commit_msg>Add ')' to paredit-mode.<commit_after>#|
link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
|#

(defpackage :lem-paredit-mode
  (:use :cl
        :lem
        :lem-vi-mode.word)
  (:export :paredit-mode))
(in-package :lem-paredit-mode)

(define-minor-mode paredit-mode
    (:name "paredit"
     :keymap *paredit-mode-keymap*))

(defun move-to-forward-open-paren (p)
  (loop while (and (not (syntax-open-paren-char-p (character-at p)))
                   (/= (point-charpos p) 0))
        do (character-offset p -1)))

(defun move-to-word-end (q)
  (loop while (not (syntax-space-char-p (character-at q)))
        do (character-offset q 1)))

(defun move-to-closed-paren (p)
  (scan-lists p 1 0))

(defun backward-open-paren-char-p (p)
  (with-point ((q p))
    (skip-whitespace-backward q)
    (syntax-open-paren-char-p (character-at q))))

(defun yank-with-space (yank-point)
  (unless (backward-open-paren-char-p yank-point)
    (move-point (current-point) yank-point))
  (insert-character yank-point #\space)
  (yank))

(defun delete-backward-whitespace (point-end)
  (with-point ((point-start point-end))
    (skip-whitespace-backward point-start)
    (delete-between-points point-start point-end)))

(define-command paredit-insert-paren () ()
  (let ((p (current-point)))
    (dolist (c '(#\( #\)))
      (insert-character p c))
    (character-offset p -1)))

(define-command paredit-backward-delete (&optional (n 1)) ("p")
  (when (< 0 n)
    (with-point ((p (current-point)))
      (character-offset p -1)
      (case (character-at p)
        (#\( (when (char= (character-at p 1) #\))
               (delete-next-char)
               (delete-previous-char)))
        (#\) (backward-char))
        (otherwise
         (delete-previous-char))))
    (paredit-backward-delete (1- n))))

(define-command paredit-close-parenthesis () ()
  (with-point ((p (current-point)))
    (case (character-at p)
      (#\) (forward-char))
      (otherwise
       (message "aaa")
       (handler-case (scan-lists p 1 1)
         (error ()
           (insert-character p #\))
           (return-from paredit-close-parenthesis)))
       (message "bbb")
       (with-point ((new-p p))
         (message "~A" new-p)
         (character-offset new-p -1)
         (move-point (current-point) new-p)
         (with-point ((p new-p))
           (skip-whitespace-backward p)
           (delete-between-points p new-p)))))))

(define-command paredit-slurp () ()
  (with-point ((origin (current-point))
               (p (current-point)))
    (move-to-forward-open-paren p)
    (when (syntax-open-paren-char-p (character-at p))
      (move-to-closed-paren p)
      (character-offset p -1)
      (with-point ((yank-point p))
        (character-offset p 1)
        (skip-whitespace-forward p)
        (when (not (syntax-closed-paren-char-p (character-at p)))
          (with-point ((kill-end p))
            (if (syntax-open-paren-char-p (character-at p))
                (scan-lists kill-end 1 0)
                (move-to-word-end kill-end))
            (kill-ring-new)
            (kill-region p kill-end)
            (delete-backward-whitespace p))
          (yank-with-space yank-point))))
    (move-point (current-point) origin)))

(define-command paredit-barf () ()
  (with-point ((origin (current-point))
               (p (current-point)))
    (move-to-forward-open-paren p)
    (when (syntax-open-paren-char-p (character-at p))
      (move-to-closed-paren p)
        (character-offset p -1)
        (with-point ((kill-begin p))
          (character-offset p -1)
          (if (syntax-closed-paren-char-p (character-at p))
              (scan-lists kill-begin -1 0)
              (backward-word-begin kill-begin 1 t))
          (character-offset p 1)
          (kill-ring-new)
          (kill-region kill-begin p)
          (with-point ((yank-point kill-begin))
            (skip-whitespace-backward yank-point)
            (delete-backward-whitespace kill-begin)
            (character-offset yank-point 1)
            (yank-with-space yank-point))))
    (move-point (current-point) origin)))

(loop for (k . f) in '(("(" . paredit-insert-paren)
                       (")" . paredit-close-parenthesis)
                       ("Backspace" . paredit-backward-delete)
                       ("C-Right" . paredit-slurp)
                       ("C-Left" . paredit-barf))
      do (define-key *paredit-mode-keymap* k f))
{'fraction_non_alphanumeric': 0.16904001964154186, 'fraction_numerical': 0.005769702921679352, 'mean_word_length': 2.1394990366088633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804076', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2866, 'n_words': 576}