Skip to content

Instantly share code, notes, and snippets.

@sudish
Created May 18, 2012 14:06
Show Gist options
  • Select an option

  • Save sudish/2725449 to your computer and use it in GitHub Desktop.

Select an option

Save sudish/2725449 to your computer and use it in GitHub Desktop.
Emacs mac-port customizations
;; Useful helper function
(defun sj/copy-keys-from-keymap (from-map keys &optional to-map)
"Copy the definitions of key sequences in `keys' from `from-map' to `to-map'.
A new keymap is created if `to-map' is nil. `keys' should be a
list of the keys whose bindings are to be copied. Each entry may
also be of the form (from-key . to-key) if the keys differ in the
two keymaps.
Example:
(\"a\" [backspace]
(\"v\" . \"k\")
([?v] . [?\C-o])
(\"\C-y\" . \"x\"))
The keymap will have `from-map's bindings for \"v\" on \"k\" and \"\C-o\",
and the binding for \"\C-y\" on \"x\". The bindings for \"a\" and [backspace]
will be copied as well."
(let ((new-map (or to-map (make-sparse-keymap))))
(dolist (entry keys)
(let ((from-key (if (listp entry) (car entry) entry))
(to-key (if (listp entry) (cdr entry) entry)))
(define-key new-map to-key (lookup-key from-map from-key))))
new-map))
;; Distinguish between various Emacs ports to OS X
(cond
;; ns port
((boundp 'ns-version-string)
(setq ns-antialias-text t
ns-option-modifier 'meta)
(define-key global-map [ns-drag-file] 'ns-find-file))
;; mac port
((boundp 'mac-carbon-version-string)
(setq mac-command-modifier 'super
mac-option-modifier 'meta)
;; Command-S to save, C to copy, V to paste, etc.
(let ((keys '(("\C-x\C-s" . [(super s)])
("\C-w" . [(super x)])
("\M-w" . [(super c)])
("\C-y" . [(super v)])
([(control /)] . [(super z)]))))
(sj/copy-keys-from-keymap global-map keys global-map))))
;; Fullscreen hacks for the two primary versions: ns- and mac-port
;; The latter supports fullscreen natively (both old-school and Lion
;; style).
;; Emacs HEAD now has support for auto-hiding the OS X menubar.
;; Combine this with moving the frame decoration offscreen and
;; we have fullscreen mode under OS X.
;; (see e7f047d6f3e1ea53c8469c28279c2c284fd4d655)
(when (boundp 'ns-auto-hide-menu-bar)
(define-key global-map [(super S-return)] 'sj/ns-toggle-menu-bar)
(defun sj/ns-toggle-menu-bar ()
"Toggle the auto-hide of the menu bar."
(interactive)
(setq ns-auto-hide-menu-bar (not ns-auto-hide-menu-bar)))
(define-key global-map [(super return)] 'sj/ns-make-frame-fullscreen)
(defun sj/ns-make-frame-fullscreen ()
"Make the current frame fullscreen."
(interactive)
(setq ns-auto-hide-menu-bar t)
(labels ((fp (p) (frame-parameter (selected-frame) p)))
;; Make this frame large enough to cover the whole screen.
;; set-frame-size takes character rows and columns, so convert
;; all the pixel-based values accordingly.
(let* ((rows (/ (display-pixel-height) (frame-char-height)))
(cols (/ (display-pixel-width) (frame-char-width)))
(fringe-pixels (+ (fp 'left-fringe) (fp 'right-fringe)))
(fringe (/ fringe-pixels (frame-char-width)))
(scrollbar (/ (fp 'scroll-bar-width) (frame-char-width)))
(real-cols (- cols fringe scrollbar)))
(set-frame-size (selected-frame) real-cols rows))
;; Move this frame's window decoration offscreen.
;; - The magic number here is the size of the decorator in pixels.
;; - vertical-gap is any leftover vertical space (pixels) after
;; computing the number of rows above. We distribute this evenly
;; at the top and bottom.
;; - vertical-offset must be negative to move the window decoration
;; offscreen.
(let* ((decorator-size 24)
(vertical-gap (mod (display-pixel-height) (frame-char-height)))
(vertical-offset (- (/ vertical-gap 2) decorator-size)))
(set-frame-position (selected-frame) 0 vertical-offset)))))
(when (boundp 'mac-carbon-version-string)
(defun sj/mac-carbon-toggle-frame-fullscreen ()
"Toggle fullscreen mode for current frame."
(interactive)
(let* ((frame (selected-frame))
(fs-param (if (eq (frame-parameter frame 'fullscreen) 'fullboth)
nil
'fullboth)))
(set-frame-parameter frame 'fullscreen fs-param)))
(define-key global-map [(super return)] 'sj/mac-carbon-toggle-frame-fullscreen))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment