help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using called-interactively-p


From: Emanuel Berg
Subject: Re: Using called-interactively-p
Date: Wed, 05 Jul 2023 19:52:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]