From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alexis Roda Newsgroups: gmane.emacs.help Subject: Re: Optional Arguments Date: Mon, 7 Dec 2020 09:16:56 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21933"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Help Gnu Emacs To: pietru@caramail.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Dec 07 09:18:19 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmBit-0005cg-D6 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 07 Dec 2020 09:18:19 +0100 Original-Received: from localhost ([::1]:33096 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kmBis-0000F0-DS for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 07 Dec 2020 03:18:18 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59784) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kmBhw-0000EI-96 for help-gnu-emacs@gnu.org; Mon, 07 Dec 2020 03:17:20 -0500 Original-Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]:54262) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kmBhl-0002SS-Ug for help-gnu-emacs@gnu.org; Mon, 07 Dec 2020 03:17:19 -0500 Original-Received: by mail-wm1-x32d.google.com with SMTP id k10so10679340wmi.3 for ; Mon, 07 Dec 2020 00:17:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vS9f/dH11iqEl87vbw3335TZJ2+I11JJ9QY6H3NMwAQ=; b=HSgYGMZBIqlOtHDd8PO/c8g5WiUfy5QVuFXuHLpv8GAX4JrFfpWd3jG0yEaO06+2hY 06tiMYSN3DAuYsneC+/D5WWL/+fDDGWrtSQjARkVb9HnRVwqGCHyKF+llmyNM9AXXudw 841Fi6uqIzPV3BHAY/5hZ5sO7HoNG7RpRsu5/cBFyHEozgl18VZl9crGN9/uLMULDw7K 0KrB6Dl2jffJtQ5g1OX4Zz6kAMIcBIcd1ZkwkBycxvgzPZsL/rVYiDBBL5yKct2TEIsF cbrPpUYMbbNp0X8qKKL6rmHvRL69uaFZG40k6bYppnIPsQihe6GDGeMRJsb/LpOEBOP0 RTYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vS9f/dH11iqEl87vbw3335TZJ2+I11JJ9QY6H3NMwAQ=; b=O5uNAZQficHZBbPor6uHp4PGehxVV6OWyQJwOceYOeJjT4bK2I0vON/Qxe6AVXCaBQ VVaiC8jJW/uETRaDrfAeZOxx4G9v2bSd66Vw7cmXbs+AIA8n4ac5zo7bHcEy10Y/sQ+6 Nvuq6oUO/xLi8jbaitT++bn+9ASVy7200fkZfXRyJ1D0SU3QJsOnvTCQWhg7uGSUlxmZ 6w7KDYrAts5C6bWVj0Zm55RAdsSs2BPVl1eS+ej3C/jYqQq/3hcMXlYBHgpS6ak+NcfR y2Nk7FSKMpeZfW1NtKhh1SXIP0bpT9upTAnRkHoBJG5Yi+PBFMuY0TiDEkAVeQirPGLv Yhpg== X-Gm-Message-State: AOAM531BVIhXqI31Tk757nN6cgXSlWzh1dn4CS0JBaCPlEyUHrjr6rq2 gRa3QhYDY84QlBuN3+ksrIgSaYmbpwz/ccMEsA4= X-Google-Smtp-Source: ABdhPJxsYA7BJjBjkfBh05UKA5H3R7wfuvEGRQlQEx0WQciYPPsVXVfLfiF1KveGr10MNX7bLonkabk4+7CXHppqTMw= X-Received: by 2002:a1c:4e0a:: with SMTP id g10mr16836077wmh.88.1607329027975; Mon, 07 Dec 2020 00:17:07 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=alexis.roda.villalonga@gmail.com; helo=mail-wm1-x32d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:126146 Archived-At: Hi, Not sure what your question is. Regarding the error, I'm assuming that you execute the function with 'M-x typh-word-markers'. That won't work interactively because the function expects two arguments and 'interactive' don't tell emacs how to get them. It works if you execute it with 'M-: (typh-word-markers nil nil)', from IELM or from the scratch buffer. That said, in your code the arguments are not required, they are used only as local variables, not to provide information to the function, so the function can be written with a 'let' [1][2] form defining local variables: (defun typh-word-markers () (interactive) (let ((ma) (mb)) (skip-chars-backward "[:alpha:]") (setq ma (point)) (skip-chars-forward "[:alpha:]") (setq mb (point)) (message "[ma,mb]: %s,%s" ma mb)) ) In the first function '&optional' is not strictly required. Since you specify in 'interactive' the 'n' option [3] (A number, read with the minibuffer. If the input is not a number, the user has to try again. =E2=80= =98n=E2=80=99 never uses the prefix argument. Prompt.) the user will be prompted for a numeric value and the function will always get an argument. That's the opposite case to typh-word-markers. Probably you want the 'p' code [3]: The numeric prefix argument. (Note that this =E2=80=98p=E2=80=99 is lower case.) No I/O. (defun typh-skip-chars (n) (interactive "p") (if (=3D n 1) (skip-chars-forward "[:alpha:]") (skip-chars-backward "[:alpha:]")) ) That way executing 'M-x typh-skip-chars' will skip chars forward while 'C-u M-x typh-skip-chars' will skip chars backward. No interactive input will be requested to the user, the input to the function is provided with the prefix argument [4]. That's more idiomatic. Hope this helps [1] https://www.gnu.org/software/emacs/manual/html_node/eintr/Parts-of-let-Expr= ession.html [2] https://www.gnu.org/software/emacs/manual/html_node/eintr/Sample-let-Expres= sion.html#Sample-let-Expression [3] https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes= .html [4] https://www.gnu.org/software/emacs/manual/html_node/emacs/Arguments.htm= l Missatge de l'adre=C3=A7a del dia dl., 7 de des. 2020= a les 4:17: > Would like to have two functions > > 1. skip-chars - skip to beg of word (if no argument) or end of word (if > argument is 1) > > 2. word-markers - get beg and end of a word position of word > > Have written the following. For the first one, I have not used optional > arguments before. > And have read documentation about functions. > > About the second, there errors when I try to execute. > > (defun typh-skip-chars (&optional n) > (interactive "n Skip direction: ") > (if (=3D n 1) > (skip-chars-forward "[:alpha:]") > (skip-chars-backward "[:alpha:]")) ) > > (defun typh-word-markers (ma mb) > (interactive) > (skip-chars-backward "[:alpha:]") > (setq ma (point)) > (skip-chars-forward "[:alpha:]") > (setq mb (point)) > (message "[ma,mb]: %s,%s" ma mb) ) > > > >