Skip to content

Instantly share code, notes, and snippets.

@fzerorubigd
Created June 22, 2012 19:35
Show Gist options
  • Save fzerorubigd/2974656 to your computer and use it in GitHub Desktop.
Save fzerorubigd/2974656 to your computer and use it in GitHub Desktop.
;; Disable automatic cutting and pasting to the clipboard.
;; This causes noticeable delays over slow network links.
;; The function `insert-clipboard-contents' and the function
;; `set-clipboard-contents' let me do this explicitly.
(setq interprogram-cut-function nil)
(setq interprogram-paste-function nil)
(defun get-clipboard-contents-as-string ()
"Return the value of the clipboard contents as a string."
(let ((x-select-enable-clipboard t))
(or (x-cut-buffer-or-selection-value)
x-last-selected-text-clipboard)))
(defun insert-clipboard-contents ()
"Insert the value of the current X selection at point.
Uses the clipboard value if it is defined or not empty, otherwise
falls back on the primary selection."
(interactive)
(let ((text (get-clipboard-contents-as-string)))
(when text
;; This operation is very much like a yank, so set mark like
;; yank does. Note that the "longlines" mode advice on this
;; function depends on mark having been set.
(push-mark)
(insert text))))
(defun set-clipboard-contents-from-string (str)
"Copy the value of string STR into the clipboard."
(let ((x-select-enable-clipboard t))
(x-select-text str)))
(defun set-clipboard-contents (beg end)
"Copy the value of the current region into the clipboard."
(interactive "r")
(set-clipboard-contents-from-string
(buffer-substring-no-properties beg end))
(setq deactivate-mark t))
(defun set-clipboard-contents-delete (beg end)
"Cut the value of the current region into the clipboard.
The current region is deleted (without updating the kill ring)."
(interactive "r")
(set-clipboard-contents-from-string
(buffer-substring-no-properties beg end))
(delete-region beg end)
(setq deactivate-mark t))
(defun set-clipboard-contents-and-kill-ring-from-string (str)
"Copy the value of string STR into the clipboard, and make it the latest kill."
(set-clipboard-contents-from-string str)
(kill-new str)
(message "%s" str))
(eval-after-load "longlines"
'(progn
(defadvice insert-clipboard-contents (after longlines-decode-kill activate)
;; Depends on insert-clipboard-contents having set mark at the
;; begging of the text. If we have to stop doing that, we
;; should turn this into "around" advice that can capture point
;; before doing the insert.
(when longlines-mode
(longlines-decode-region (point) (mark t))
(when longlines-showing
(longlines-show-hard-newlines))))
(defadvice set-clipboard-contents (around longlines-encode-kill activate)
(if longlines-mode
(let ((str (buffer-substring beg end)))
(with-temp-buffer
(insert str)
(longlines-encode-region (point-min) (point-max))
(setq beg (point-min))
(setq end (point-max))
ad-do-it))
ad-do-it))
(defadvice set-clipboard-contents-delete (before longlines-encode-kill activate)
(when longlines-mode
(longlines-encode-region beg end)))))
(defun path-to-clipboard ()
"Copy the current file's path to the clipboard.
If the current buffer has no file, copy the buffer's default directory."
(interactive)
(let ((path (expand-file-name (or (buffer-file-name) default-directory))))
(set-clipboard-contents-from-string path)
(message "%s" path)))
(defun npath-to-clipboard ()
"Copy the current file's path to the clipboard, with a network filename.
The resulting value will have the correct syntax to use with SCP.
If the current buffer has no file, copy the buffer's default directory."
(interactive)
(let* ((host (system-name))
(path (expand-file-name (or (buffer-file-name) default-directory)))
(network-path (concat host ":" path)))
(set-clipboard-contents-from-string network-path)
(message "%s" network-path)))
;; Turn Control-z into another keymap. The previous binding becomes
;; `Control-z Control-z'. This frees up some easy-to-reach
;; keybindings for my own use.
(defvar ctrl-z-map (make-sparse-keymap))
(let ((orig-ctrl-z-binding (lookup-key (current-global-map) [(control ?z)])))
(global-set-key [(control ?z)] ctrl-z-map)
(global-set-key [(control ?z) (control ?z)] orig-ctrl-z-binding))
(global-set-key [(control ?z) (control ?g)] 'keyboard-quit)
;; Control-Z has been converted to be a new keymap, like Control-X.
;; This is a backup for when the mouse's rocker switch isn't working
;; (such as in Exceed).
(global-set-key [(control ?z) (control ?x)] 'set-clipboard-contents-delete)
(global-set-key [(control ?z) (control ?c)] 'set-clipboard-contents)
(global-set-key [(control ?z) (control ?v)] 'insert-clipboard-contents)
;; Buttons 6 and 7 on my mouse are a rocker switch; forward is 6,
;; backward is 7. When I push forward, I'm "pushing the clipboard
;; contents into Emacs". When I pull backward, I'm "pulling from
;; Emacs into the clipboard".
(global-set-key [(mouse-6)] 'insert-clipboard-contents)
(global-set-key [(mouse-7)] 'set-clipboard-contents)
(setq
backup-by-copying t ; don't clobber symlinks
backup-directory-alist
'(("." . "~/.saves")) ; don't litter my fs tree
delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t) ; use versioned backups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment