Emacs: copy things at point without marking
(defun get-point (symbol &optional arg) “get the point” (funcall symbol arg) (point) )
(defun copy-thing (begin-of-thing end-of-thing &optional arg) “Copy thing between beg & end into kill ring. Remove leading andtrailing whitespace while we’re at it. Also, remove whitespace beforecolumn, if any. Also, font-lock will be removed, if any. Also, thecopied region will be highlighted shortly (it ‘blinks’)." (save-excursion (let* ((beg (get-point begin-of-thing 1)) (end (get-point end-of-thing arg))) (progn (copy-region-as-kill beg end) (with-temp-buffer (yank) (goto-char 1) (while (looking-at ”[ \t\n\r]") (delete-char 1)) (delete-trailing-whitespace) (delete-whitespace-rectangle (point-min) (point-max)) ;; del column \s, hehe (font-lock-unfontify-buffer) ;; reset font lock (kill-region (point-min) (point-max)) ) ))))
(defun copy-word (&optional arg) “Copy word at point into kill-ring” (interactive “P”) (my-blink (get-point ‘backward-word 1) (get-point ‘forward-word 1)) (copy-thing ‘backward-word ‘forward-word arg) (message “word at point copied”))
(defun copy-line (&optional arg) “Copy line at point into kill-ring, truncated” (interactive “P”) (my-blink (get-point ‘beginning-of-line 1) (get-point ’end-of-line 1)) (copy-thing ‘beginning-of-line ’end-of-line arg) (message “line at point copied”))
(defun copy-paragraph (&optional arg) “Copy paragraph at point into kill-ring, truncated” (interactive “P”) (my-blink (get-point ‘backward-paragraph 1) (get-point ‘forward-paragraph 1)) (copy-thing ‘backward-paragraph ‘forward-paragraph arg) (message “paragraph at point copied”))
(defun copy-buffer(&optional arg) “Copy the whole buffer into kill-ring, as-is” (interactive “P”) (progn (my-blink (point-min) (point-max)) (copy-region-as-kill (point-min) (point-max)) (message “buffer copied”)))
;; “speaking” bindings CTRL-[c]opy [w]ord, etc… (global-set-key (kbd “C-c w”) ‘copy-word)(global-set-key (kbd “C-c l”) ‘copy-line) (global-set-key (kbd “C-c p”) ‘copy-paragraph) (global-set-key (kbd “C-c a”) ‘copy-buffer)Update 2017-02-12:Sometimes during programming in emacs I need to copy a whole function. If a function contains empty lines, copy-paragraph is insuffcient. So, I added copy-defun, which copies the whole function (at point, that is, point must be inside the function). This works with almost all programming modes, as long as it implements beginning-of-defun and end-of-defun.
<pre>
(defun copy-defun (&optional arg) “Copy function at point into kill-ring” (interactive “P”) (my-blink (get-point ‘beginning-of-defun) (get-point ’end-of-defun)) (kill-ring-save (get-point ‘beginning-of-defun) (get-point ’end-of-defun)) (message “function at point copied”))
(global-set-key (kbd “C-c f”) ‘copy-defun)