1

I would like to call a certain command from Emacs Lisp. This function takes two arguments plus a third optional argument. This third argument, when non-nil, doesn't ask for confirmation. I would like the programmatic call to behave just like the interactive call, except that I also want to bypass the confirmation prompt, by passing t as the third optional argument. How can I do this? I read the interactive call section of the Emacs Lisp manual but I was unable to find an answer to my question.

For concreteness, here is the command in question (from the BBDB package):

(defun bbdb-delete-field-or-record (records field &optional noprompt)
  "For RECORDS delete FIELD.
If FIELD is the `name' field, delete RECORDS from datanbase.
Interactively, use BBDB prefix \
\\<bbdb-mode-map>\\[bbdb-do-all-records], see `bbdb-do-all-records',
and FIELD is the field point is on.
If prefix NOPROMPT is non-nil, do not confirm deletion."
  ;; The value of FIELD is whatever `bbdb-current-field' returns.
  ;; This way we can identify more accurately what really needs
  ;; to be done.
  (interactive
   (list (bbdb-do-records) (bbdb-current-field) current-prefix-arg))
  (bbdb-editable)
  (unless field (user-error "Not a field"))
  (setq records (bbdb-record-list records))
  (let* ((type (car field))
         (type-x (if (eq type 'xfields)
                     (car (nth 1 field))
                   type)))
    (if (eq type 'name)
        (bbdb-delete-records records noprompt)
      (if (memq type '(firstname lastname))
          (user-error "Cannot delete field `%s'" type))
      (dolist (record records)
        (when (or noprompt
                  (y-or-n-p (format "delete this `%s' field (of %s)? "
                                    type-x (bbdb-record-name record))))
          (cond ((memq type '(phone address))
                 (bbdb-record-set-field
                  record type
                  ;; We use `delete' which deletes all phone and address
                  ;; fields equal to the current one.  This works for
                  ;; multiple records.
                  (delete (nth 1 field)
                          (bbdb-record-field record type))))
                ((memq type '(affix organization mail aka))
                 (bbdb-record-set-field record type nil))
                ((eq type 'xfields)
                 (bbdb-record-set-xfield record type-x nil))
                (t (error "Unknown field %s" type)))
          (bbdb-change-record record))))))
1

1 Answer 1

0

See call-interactively function, might do what you need. You would need to pass (bbdb-do-records) and (bbdb-current-field) manually.

EDIT:

I haven't really tried this, but the doc above says, if you run C-u before calling the command then it won't ask you for confirmation. So, in the code, you can do something like this:

(defun bbdb-delete-field-or-record-no-confirmation ()
  ""
  (interactive)
  (let ((current-prefix-arg 1))
    (call-interactively #'bbdb-delete-field-or-record)))
2
  • Thanks. Yeah, I'm familiar with call-interactively, and one can do what I want with something like (call-interactively (lambda () (interactive) (bbdb-delete-field-or-record (bbdb-do-records) (bbdb-current-field) t))), but I was interested in learning whether Elisp provides a better way of doing this. If there was no need to pass an argument, I could just do (call-interactively 'bbdb-delete-field-or-record), so I was wondering whether there was a way of passing the optional argument only, without also having to pass the two other arguments explicitly, in this more convoluted way.
    – Pablo
    Mar 26, 2023 at 18:08
  • There is also funcall. But I really don't think you can bypass passing the positional arguments if you call the function from code. Also, I suppose it is just a typo, you don't need the (interactive) bit in the call-interactively as it is already being called interactively.
    – Arktik
    Mar 27, 2023 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.