unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to write the "interactive" form for a command acting on a region
@ 2015-01-13 22:05 Marcin Borkowski
  2015-01-13 23:24 ` Nicolas Richard
  0 siblings, 1 reply; 7+ messages in thread
From: Marcin Borkowski @ 2015-01-13 22:05 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

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.



^ permalink raw reply	[flat|nested] 7+ messages in thread
[parent not found: <mailman.17860.1421186716.1147.help-gnu-emacs@gnu.org>]

end of thread, other threads:[~2015-01-14  3:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-13 22:05 How to write the "interactive" form for a command acting on a region Marcin Borkowski
2015-01-13 23:24 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).