<commit_before>(in-package :lem)

(export '(*set-location-hook*
          exit-lem
          quick-exit
          keyboard-quit
          universal-argument
          self-insert
          unmark-buffer
          *read-only-function*
          toggle-read-only
          rename-buffer
          quoted-insert
          newline
          open-line
          delete-next-char
          delete-previous-char
          copy-region
          kill-region
          kill-line
          yank
          yank-pop
          next-line
          prev-line
          next-char
          prev-char
          move-to-beginning-of-buffer
          move-to-end-of-buffer
          move-to-beginning-of-line
          move-to-end-of-line
          next-page
          prev-page
          entab-line
          detab-line
          next-page-char
          prev-page-char
          delete-blank-lines
          just-one-space
          delete-indentation
          transpose-characters
          back-to-indentation-command
          undo
          redo
          mark-set
          exchange-point-mark
          goto-line
          filter-buffer
          pipe-command
          delete-trailing-whitespace))

(defvar *set-location-hook* '())

(defun delete-character-with-killring (point n killp)
  (let ((string (delete-character point n)))
    (when (and killp string) (kill-push string))
    t))

(define-key *global-keymap* "C-x C-c" 'exit-lem)
(define-command exit-lem () ()
  (when (or (not (any-modified-buffer-p))
            (prompt-for-y-or-n-p "Modified buffers exist. Leave anyway"))
    (exit-editor)))

(define-command quick-exit () ()
  (save-some-buffers t)
  (exit-editor))

(define-key *global-keymap* "C-g" 'keyboard-quit)
(define-command keyboard-quit () ()
  (error 'editor-abort))

(define-key *global-keymap* "C-u" 'universal-argument)
(define-command universal-argument () ()
  (let ((numlist)
        n)
    (do ((c (prompt-for-character "C-u 4")
            (prompt-for-character
             (format nil "C-u ~{~a~}" numlist))))
        (nil)
      (cond
        ((char= c (keyname->keychar "C-u"))
         (setq numlist
               (mapcar 'digit-char-p
                       (coerce
                        (format nil "~a"
                                (* 4
                                   (if numlist
                                       (parse-integer
                                        (format nil "~{~a~}" numlist))
                                       4)))
                        'list))))
        ((and (char= c #\-) (null numlist))
         (setq numlist (append numlist (list #\-))))
        ((setq n (digit-char-p c))
         (setq numlist
               (append numlist (list n))))
        (t
         (unread-key c)
         (let ((arg (if numlist
                        (parse-integer (format nil "~{~a~}" numlist))
                        4)))
           (return (call-command (read-command) arg))))))))

(define-command self-insert (n) ("p")
  (let ((c (insertion-key-p (last-read-key-sequence))))
    (cond (c
           (insert-character (current-point) c n))
          (t
           (undefined-key)))))

(define-key *global-keymap* "M-~" 'unmark-buffer)
(define-command unmark-buffer () ()
  (buffer-unmark (current-buffer))
  t)

(defvar *read-only-function* nil)

(define-key *global-keymap* "C-x C-q" 'toggle-read-only)
(define-command toggle-read-only () ()
  (setf (buffer-read-only-p (current-buffer))
        (not (buffer-read-only-p (current-buffer))))
  (when *read-only-function*
    (funcall *read-only-function*
             (buffer-read-only-p (current-buffer))))
  t)

(define-command rename-buffer (name) ("sRename buffer: ")
  (buffer-rename (current-buffer) name)
  t)

(define-key *global-keymap* "C-q" 'quoted-insert)
(define-command quoted-insert (&optional (n 1)) ("p")
  (let ((c (read-key)))
    (dotimes (_ n t)
      (insert-character (current-point) c 1))))

(define-key *global-keymap* "C-m" 'newline)
(define-command newline (&optional (n 1)) ("p")
  (insert-character (current-point) #\newline n))

(define-key *global-keymap* "C-o" 'open-line)
(define-command open-line (n) ("p")
  (let ((point (current-point)))
    (insert-character (current-point) #\newline n)
    (character-offset point (- n))))

(define-key *global-keymap* "C-d" 'delete-next-char)
(define-key *global-keymap* "[dc]" 'delete-next-char)
(define-command delete-next-char (&optional n) ("P")
  (when (end-buffer-p (current-point))
    (editor-error "End of buffer"))
  (when n
    (unless (continue-flag :kill)
      (kill-ring-new)))
  (delete-character-with-killring (current-point)
                                  (or n 1)
                                  (if n t nil)))

(define-key *global-keymap* "C-h" 'delete-previous-char)
(define-key *global-keymap* "[backspace]" 'delete-previous-char)
(define-key *global-keymap* "[del]" 'delete-previous-char)
(define-command delete-previous-char (&optional n) ("P")
  (prev-char (or n 1))
  (delete-next-char n))

(define-key *global-keymap* "M-w" 'copy-region)
(define-command copy-region (start end) ("r")
  (unless (continue-flag :kill)
    (kill-ring-new))
  (kill-push (points-to-string start end))
  (buffer-mark-cancel (current-buffer))
  t)

(define-key *global-keymap* "C-w" 'kill-region)
(define-command kill-region (start end) ("r")
  (when (point< end start)
    (rotatef start end))<commit_msg>improve delete-previous-char<commit_after>(in-package :lem)

(export '(*set-location-hook*
          exit-lem
          quick-exit
          keyboard-quit
          universal-argument
          self-insert
          unmark-buffer
          *read-only-function*
          toggle-read-only
          rename-buffer
          quoted-insert
          newline
          open-line
          delete-next-char
          delete-previous-char
          copy-region
          kill-region
          kill-line
          yank
          yank-pop
          next-line
          prev-line
          next-char
          prev-char
          move-to-beginning-of-buffer
          move-to-end-of-buffer
          move-to-beginning-of-line
          move-to-end-of-line
          next-page
          prev-page
          entab-line
          detab-line
          next-page-char
          prev-page-char
          delete-blank-lines
          just-one-space
          delete-indentation
          transpose-characters
          back-to-indentation-command
          undo
          redo
          mark-set
          exchange-point-mark
          goto-line
          filter-buffer
          pipe-command
          delete-trailing-whitespace))

(defvar *set-location-hook* '())

(defun delete-character-with-killring (point n killp)
  (let ((string (delete-character point n)))
    (when (and killp string) (kill-push string))
    t))

(define-key *global-keymap* "C-x C-c" 'exit-lem)
(define-command exit-lem () ()
  (when (or (not (any-modified-buffer-p))
            (prompt-for-y-or-n-p "Modified buffers exist. Leave anyway"))
    (exit-editor)))

(define-command quick-exit () ()
  (save-some-buffers t)
  (exit-editor))

(define-key *global-keymap* "C-g" 'keyboard-quit)
(define-command keyboard-quit () ()
  (error 'editor-abort))

(define-key *global-keymap* "C-u" 'universal-argument)
(define-command universal-argument () ()
  (let ((numlist)
        n)
    (do ((c (prompt-for-character "C-u 4")
            (prompt-for-character
             (format nil "C-u ~{~a~}" numlist))))
        (nil)
      (cond
        ((char= c (keyname->keychar "C-u"))
         (setq numlist
               (mapcar 'digit-char-p
                       (coerce
                        (format nil "~a"
                                (* 4
                                   (if numlist
                                       (parse-integer
                                        (format nil "~{~a~}" numlist))
                                       4)))
                        'list))))
        ((and (char= c #\-) (null numlist))
         (setq numlist (append numlist (list #\-))))
        ((setq n (digit-char-p c))
         (setq numlist
               (append numlist (list n))))
        (t
         (unread-key c)
         (let ((arg (if numlist
                        (parse-integer (format nil "~{~a~}" numlist))
                        4)))
           (return (call-command (read-command) arg))))))))

(define-command self-insert (n) ("p")
  (let ((c (insertion-key-p (last-read-key-sequence))))
    (cond (c
           (insert-character (current-point) c n))
          (t
           (undefined-key)))))

(define-key *global-keymap* "M-~" 'unmark-buffer)
(define-command unmark-buffer () ()
  (buffer-unmark (current-buffer))
  t)

(defvar *read-only-function* nil)

(define-key *global-keymap* "C-x C-q" 'toggle-read-only)
(define-command toggle-read-only () ()
  (setf (buffer-read-only-p (current-buffer))
        (not (buffer-read-only-p (current-buffer))))
  (when *read-only-function*
    (funcall *read-only-function*
             (buffer-read-only-p (current-buffer))))
  t)

(define-command rename-buffer (name) ("sRename buffer: ")
  (buffer-rename (current-buffer) name)
  t)

(define-key *global-keymap* "C-q" 'quoted-insert)
(define-command quoted-insert (&optional (n 1)) ("p")
  (let ((c (read-key)))
    (dotimes (_ n t)
      (insert-character (current-point) c 1))))

(define-key *global-keymap* "C-m" 'newline)
(define-command newline (&optional (n 1)) ("p")
  (insert-character (current-point) #\newline n))

(define-key *global-keymap* "C-o" 'open-line)
(define-command open-line (n) ("p")
  (let ((point (current-point)))
    (insert-character (current-point) #\newline n)
    (character-offset point (- n))))

(define-key *global-keymap* "C-d" 'delete-next-char)
(define-key *global-keymap* "[dc]" 'delete-next-char)
(define-command delete-next-char (&optional n) ("P")
  (when (end-buffer-p (current-point))
    (editor-error "End of buffer"))
  (when n
    (unless (continue-flag :kill)
      (kill-ring-new)))
  (delete-character-with-killring (current-point)
                                  (or n 1)
                                  (if n t nil)))


(define-key *global-keymap* "C-h" 'delete-previous-char)
(define-key *global-keymap* "[backspace]" 'delete-previous-char)
(define-key *global-keymap* "[del]" 'delete-previous-char)
(define-command delete-previous-char (&optional n) ("P")
  (if (buffer-mark (current-buffer))
      (let ((start (region-beginning))
            (end (region-end)))
        (delete-character start (count-characters start end)))
      (progn
        (prev-char (or n 1))
        (delete-next-char n))))

(define-key *global-keymap* "M-w" 'copy-region)
(define-command copy-region (start end) ("r")
  (unless (continue-flag :kill)
    (kill-ring-new))
  (kill-push (points-to-string start end))
  (buffer-mark-cancel (current-buffer))
  t)

(define-key *global-keymap* "C-w" 'kill-region)
(define-command kill-region (start end) ("r")
  (when (point< end start)
    (rotatef start end))
{'fraction_non_alphanumeric': 0.1552693633230546, 'fraction_numerical': 0.0016325049882096863, 'mean_word_length': 1.8889179984280848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4853674', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3553, 'n_words': 782}