From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joe Smith Newsgroups: gmane.emacs.help Subject: Re: Anyone have a 'move-line' function? Date: Thu, 04 May 2006 11:29:32 -0400 Message-ID: References: <87d5ewj931.fsf@tallis.ilo.ucl.ac.uk> <1146690224.061525.281590@j73g2000cwa.googlegroups.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1146756742 6283 80.91.229.2 (4 May 2006 15:32:22 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 4 May 2006 15:32:22 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu May 04 17:32:18 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FbfnJ-0006o8-0m for geh-help-gnu-emacs@m.gmane.org; Thu, 04 May 2006 17:31:09 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FbfnI-0007NV-FV for geh-help-gnu-emacs@m.gmane.org; Thu, 04 May 2006 11:31:08 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Fbfmk-0007Gz-7x for help-gnu-emacs@gnu.org; Thu, 04 May 2006 11:30:34 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Fbfmi-0007EA-DV for help-gnu-emacs@gnu.org; Thu, 04 May 2006 11:30:33 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Fbfmi-0007Dh-1N for help-gnu-emacs@gnu.org; Thu, 04 May 2006 11:30:32 -0400 Original-Received: from [80.91.229.2] (helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1FbfnL-00063C-3G for help-gnu-emacs@gnu.org; Thu, 04 May 2006 11:31:11 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1FbfmI-0006bK-Hw for help-gnu-emacs@gnu.org; Thu, 04 May 2006 17:30:08 +0200 Original-Received: from 01-136.200.popsite.net ([66.19.241.136]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 May 2006 17:30:06 +0200 Original-Received: from jes by 01-136.200.popsite.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 04 May 2006 17:30:06 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-To: help-gnu-emacs@gnu.org Original-Lines: 44 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 01-136.200.popsite.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.2) Gecko/20060405 SeaMonkey/1.0.1 In-Reply-To: <1146690224.061525.281590@j73g2000cwa.googlegroups.com> X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:34817 Archived-At: Mathias Dahl wrote: > The following is quite ugly but seems to work. ... liyer.vijay@gmail.com wrote: > Here's a solution that doesn't add to the kill-ring ... Very nice. Thanks guys. I like the idea of using current-column (new to me anyway) and not adding to the kill-ring. Here's what I came up with--after studying your suggestions and adapting what I had; Vijay's is simpler but it goes a little weird at the file edges: moving the last line down adds empty lines; moving the first line up gives an error and leaves point on line 2. Thanks again. Comments/corrections welcome. (defun move-line (n) "Move the current line up or down by N lines." (interactive "p") (setq col (current-column)) (beginning-of-line) (setq start (point)) (end-of-line) (forward-char) (setq end (point)) (let ((line-text (delete-and-extract-region start end))) (forward-line n) (insert line-text) ;; restore point to original column in moved line (forward-line -1) (forward-char col))) (defun move-line-up (n) "Move the current line up by N lines." (interactive "p") (move-line (if (null n) -1 (- n)))) (defun move-line-down (n) "Move the current line down by N lines." (interactive "p") (move-line (if (null n) 1 n))) (global-set-key (kbd "M-") 'move-line-up) (global-set-key (kbd "M-") 'move-line-down)