Ostap Bender
;; () (defconst wpers-overloaded-funs [next-line previous-line right-char move-end-of-line] "Functions overloaded by the mode") ;; ("") ;; ("") (defconst wpers-fun-prefix "wpers-" "Prefix for new functions") ;; - "" "" (defconst wpers-funs-alist (mapcar '(lambda (x) (cons x (intern (concat wpers-fun-prefix (symbol-name x))))) wpers-overloaded-funs) "alist (old . new) functions") ;; Key-map - (wpers-overloaded-funs) (defconst wpers-mode-map (reduce '(lambda (sx) (define-key s (vector 'remap (car x)) (cdr x)) s) wpers-funs-alist :initial-value (make-sparse-keymap)) "Mode map for `wpers'") ;; - - () (defconst wreps-hooks-alist '((pre-command-hook . wpers--pre-command-hook) (auto-save-hook . wpers-kill-final-spaces) (before-save-hook . wpers-kill-final-spaces)) "alist (hook-var . hook-function)")
(define-minor-mode wpers-mode "Toggle persistent cursor mode." :init-value nil :lighter " wpers" :group 'wpers :keymap wpers-mode-map (if wpers-mode (progn (message "Wpers enabled") (mapc '(lambda (x) (add-hook (car x) (cdr x) nil t)) wreps-hooks-alist)) ; (progn (message "Wpers disabled") (wpers-kill-final-spaces) (mapc '(lambda (x) (remove-hook (car x) (cdr x) t)) wreps-hooks-alist)))) ;
;; (form) () (defmacro wpers-save-vpos (form) "Eval form with saving current cursor's position in the line (column)" (let ((old-col (make-symbol "old-col"))) `(let ((,old-col (current-column)) last-col) ,form (move-to-column ,old-col t)))) ;;; / (defun wpers-next-line () "Same as `new-line' but adds the spaces if it's needed for saving cursor's position in the line (column)" (interactive) (wpers-save-vpos (next-line))) (defun wpers-previous-line () "Same as `previous-line' but adds the spaces if it's needed for saving cursor's position in the line (column)" (interactive) (wpers-save-vpos (previous-line))) ;; , " " (defun wpers-right-char () "Same as `right-char' but adds the spaces if cursor at end of line (column)" (interactive) (let ((ca (char-after))) (if (or (null ca) (eq ca 10)) (insert 32) (right-char)))) ;; (defun wpers-move-end-of-line () "Function `move-end-of-line' is called and then removes all trailing spaces" (interactive) (move-end-of-line nil) (while (eq (char-before) 32) (delete-char -1))) ;; (defun wpers-kill-final-spaces () "Deleting all trailing spaces for all lines in the buffer" (save-excursion (goto-char (point-min)) (while (search-forward-regexp " +$" nil t) (replace-match "")))) ;; ( ) read-only, visual-line . (defun wpers--pre-command-hook () "Disabling functionality when buffer is read only, visual-line-mode is non-nil or marking is active" (if (or buffer-read-only this-command-keys-shift-translated mark-active visual-line-mode) (let ((fn-pair (rassoc this-command wpers-funs-alist))) (when fn-pair (setq this-command (car fn-pair))))))
Source: https://habr.com/ru/post/265531/
All Articles