From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: How to write the "interactive" form for a command acting on a region Date: Tue, 13 Jan 2015 23:05:01 +0100 Message-ID: <8761ca5oc2.fsf@wmi.amu.edu.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1421186745 23116 80.91.229.3 (13 Jan 2015 22:05:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Jan 2015 22:05:45 +0000 (UTC) To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jan 13 23:05:41 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YB9ak-0002HR-GD for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 23:05:38 +0100 Original-Received: from localhost ([::1]:41643 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB9aj-0005yF-RU for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 17:05:37 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34847) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB9aM-0005tK-9m for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 17:05:15 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YB9aI-0006DT-Ur for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 17:05:14 -0500 Original-Received: from msg.wmi.amu.edu.pl ([2001:808:114:2::50]:52280) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB9aI-0006Cn-O9 for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 17:05:10 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by msg.wmi.amu.edu.pl (Postfix) with ESMTP id 7D0CC47F01 for ; Tue, 13 Jan 2015 23:05:08 +0100 (CET) Original-Received: from msg.wmi.amu.edu.pl ([127.0.0.1]) by localhost (msg.wmi.amu.edu.pl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IM1hf+6z1L11 for ; Tue, 13 Jan 2015 23:05:08 +0100 (CET) Original-Received: from localhost (117-116.echostar.pl [213.156.117.116]) by msg.wmi.amu.edu.pl (Postfix) with ESMTPSA id 35CFD42061 for ; Tue, 13 Jan 2015 23:05:08 +0100 (CET) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:808:114:2::50 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:102054 Archived-At: Hi all, so I want to have a function which should do something on the region. If no region is active, I want it to act on the whole buffer. If called from Lisp code, I want to be able to supply "begin" and/or "end" parameters, which (if nil) should default to (point-min) and (point-max). Finally, I want my command to behave differently depending on whether it was called interactively or programmatically. I did some RTFMing, and after a few iterations I came up with this: (defun my-function (&optional begin end print-message) "Do something clever on region or buffer." (interactive (if (use-region-p) (list (region-beginning) (region-end) t) (list (point-min) (point-max) t))) (save-excursion (save-restriction (narrow-to-region (or begin (point-min)) (or end (point-max))) (let ((result)) (ding) ; do something clever here (if print-message (message "Result: %s." result) result))))) I'm wondering whether it can be made better? Regards, -- Marcin Borkowski This email was proudly sent http://mbork.pl from my Emacs.