all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@wmi.amu.edu.pl>
To: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: How to write the "interactive" form for a command acting on a region
Date: Tue, 13 Jan 2015 23:05:01 +0100	[thread overview]
Message-ID: <8761ca5oc2.fsf@wmi.amu.edu.pl> (raw)

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.



             reply	other threads:[~2015-01-13 22:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 22:05 Marcin Borkowski [this message]
2015-01-13 23:24 ` How to write the "interactive" form for a command acting on a region Nicolas Richard
2015-01-13 23:35   ` Marcin Borkowski
     [not found] <mailman.17860.1421186716.1147.help-gnu-emacs@gnu.org>
2015-01-13 22:38 ` Pascal J. Bourguignon
2015-01-13 23:06   ` Marcin Borkowski
     [not found]   ` <mailman.17861.1421190402.1147.help-gnu-emacs@gnu.org>
2015-01-14  2:04     ` Pascal J. Bourguignon
2015-01-14  3:49       ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8761ca5oc2.fsf@wmi.amu.edu.pl \
    --to=mbork@wmi.amu.edu.pl \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.