(defun poll-mac-bindings () (let (fn q) (setq fn #'(lambda (ch) (setq ch (string ch)) (let ((kd (key-sequence-list-description (read-key-sequence (concat "Alt-" ch))))) (and (= (length kd) 1) (setq kd (elt kd 0))) (and (vectorp kd) (setq kd (vector->list (map 'list 'identity kd)))) (push (cons (intern ch) kd) q)))) (dotimes (i 26) (funcall fn (+ ?a i))) (dolist (i '(?, ?. ?/ ?< ?> ??)) (funcall fn i)) (reverse q))) (defun transfer-alt-bindings-to-mac-keys (&optional allow-overwrite) (interactive) (dolist (syms (car '((;; (poll-mac-bindings) on an MacBook Pro (a aring) (b integral) (c ccedilla) (d partialderivative) (e dead-acute) (f function) (g copyright) (h abovedot) (i dead-circumflex) (j unknown-keysym-0x1002206) (k unknown-keysym-0x10002DA) (l notsign) (m mu) (n dead-tilde) (o oslash) (p Greek_pi) (q oe) (r registered) (s ssharp) (t dagger) (u dead-diaeresis) (v radical) (w unknown-keysym-0x1002211) (x approxeq) (y yen) (z Greek_OMEGA) (\, lessthanequal) (\. greaterthanequal) (/ division) (< macron) (> breve) (\? questiondown) ) (;; (poll-mac-bindings) on an iBook G4 (a dead-macron) (b dead-breve) (c dead-cedilla) (d eth) (e dead-acute) (f function) (g copyright) (h unknown-keysym-0x10002CD) (i unknown-keysym-0x10002BC) (j dead-doubleacute) (k dead-abovering) ;;(l ?-) (m dead-ogonek) (n dead-tilde) (o oslash) ;;(p ?\,) (q oe) (r registered) (s ssharp) (t thorn) (u dead-diaeresis) (v dead-caron) (w dead-abovedot) ;;(x ?\.) (y yen) (z unknown-keysym-0x10002C0) (\, lessthanequal) (\. greaterthanequal) (/ division) (< doublelowquotemark) (> unknown-keysym-0x1000294) (\? questiondown) ) ))) (let* ((usym (if (listp (car syms)) (car syms) (list (car syms)))) (asym (cadr syms)) (b1 (or (lookup-key global-map `[(alt . ,usym)]) (lookup-key global-map `[(meta . ,usym)]))) (b2 (lookup-key global-map `[,asym]))) (when (or (eq b2 'self-insert-command) (eq b2 'universal-argument) ;Alt-u !? (eq b2 b1)) (setq b2 nil)) (when (and b2 (not (equal b1 b2))) (let ((msg (format "Non-trivial binding for %s %s => %s" asym b1 b2))) (if allow-overwrite (princ (concat msg "\n")) (error msg)))) (when (and b1 (not b2)) (define-key global-map `[,asym] b1)) ))) ;; Mac-like bindings: (defun mac-keys-enable () (interactive) (mac-keys-disable t)) (defun mac-keys-disable (&optional enable) (interactive "P") (dolist (x '( ((meta c) capitalize-region-or-word copy-primary-selection) ((meta l) downcase-region-or-word goto-line) ((meta u) upcase-region-or-word "Use C-x C-c C-u instead of M-u.") ;;((meta s) nil save-buffer) ((meta v) scroll-down-command yank-clipboard-selection) ;;((meta a) backward-sentence mark-whole-buffer) ((control r) isearch-backward scroll-down-command) ((control s) isearch-forward backward-char-command) ((meta r) recenter query-replace) ((meta f) forward-word isearch-forward) ((meta F) forward-word isearch-backward) ((meta g) goto-line isearch-forward-again) ((meta G) goto-line isearch-backward-again) ((meta w) kill-ring-save "Use mouse to close window.") ;;((meta x) execute-extended-command kill-primary-selection) ((meta j) indent-new-comment-line comint-find-source-code) ((meta z) zap-to-char advertised-undo) )) (let ((b (cond ((not enable) (cadr x)) ;turned off; use standard binding ((not (stringp (caddr x))) (caddr x)) ;turn on, never transitional ((and (eq enable t) (cdddr x)) (cadddr x)) ;not transitional (t `(lambda () (interactive) (error ,(caddr x)))) ;make transitional binding ))) (when b (define-key global-map `[,(car x)] b)))) (define-key global-map [(meta \])] 'next-error) (define-key global-map [(meta \[)] 'previous-error) (define-key global-map [(meta s)] 'save-buffer) ; was undefined (define-key global-map [(meta o)] 'find-file) ; was undefined (define-key global-map [(meta X)] 'kill-primary-selection) ; was undefined (define-key global-map [(meta e)] 'isearch-forward-with-selection) ; forward-sentence ;; M-x is still execute-extended-command (define-key isearch-mode-map [(meta w)] 'isearch-yank-word) (define-key isearch-mode-map [(meta y)] 'isearch-yank-line) (define-key isearch-mode-map [(meta k)] 'isearch-yank-sexp) (define-key isearch-mode-map [(meta f)] 'isearch-repeat-forward) (define-key isearch-mode-map [(meta g)] 'isearch-repeat-forward) (define-key isearch-mode-map [(meta F)] 'isearch-repeat-backward) (define-key isearch-mode-map [(meta G)] 'isearch-repeat-backward) (define-key isearch-mode-map [(meta e)] 'isearch-yank-selection) (define-key isearch-mode-map [(meta r)] 'isearch-toggle-regexp) (define-key isearch-mode-map [(control return)] 'isearch-edit-string) ;; local bindings which interfere: (define-key text-mode-map [(meta s)] nil) (require 'dired) (define-key dired-mode-map [(meta c)] nil) ;; new access to case change commands: (define-key global-map [(control x) (control c) (control u)] 'upcase-region-or-word) (define-key global-map [(control x) (control c) (control l)] 'downcase-region-or-word) (define-key global-map [(control x) (control c) (control c)] 'capitalize-region-or-word) (define-key global-map [(control x) (control c) (control z)] 'zap-to-char) (define-key global-map [(control x) (control c) (control s)] 'center-line) (transfer-alt-bindings-to-mac-keys) ) ;; Often: ;; Command-F for forward-word (=> Control-right) ;; Command-B for backward-word (=> Control-left) ;; Command-X for execute-extended-command (=> ?) ;; Command-V for scroll-up (=> Control-R) ;; Command-W for copy-primary-selection (=> Command-C) ;; Command-C for capitalize-region-or-word (=> C-X C-C C-C) ;; Command-U for upcase-region-or-word (=> C-X C-C C-U) ;; ;; Sometimes: ;; Command-G for goto-line (=> Command-L) ;; Command-Q for fill-paragraph-or-region (keep as is) ;; Command-L for downcase-region-or-word (=> C-X C-C C-U) ;; ;; Never: ;; Command-J for indent-new-comment-line (=> C-M-J) ;; Command-R for move-to-window-line (=> none) ;; Command-A for backward-sentence (=> none) ;; Command-S for center-line (=> C-X C-C C-S) ;; Command-Z for zap-to-char (=> C-X C-C C-Z) ;; Command-Shift-E for end-of-defun (defun isearch-with-command (forward-p command) (if isearch-recursive-edit (funcall command) (isearch-mode forward-p) (funcall command))) (defun isearch-forward-again () (interactive) (isearch-with-command t 'isearch-repeat-forward)) (defun isearch-backward-again () (interactive) (isearch-with-command nil 'isearch-repeat-backward)) (defun isearch-forward-with-selection () (interactive) (unless (selection-exists-p) (mark-word)) (isearch-with-command t 'isearch-yank-selection)) (put 'isearch-forward-again 'isearch-command t) (put 'isearch-backward-again 'isearch-command t) (put 'isearch-forward-with-selection 'isearch-command t)