unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Using show-paren-style function in elisp code
@ 2022-06-20  8:00 carlmarcos--- via Users list for the GNU Emacs text editor
  2022-06-20  9:25 ` Rudolf Schlatte
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-06-20  8:00 UTC (permalink / raw)
  To: Help Gnu Emacs

Have written the following function to set `show-paren-style`.  Would it be possible to modify
the code in a way that also allows a user to use the style as an argument so that the function 
can be used in elisp code.  

Is there a standard way for a function to be used in elisp code, or is my plan viable in the way
described?


(defun view-parens ()
  "Visualise parentheses and expressions."
  (interactive)

  (let* ( (cseq '("bracemk" "expression" "mixed"))
          (csel  (completing-read "Visualise: " cseq nil t "mixed")) )

    (pcase csel
      ("bracemk"
       (setq 'show-paren-style 'parenthesis))
      ("expression"
       (setq 'show-paren-style 'expression))
      ("mixed"
       (setq 'show-paren-style 'mixed)) )))





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using show-paren-style function in elisp code
  2022-06-20  8:00 Using show-paren-style function in elisp code carlmarcos--- via Users list for the GNU Emacs text editor
@ 2022-06-20  9:25 ` Rudolf Schlatte
  2022-06-20 17:51 ` Bruno Barbier
       [not found] ` <N51E0wj--3-2@missing-mail-id>
  2 siblings, 0 replies; 4+ messages in thread
From: Rudolf Schlatte @ 2022-06-20  9:25 UTC (permalink / raw)
  To: help-gnu-emacs

carlmarcos--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> Have written the following function to set `show-paren-style`.  Would it be
> possible to modify
> the code in a way that also allows a user to use the style as an argument so
> that the function
> can be used in elisp code.  

Wouldn't it be enough to set `show-paren-style' as a file-local
variable?  (See the Emacs manual for how to set those.)  If you want to
call a function every time the user opens a file, add it to the
`emacs-lisp-mode-hook' variable (use the `add-hook' function).




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using show-paren-style function in elisp code
  2022-06-20  8:00 Using show-paren-style function in elisp code carlmarcos--- via Users list for the GNU Emacs text editor
  2022-06-20  9:25 ` Rudolf Schlatte
@ 2022-06-20 17:51 ` Bruno Barbier
       [not found] ` <N51E0wj--3-2@missing-mail-id>
  2 siblings, 0 replies; 4+ messages in thread
From: Bruno Barbier @ 2022-06-20 17:51 UTC (permalink / raw)
  To: carlmarcos, Help Gnu Emacs

carlmarcos--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> Have written the following function to set `show-paren-style`.  Would it be possible to modify
> the code in a way that also allows a user to use the style as an argument so that the function 
> can be used in elisp code.  
>

I would follow Rudolf's advice, to not have to set the style manually
everytime.

But, if you still want to learn how to use a function both
interactively and in elisp code, you should read the Elisp
documentation about 'interactive'. It explains how to write functions
that you can use as commands.

   (info "(elisp)Using Interactive")

   
In your case, here is a possible solution:

   #+begin_src elisp
     (defun view-parens (style)
       "Visualise parentheses and expressions using STYLE.
     STYLE must be a symbol."
       (interactive (list 
                     (intern (completing-read "Visualise: " '("bracemk" "expression" "mixed")
                                              nil t "mixed"))))
       (setq show-paren-style style)
       )
   #+end_src


    
Note 1: The function 'intern' transforms a string into a symbol; you then don't need a 'pcase' anymore.

Note 2: Your line:

         (setq 'show-paren-style 'parenthesis)
         
should have been:
        
         (setq show-paren-style 'parenthesis)
         
as 'setq' expects a non-quoted symbol.

Bruno




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using show-paren-style function in elisp code
       [not found] ` <N51E0wj--3-2@missing-mail-id>
@ 2022-06-21  4:58   ` carlmarcos--- via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 4+ messages in thread
From: carlmarcos--- via Users list for the GNU Emacs text editor @ 2022-06-21  4:58 UTC (permalink / raw)
  To: Bruno Barbier; +Cc: Help Gnu Emacs



Jun 20, 2022, 17:51 by brubar.cs@gmail.com:

> carlmarcos--- via Users list for the GNU Emacs text editor
> <help-gnu-emacs@gnu.org> writes:
>
>> Have written the following function to set `show-paren-style`.  Would it be possible to modify
>> the code in a way that also allows a user to use the style as an argument so that the function 
>> can be used in elisp code.  
>>
>
> I would follow Rudolf's advice, to not have to set the style manually
> everytime.
>
> But, if you still want to learn how to use a function both
> interactively and in elisp code, you should read the Elisp
> documentation about 'interactive'. It explains how to write functions
> that you can use as commands.
>
>  (info "(elisp)Using Interactive")
>
>  
> In your case, here is a possible solution:
>
>  #+begin_src elisp
>  (defun view-parens (style)
>  "Visualise parentheses and expressions using STYLE.
>  STYLE must be a symbol."
>  (interactive (list 
>  (intern (completing-read "Visualise: " '("bracemk" "expression" "mixed")
>  nil t "mixed"))))
>  (setq show-paren-style style)
>  )
>  #+end_src
>
>
Cheers Bruno,  I wonder how the symbol provided by intern is taken up by the variable `style`.


>  
> Note 1: The function 'intern' transforms a string into a symbol; you then don't need a 'pcase' anymore.
>
> Note 2: Your line:
>
>  (setq 'show-paren-style 'parenthesis)
>  
> should have been:
>  
>  (setq show-paren-style 'parenthesis)
>  
> as 'setq' expects a non-quoted symbol.
>
> Bruno
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-21  4:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-20  8:00 Using show-paren-style function in elisp code carlmarcos--- via Users list for the GNU Emacs text editor
2022-06-20  9:25 ` Rudolf Schlatte
2022-06-20 17:51 ` Bruno Barbier
     [not found] ` <N51E0wj--3-2@missing-mail-id>
2022-06-21  4:58   ` carlmarcos--- via Users list for the GNU Emacs text editor

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).