From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "John Conrad" Newsgroups: gmane.emacs.help Subject: Re: Anyone have a 'move-line' function? Date: Thu, 4 May 2006 12:24:45 -0400 Message-ID: <7f270cb80605040924l5662e85emf5e4c5e4cfe7e48a@mail.gmail.com> 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: quoted-printable X-Trace: sea.gmane.org 1146759925 18503 80.91.229.2 (4 May 2006 16:25:25 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 4 May 2006 16:25:25 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu May 04 18:25:23 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 1Fbgda-000151-BE for geh-help-gnu-emacs@m.gmane.org; Thu, 04 May 2006 18:25:10 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FbgdZ-0007jZ-HG for geh-help-gnu-emacs@m.gmane.org; Thu, 04 May 2006 12:25:09 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1FbgdF-0007b1-ET for help-gnu-emacs@gnu.org; Thu, 04 May 2006 12:24:49 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1FbgdD-0007Z5-Bv for help-gnu-emacs@gnu.org; Thu, 04 May 2006 12:24:48 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FbgdD-0007Yc-6j for help-gnu-emacs@gnu.org; Thu, 04 May 2006 12:24:47 -0400 Original-Received: from [64.233.182.189] (helo=nf-out-0910.google.com) by monty-python.gnu.org with esmtp (Exim 4.52) id 1Fbgdq-00038B-Ny for help-gnu-emacs@gnu.org; Thu, 04 May 2006 12:25:26 -0400 Original-Received: by nf-out-0910.google.com with SMTP id a27so380279nfc for ; Thu, 04 May 2006 09:24:45 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=jU4bAX4Ir4+yoiEanUm9q7zi1/OdOl4JAi5AqlS58cNLAg6ANOSTPsU1Ma1YE3573TjsuBitfQGZzJ2C0JEAdwooH7VUt7t3qzGKKINtiCaqmWvIFKBaAzdgNalV5pvOcT3St91F6iuakYJ5KHi9Pk95VwExj0UZ2NIV+LgG5xc= Original-Received: by 10.48.108.19 with SMTP id g19mr908535nfc; Thu, 04 May 2006 09:24:45 -0700 (PDT) Original-Received: by 10.49.94.7 with HTTP; Thu, 4 May 2006 09:24:45 -0700 (PDT) Original-To: help-gnu-emacs@gnu.org In-Reply-To: Content-Disposition: inline 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:34818 Archived-At: On 5/4/06, Joe Smith wrote: > 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) I don't know if this is the sort of comment you are welcoming, but I think your style would be improved by wrapping the col, start and end variables in a let expression (with the effect of making them local) instead of declaring and setting global variables simultaneously with setq. After you've declared them in the let, you can then use setq on them as needed. In the following variation on your function, I create a local col variable and set it to (current-column), then I create start and end local variables but I do not initialize them with any particular value. Later, I use setq on them the same way as you did in your original function. (defun move-line (n) "Move the current line up or down by N lines." (interactive "p") (let ((col (current-column)) start end) (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)))) John Emerson Conrad