;;; ycp-editing.el -- My config for editing -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My config for editing.

;;; Code:


;; line wrap at window edge
(setq-default truncate-lines nil)
(setq kill-do-not-save-duplicates t)
(setq kill-transform-function
      (lambda (s) (when (string-match-p "[^ \t\n]" s) s)))
(setq bidi-inhibit-bpa t)
(setq save-interprogram-paste-before-kill t)
(setq kill-ring-max 200)
(setq sentence-end-double-space nil)
(setq show-paren-delay 0)
(setq window-divider-default-bottom-width 1)
(setq line-number-display-limit-width 9999)
(setq window-divider-default-places 'bottom-only)
;; If run in gui, don't interpret C-m as RET
(when (display-graphic-p)
  (define-key input-decode-map [?\C-m] [C-m])
  (define-key input-decode-map [?\C-i] [C-i]))
;; fixme: the line below does not work
;; (define-key input-decode-map [?\C-M-m] [C-M-m])
(setq save-place-file (locate-user-emacs-file "saveplace"))

(my-configure
  (setq visual-line-fringe-indicators
        '(left-curly-arrow right-curly-arrow))
  ;; Keep the binding of C-a, C-e, and C-k
  (my-keybind visual-line-mode-map
    "C-a" nil
    "C-e" nil
    "C-k" nil)
  ;; We need to override them still because org-kill-line hardcodes
  ;; call to `kill-visual-line' etc. when `visual-line-mode' is on.
  (advice-add 'kill-visual-line :override 'kill-line)
  (advice-add 'beginning-of-visual-line :override 'beginning-of-line)
  (advice-add 'end-of-visual-line :override 'end-of-line)
  (setq delete-pair-blink-delay 0)
  )

(my-package my-editing
  (:delay 5)
  (my-override delete-pair)
  (my-keybind global-map
    "C-k" #'my-kill-line
    "M-k" #'my-kill-line-backward
    "M-w" #'my-copy-line-or-region
    "C-o" #'my-new-line-above-or-below
    "C-<" #'my-escape-url-dwim
    "M-'" #'my-insert-pair
    "M-\\" #'my-delete-pair-dwim
    "M-Z" #'my-zap-back-to-char
    "C-x C-t" #'my-transpose-lines
    "M-`" #'my-buffer-create-scratch
    "C-M-;" #'my-comment-and-copy-selection
    "C-c C-;" #'my-elide-region
    "M-Q" #'my-unfill-paragraph
    "C-x M-s" #'my-save-without-formatting
    "C-x w" #'my-copy-url-at-point
    "C-<backspace>" #'my-backward-kill-path-component
    "C-w" #'my-kill-region-if-active
    "C-c r <SPC>" #'my-replace-leading-space
    "C-c r <RET>" #'my-concat-lines
    "C-M-y" #'my-yank-primary
    "C-a" #'my-beginning-of-line-or-indentation
    "M-c" #'my-copy-buffer-file-name    ; override capitalize
    "M-o" #'delete-blank-lines          ; alias for C-x C-o
    "M-SPC" #'cycle-spacing
    "M-z" #'zap-up-to-char              ; NOT `zap-to-char'
    "<C-M-backspace>" #'backward-kill-sexp
    "C-M-/" #'my-mark-backward-up-list
    "C-M-k" #'my-kill-sexp-or-comment
    "C-x C-w" #'my-write-file
    )
  (electric-pair-mode)
  (my-add-hooks #'my-non-special-modes-setup '(text-mode-hook prog-mode-hook))
  (advice-add 'find-file :around 'my-find-file-line-number)
  (advice-add 'find-file-other-window :around 'my-find-file-line-number)
  )

(my-package newcomment
  (add-to-list 'safe-local-variable-values
               '((comment-style . extra-line)
                 (comment-continue . "  "))))

(setq viper-mode nil)
(my-package viper
  (:delay 60)
  (setq viper-syntax-preference 'extended))

(define-key global-map [f2] 'revert-buffer)

(my-package aggressive-indent
  (:install t)
  (:delay 15)
  (my-add-hooks #'aggressive-indent-mode '(emacs-lisp-mode-hook lisp-mode-hook)))

(my-package avy
  (:install t)
  (:delay 15)
  (my-keybind global-map
    "C-." #'avy-goto-char-timer
    "M-g M-g" #'avy-goto-line)
  (setq avy-keys '(97 115 100 102 103 104 106 107 108)))

(my-package ispell
  ;; Use aspell:
  ;; https://battlepenguin.com/tech/aspell-and-hunspell-a-tale-of-two-spell-checkers/
  ;; also, ispell seems to have problem finding hunspell aff files
  ;; using `ispell-find-hunspell-dictionaries', even though the files
  ;; are available.
  (setq ispell-program-name "aspell"
        ispell-dictionary "en_GB")
  )

(my-package flyspell
  (my-keybind flyspell-mode-map
    "C-." nil
    ))

(my-package pyim
  (:delay 30)
  (:install t))

;;;; Auto revert mode
(setq auto-revert-verbose t)
(global-auto-revert-mode 1)

;;;; Delete selection
(delete-selection-mode 1)

;;;; Tabs, indentation, and the TAB key
(setq-default tab-always-indent 'complete
              tab-first-completion 'word-or-paren-or-punct ; Emacs 27
              tab-width 2
              indent-tabs-mode nil)

(define-key global-map [f12] 'display-line-numbers-mode)

;; show column number
(column-number-mode t)

(define-key global-map (kbd "C-x F") 'my-sudo-find-file)

;; find file
(my-package ffap
  (ffap-bindings)
  ;; Stop ffap from pinging things that look like domain names
  (setq ffap-machine-p-known 'accept))

(put 'narrow-to-region 'disabled nil)

(setq large-file-warning-threshold nil)
(setq x-stretch-cursor t)
(setq delete-old-versions t)
(setq version-control t)

(add-hook 'text-mode-hook #'my-text-modes-setup)

(add-to-list
 'auto-mode-alist
 '("\\(README\\|CHANGELOG\\|COPYING\\|LICENSE\\)\\'" . text-mode))
;; https://lists.gnu.org/archive/html/emacs-devel/2023-06/msg00113.html
;; https://lists.gnu.org/archive/html/bug-gnu-emacs/2023-11/msg01149.html
(unless (string< emacs-version "29")
  (setq long-line-threshold nil))

(save-place-mode 1)

(provide 'ycp-editing)