Created
June 13, 2025 09:25
-
-
Save amno1/73dc4e38d18010b86083808201b7d6ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; cua-mini.el --- Override standard bindings with CUA-bindings -*- lexical-binding: t; -*- | |
;; Copyright (C) 2024 Arthur Miller | |
;; Author: Arthur Miller <[email protected]> | |
;; Keywords: | |
;; This program is free software; you can redistribute it and/or modify | |
;; it under the terms of the GNU General Public License as published by | |
;; the Free Software Foundation, either version 3 of the License, or | |
;; (at your option) any later version. | |
;; This program 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 General Public License for more details. | |
;; You should have received a copy of the GNU General Public License | |
;; along with this program. If not, see <https://www.gnu.org/licenses/>. | |
;;; Commentary: | |
;; A small experiment after som discussion on Reddit about "modern" Emacs | |
;; This shadows standard key bindings with a set of CUA-bindings | |
;; | |
;; shadowed key bindings are available on a prefix key by user choice | |
;; | |
;; C-x map is now on C-space, and C-c is remapped to C-e | |
;;; Code: | |
(require 'bind-key) | |
(defgroup cua-mini nil | |
"Enable CUA-style key bindings" | |
:prefix "cua-mini-" | |
:group 'convenience | |
:group 'emulations | |
:group 'editing-basics) | |
(defcustom cua-mini-default-mode 'inherit | |
"Default major mode to use when creating a new buffer. | |
It should either be a cons, (mode-name . file-extension), or a special | |
value \='inherit which means to create a new buffer in the same major mode and | |
with the same file extension (if any) as the current buffer." | |
:type 'symbol | |
:group 'cua-mini) | |
(defun cua-mini-copy () | |
"Copy selection to clipboard." | |
(interactive) | |
(if (region-active-p) | |
(call-interactively #'kill-ring-save) | |
(message "No selection"))) | |
(defun cua-mini-cut () | |
"Delete a selection and copy its content to clipboard." | |
(interactive) | |
(if (region-active-p) | |
(call-interactively #'kill-region) | |
(message "No selection"))) | |
(defun cua-mini-new () | |
"Create a new file in memory." | |
(interactive) | |
(let ((name "New File") | |
mode extension buffer) | |
(if (eq cua-mini-default-mode 'inherit) | |
(setf mode major-mode | |
extension (file-name-extension (buffer-name (current-buffer)))) | |
(setf mode (car cua-mini-default-mode) | |
extension (cdr cua-mini-default-mode))) | |
(when extension | |
(setf name (format "%s.%s" | |
name | |
(substring extension | |
0 (string-match-p "<[0-9]+>" extension))))) | |
(setf buffer (get-buffer-create (generate-new-buffer-name name))) | |
(with-current-buffer buffer (call-interactively mode)) | |
(pop-to-buffer-same-window buffer))) | |
(defun cua-mini-open () | |
"Show systems Open file dialog." | |
(interactive) | |
(let ((use-dialog-box t) | |
(use-file-dialog t) | |
(last-nonmenu-event nil)) | |
(call-interactively #'find-file))) | |
(defun cua-mini-save-as () | |
"Show systems File save-as dialog." | |
(interactive) | |
(let ((use-dialog-box t) | |
(use-file-dialog t) | |
(last-nonmenu-event nil)) | |
(call-interactively #'write-file))) | |
(defvar-local c-c-map (make-sparse-keymap)) | |
(defvar-keymap cua-mini-mode-map | |
:doc "Keymap for CUA-style key bindings" | |
"C-<SPC>" ctl-x-map | |
"C-S-<SPC>" global-map | |
"C-a" #'mark-whole-buffer | |
"C-f" #'isearch-forward | |
"C-n" #'cua-mini-new | |
"C-o" #'cua-mini-open | |
"C-s" #'save-buffer | |
"C-S-s" #'cua-mini-save-as | |
"C-x" #'cua-mini-cut | |
"C-z" #'undo | |
"C-v" #'yank) | |
(defun cua-mini-on () | |
(setf c-c-map (keymap-lookup nil "C-c")) | |
(keymap-set cua-mini-mode-map "C-e" c-c-map) | |
(keymap-set cua-mini-mode-map "C-c" #'cua-mini-copy) | |
(override-global-mode +1)) | |
(defun cua-mini-off () | |
(override-global-mode -1)) | |
(define-minor-mode cua-mini-mode | |
"Enable CUA-style key bindings" | |
:lighter " xcv" :global t | |
(let ((override-global-map cua-mini-mode-map)) | |
(if cua-mini-mode | |
(cua-mini-on) | |
(cua-mini-off)))) | |
(define-globalized-minor-mode global-cua-mini-mode cua-mini-mode | |
(lambda () (cua-mini-mode +1))) | |
(provide 'cua-mini) | |
;;; cua-mini.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment