Download
(require 'misc)
(defalias 'copy-prev-line 'copy-from-above-command)
(defun copy-one-from-prev-line (&optional arg)
(interactive "P")
(copy-prev-line (or arg 1)))
(defun copy-one-from-next-line (&optional arg)
(interactive "P")
(copy-next-line (or arg 1)))
(defun copy-sexp-from-prev-line (&optional arg)
(interactive "P")
(unless (> (location-of (previous-line)
(forward-sexp))
(line-end-position 0))
(let ((w (save-excursion (previous-line)
(thing-at-point 'word))))
(if w (insert w)
(insert (save-excursion
(previous-line)
(buffer-substring (point)
(location-of (forward-sexp)))))))))
(defun copy-sexp-from-next-line (&optional arg)
(interactive "P")
(unless (> (location-of (next-line)
(forward-sexp))
(line-end-position 2))
(let ((w (save-excursion (next-line)
(thing-at-point 'word))))
(if w (insert w)
(insert (save-excursion
(next-line)
(buffer-substring (point)
(location-of (forward-sexp)))))))))
(defun ph/copy-thing-from-next-line (&optional arg)
(interactive "P")
(if (eq last-command 'ph/copy-thing-from-next-line)
(progn
(undo)
(copy-next-line arg))
(copy-sexp-from-next-line arg)))
(defun ph/copy-thing-from-prev-line (&optional arg)
(interactive "P")
(if (eq last-command 'ph/copy-thing-from-prev-line)
(progn
(undo)
(copy-prev-line arg))
(copy-sexp-from-prev-line arg)))
(defun copy-next-line (&optional arg)
"Copy characters from next nonblank line, starting just above point.
Copy ARG characters, but not past the end of that line.
If no argument given, copy the entire rest of the line.
The characters copied are inserted in the buffer before point."
(interactive "P")
(let ((cc (current-column))
n (string ""))
(save-excursion
(end-of-line)
(forward-char 1)
(skip-chars-forward "\ \t\n")
(move-to-column cc)
(setq n (if arg (prefix-numeric-value arg) (point-max)))
(if (< cc (current-column))
(if (= (preceding-char) ?\t)
(progn
(setq string (make-string (min n (- (current-column) cc)) ?\s))
(setq n (- n (min n (- (current-column) cc)))))
(backward-char 1)))
(setq string (concat string
(buffer-substring
(point)
(min (save-excursion (end-of-line) (point))
(+ n (point)))))))
(insert string)))
(provide 'copy-from-surrounding-lines)