all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Passing a list to a function
@ 2021-07-12  5:32 lisa-asket
  2021-07-12 12:01 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 4+ messages in thread
From: lisa-asket @ 2021-07-12  5:32 UTC (permalink / raw
  To: help-gnu-emacs

I have a function named `outline-headings` that uses a list called `hdlevels`

that is filled bp a defvar named `hstyle-hdl`.



How can I make `outline-headings` take the list as an argument? 



(defvar hstyle-hdl
   '( ("@c h1" . 1) ("@c h2" . 2) ("@c h3" . 3) ("@c h4" . 4)
      ("@c h5" . 5) ("@c h6" . 6) ("@c h7" . 7) ("@c h8" . 8) )
   "Define names and levels for texinfo outline headings." )



(defun outline-headings ()
  "Sets texinfo headings for use in outline mode."
  (interactive)
  (let ( (hdlevels hstyle-hdl) )
    (setq outline-regexp
      (concat (regexp-opt (mapcar 'car hdlevels)) "\\>"))
    (setq outline-heading-alist hdlevels)) )



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

* Re: Passing a list to a function
  2021-07-12  5:32 Passing a list to a function lisa-asket
@ 2021-07-12 12:01 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-12 12:36   ` lisa-asket
  0 siblings, 1 reply; 4+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-12 12:01 UTC (permalink / raw
  To: help-gnu-emacs

lisa-asket wrote:

> How can I make [a function] take [a] list as an argument?

It works like any other function and argument:

(defun is-this-a-list (lst)
  (listp lst) )

(is-this-a-list 1) ; nil
(is-this-a-list '(1 2 3)) ; t

-- 
underground experts united
https://dataswamp.org/~incal




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

* Passing a list to a function
  2021-07-12 12:01 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-12 12:36   ` lisa-asket
  2021-07-12 13:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 4+ messages in thread
From: lisa-asket @ 2021-07-12 12:36 UTC (permalink / raw
  To: moasenwood, help-gnu-emacs

What do you think of this code?  Is it valid?



(defvar hstyle-hdl
   '( ("@c h1" . 1) ("@c h2" . 2) ("@c h3" . 3) ("@c h4" . 4)
      ("@c h5" . 5) ("@c h6" . 6) ("@c h7" . 7) ("@c h8" . 8) )
   "Define names and levels for texinfo outline headings." )



(defun outline-headings (hdlevels)
  "Sets texinfo headings for use in outline mode."



    (setq outline-regexp
      (concat (regexp-opt (mapcar 'car hdlevels)) "\\>"))
    (setq outline-heading-alist hdlevels) )


(outline-headings hstyle-hdl)




From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Passing a list to a function
Date: 12/07/2021 14:01:25 Europe/Paris

lisa-asket wrote:

> How can I make [a function] take [a] list as an argument?

It works like any other function and argument:

(defun is-this-a-list (lst)
(listp lst) )

(is-this-a-list 1) ; nil
(is-this-a-list '(1 2 3)) ; t

-- 
underground experts united
https://dataswamp.org/~incal





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

* Re: Passing a list to a function
  2021-07-12 12:36   ` lisa-asket
@ 2021-07-12 13:52     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 4+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-12 13:52 UTC (permalink / raw
  To: help-gnu-emacs

lisa-asket wrote:

> Is it valid?

Run this...

(require 'checkdoc)

(defun check-package-style ()
  (interactive)
  (let ((msg "Style check..."))
    (message msg)
    (checkdoc-current-buffer t) ; TAKE-NOTES
    (message "%sdone" msg) ))
    
(defalias 'check-style #'check-package-style)

It says it is valid but

  Argument ‘hdlevels’ should appear (as HDLEVELS) in the doc
  string
  
  Probably "Sets" should be imperative "Set"

Also try to byte-compile it, the byte-compiler then says it is
valid but

  Warning: assignment to free variable ‘outline-heading-alist’

You can add

  (defvar outline-heading-alist)

to make it quiet.

> What do you think of this code?

I would avoid using `setq' in defuns, especially in cases like
yours, but it isn't wrong to do so.

Also I think some of the data either appear several times or
is a function of some other data (or both), so a lot can be
pruned, for example the way you set "hstyle-hdl" ...

> (defvar hstyle-hdl
>    '( ("@c h1" . 1) ("@c h2" . 2) ("@c h3" . 3) ("@c h4" . 4)
>       ("@c h5" . 5) ("@c h6" . 6) ("@c h7" . 7) ("@c h8" . 8) )
>    "Define names and levels for texinfo outline headings." )

... you see how the elements seem to be ("@c hx" . x) where
x is the position in the list (starting from 1) - yet you have
the same data (x) hard-coded twice everywhere - that doesn't
look good.

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-07-12 13:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-12  5:32 Passing a list to a function lisa-asket
2021-07-12 12:01 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-12 12:36   ` lisa-asket
2021-07-12 13:52     ` Emanuel Berg via Users list for the GNU Emacs text editor

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.