From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Using called-interactively-p
Date: Wed, 05 Jul 2023 19:52:33 +0200 [thread overview]
Message-ID: <875y6yp7oe.fsf@dataswamp.org> (raw)
In-Reply-To: 87edlmjs81.fsf@mbork.pl
Marcin Borkowski wrote:
>> The documentation suggests to use "called-interactively-p"
>> instead of (interactive-p) [..]
>
> I would definitely bet on symbol. Using strings for things
> like this seems highly unusual for me (in Elisp, as opposed
> to e.g. JavaScript).
>
> OTOH, you can check if you want to (do-this) or (do-that)
> using a simple trick with an optional parameter -- see
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html
It is better to not have functions do different things based
on how it is called, that should only affect the interface.
Please see yanked file:
;;; -*- lexical-binding: t -*-
;;
;; this file:
;; https://dataswamp.org/~incal/emacs-init/dwim.el
;;
;; DWIM code helpers and examples.
;;
;; Advantages to this style:
;;
;; - the same default interactively and from Lisp
;; - the default is the whole buffer
;; - the region is never used from Lisp
;; - the variables are always set, to the default if not explicitely
;; - one can still have preceding, non-optional arguments
(defun use-region (&optional both)
(if (use-region-p)
(list (region-beginning) (region-end))
(when both
(list nil nil) )))
(defun test-dwim (&optional beg end)
(interactive (use-region))
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(message "%d %d" beg end) )
(defun test-dwim-2 (re &optional beg end)
(interactive `(,(read-regexp "regexp: ") ,@(use-region)))
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(message "%s %d %d" re beg end) )
(provide 'dwim)
--
underground experts united
https://dataswamp.org/~incal
prev parent reply other threads:[~2023-07-05 17:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-05 13:35 Using called-interactively-p uzibalqa
2023-07-05 15:25 ` Marcin Borkowski
2023-07-05 15:40 ` uzibalqa
2023-07-05 20:26 ` Emanuel Berg
2023-07-06 18:55 ` Marcin Borkowski
2023-07-06 21:36 ` Emanuel Berg
2023-07-05 17:52 ` Emanuel Berg [this message]
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=875y6yp7oe.fsf@dataswamp.org \
--to=incal@dataswamp.org \
--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.