Friday, April 11, 2014

Mac and Windows friendly setup

 Since all people in our laboratory refuse to use Emacs, I decided to make Emacs behave like other applications on Windows or Mac.

(global-set-key (kbd "s-x") 'kill-region)
(global-set-key (kbd "s-c") 'kill-ring-save)
(global-set-key (kbd "s-v") 'yank)
(global-set-key (kbd "s-z") 'undo)
(global-set-key (kbd "s-a") 'mark-whole-buffer)
(global-set-key (kbd "s-w") 'close-frame-or-kill-buffer)
(global-set-key (kbd "s-n") 'new-frame-with-new-buffer)
 
(defun new-frame-with-new-buffer ()
  "Create a new frame with a new buffer `untitled-n'."
  (interactive)
  (let* ((n-th 1)
         (dir (format-time-string "~/deleteme.d/%Y-%m" (current-time)))
         (file)
         (region-p (region-active-p)))
    (if region-p (kill-ring-save (region-beginning) (region-end)))
    (switch-to-buffer-other-frame "*scratch*")
    (make-directory dir t)
    (while (file-exists-p (setq file (format "%s/untitled-%d" dir n-th)))
      (setq n-th (+ n-th 1)))
    (find-file file)
    (emacs-lisp-mode) ;; to use (lispxmp)
    (turn-on-orgtbl)
    (when region-p
      (end-of-buffer)
      (yank))
    (write-file file))
  (when (fboundp 'w32-send-sys-command)
    (w32-send-sys-command #xf030)
    (w32-send-sys-command #xf120)))
(defvar no-warning-close-frame-or-kill-buffer nil)
(defun close-frame-or-kill-buffer ()
  "Close a frame if there are more than two frames.  If there is
only one frame, kill selected-buffer.  To avoid closing
frame/buffer by mistype of M-w, prompt for an action for the
first time."
  (interactive)
  (let* ((close-buffer-p (= 1 (length (visible-frame-list))))
        (name-obj (if close-buffer-p "buffer" "frame")))
    (if (or no-warning-close-frame-or-kill-buffer
            (setq no-warning-close-frame-or-kill-buffer
                  (yes-or-no-p (format "Are you sure you want to close a %s?" name-obj))))
        (if close-buffer-p (kill-this-buffer) (delete-frame)))))

No comments: