Questions tagged [list]

is for the list object in Lisp programming. A list represents a sequence of zero or more elements. Emacs Lisp relies extensively on the list data structure; it is a fundamental building block. Emacs provides many facilities for building, modifying, extending, and efficiently handling list data structures.

Filter by
Sorted by
Tagged with
15
votes
1answer
2k views

Is '(a . b) really a list?

I'm really confused with the . notation. Is '(a . b) a list? (listp '(a . b)) returns t but when I want to know its length (length '(a . b)) gives an error Wrong type argument: listp, b. The same is ...
13
votes
2answers
7k views

How to concatenate two lists?

Sorry, but elisp is not my best. I need to combine two lists of strings in this way: ("a" "b" "c") + ("d" "e" "f") -> ("a" "b" "c" "d" "e" "f") Well, the order is not important, so I wrote this ...
12
votes
1answer
451 views

Why does a constant in `let` change with repeated calls? [duplicate]

Say we have such a foo function: (defun foo (e) (let ((lst '(a b c))) (delq e lst))) Then we use it in following way (sequentially evaluating one by one): (foo 'c) ; => (a b) (foo 'b) ; =&...
12
votes
1answer
1k views

Meaning of period in (. 123)

I learnt . /path/to/file in bash is used to execute a file. Just out of curiosity, I eval something like the following in Emacs (. 123) ⇒ 123 (read "(. 123)") ⇒ 123 It looks like Emacs ...
8
votes
6answers
5k views

How to remove / delete nth element of a list

Q:  How to remove / delete nth element of a list. CAVEAT:  Do not remove all occurrences / members matching nth element -- e.g., eq or equal. EXAMPLE:  Remove the 17th element of: '(a b c d e f g h ...
8
votes
2answers
125 views

Proper reinitialization of a list? What is happening under the hood?

I'm teaching myself some more elisp and have encountered the following problem: If I want to reset a list variable it won't get updated after the first evaluation. Here is some example code: (defun ...
7
votes
1answer
305 views

Convert 2-item list to a dotted pair?

Q: how can I convert a 2-item list to a dotted pair cons cell? I can convert a 2-item list to a dotted pair with: (let ((list '(1 2))) (cons (car list) (cadr list))) ; => (1 . 2) I'm ...
6
votes
2answers
1k views

add-to-list vs add-hook?

What is the difference between add-to-list vs add-hook? For example I see in progmodes/make-mode.el: (add-hook 'completion-at-point-functions #'makefile-completions-at-point nil t) ...
6
votes
3answers
424 views

Report duplicates in a list?

Q: how do I get only the duplicate elements in a list? delete-dups (and cl-delete-duplicates) deletes all duplicate elements from a list: (delete-dups '(a b c c d d)) ; => '(a b c d) ...
6
votes
1answer
134 views

Removing the last element of a list

Is there a simpler way to remove the last element of a list than this? (setq list (reverse (cdr (reverse list))))
6
votes
1answer
138 views

Shortening trees list with (setcdr (nthcdr 2 trees) nil)

I am starting to learn elisp and working through this tutorial https://www.gnu.org/software/emacs/manual/html_node/eintr/kill_002dnew-function.html and I do not understand this basic example: (setq ...
5
votes
3answers
935 views

How to check if a given string is a substring of an element of a list

According to documentation, member checks if a a given element is an element of a list. For example if the list, say my_list consists of apple and orange, (member "apple" my_list) returns true. Is ...
5
votes
2answers
841 views

How to iterate over JSON list and select one element

Say I have a list of names with ids, in JSON: (let ((json-object-type 'plist)) (setq mylist (json-read-from-string "[{\"name\": \"Adam\", \"id\": \"1\"}, {\"...
5
votes
1answer
323 views

Add to a list that defcustom has not yet defined

We can avoid adding duplicate elements to a list variable by using add-to-list. When setting a customizable variable (i.e. one that is or will be defined by defcustom) from our init file, we're ...
5
votes
1answer
361 views

Converting nested lists to org-mode lists

I need to convert all nested lists present in buffer to org-mode headers using elisp. How can I do this? 1.2.7.2 Some Header Name -> **** Some Header Name
5
votes
0answers
234 views

When to use quote for lists? Modifying quoted lists in Elisp

The Common Lisp Hyper Spec says: The consequences are undefined if literal objects (including quoted objects) are destructively modified. This is further motivated at the Constant Modification ...
5
votes
0answers
130 views

Structure-preserving copying of sequences

The form (cl-copy-tree TREE t) returns a deep-copy of a sequence TREE. The the Common Lisp Hyper Spec explains that copy-tree does not preserve circularities and sharing of substructures. I.e., even ...
4
votes
4answers
275 views

Map function onto nested list?

Q: how can I map a function onto elements of nested lists? For flat lists, we can use mapcar to apply a function to each element of the list: (setq flat '("kittens" "puppies" "otters" "bunnies")) (...
4
votes
2answers
290 views

How can I paste paragraph not to interrupt numbers in list org mode?

In list environment, after I copy several paragraphs, the next item number will change into number 1, How can I deal with that? For example, 1. aaa 2. aaa 3. aaa bbb <---this is what I copied ...
4
votes
1answer
181 views

Check box add date in org mode

I have a book list contained which will be read or had done. Like this * English books [0/2] - [ ] bible - [ ] When I use C-c C-c to check, is it possible to add date behind or add low level ...
3
votes
4answers
266 views

How to mapcar uneven lists?

I am aware I can use mapcar to join 2 lists into pairs, however all the map functions seem to quit at the shortest list. This for example (cl-mapcar #'concat '("1" "2" "3" "4") '("a" "b" "c")) ...
3
votes
1answer
229 views

What's difference between + and - in the list in org mode

I find I can create bullets with the - and + both. How can I decide when to use + and -
3
votes
2answers
130 views

How to delete all list elements matching a regexp?

Given a list: (setq mylist '("a1" "a2" "b1" "b2")) How to delete all elements that match ^a? What I want is "a1" and "a2" deleted, only left "b1" and "b2" in the list.
3
votes
2answers
52 views

How to define a bundle of variable-and-function pairs?

I want to define a bundle of variable-and-function pairs, e.g.: vl/path-doc points to my often used path, and vl/open-path-doc is used for open it in dired-mode. I tried this piece of code: (setq ...
3
votes
1answer
88 views

Org Mode. Set separation length between elements of a list

I have noticed there is a big gap between the elements of a list when exporting to PDF. Is there a way to modify it?
3
votes
3answers
134 views

List manipulation — appending duplicate equal lists

START WITH: (let ((my-list '((12 1 2016) (12 2 2016) (12 3 2016) (12 1 2016) (12 4 2016) (12 5 2016) ...
3
votes
1answer
77 views

Does `mapc` place a symbol on the active element of the sequence?

I understand that (eq '(3 . d) '(3 . d)) returns nil, and (eq 3 3) returns t. But I do not understand why delq is deleting one (3 . d) in the second example each loop when y is (3 . d). Is mapc ...
3
votes
1answer
125 views

Why is this function not pure

I just spent a significant amount of time debugging an issue that boils down to the following: (defun my-new-alist () `((x . nil))) (setq a (my-new-alist)) (setq b (my-new-alist)) (push 1 (alist-...
3
votes
1answer
173 views

What has replaced elib?

Looking for some data structures for elisp, I came across this. But then this warns me off -- without specifying exactly what replaces elib. So where do I get packages to do doubly linked lists?
3
votes
0answers
240 views

Automatically create checkbox in new line

When creating a list in org-mode, one can press M-RET to create a new line which already has a list item of the same indentation as the line above it in it. How can one achieve the same thing but ...
2
votes
4answers
303 views

Why does the member function return a list's tail rather than t?

Why does Elisp (member '3 '(1 2 3 4 5 6)) return (3 4 5 6) instead of t (true) or 3, or (3)? I see the utility of a function to return the remainder/tail of an ordered list starting from, and ...
2
votes
2answers
371 views

Append lists smartly?

How do I append two lists together, and delete duplicates? For example: (setq list1 ("a" "b" "c")) (setq list2 ("b" "c" "d")) (setq list3 (a b c)) (setq list4 (c d e)) ;; use case (append-list-to-...
2
votes
2answers
101 views

Does `car` on an non-lazy function generated list evals the whole list?

I want to get in a huge directory the first file which satisfies some condition, something like (require 'seq) (require 'f) (defun lazy-filter-file (pred path) (car (seq-filter pred (f--files path ...
2
votes
2answers
271 views

Sort list by decreasing length of the strings

I have this list: (setq MYLIST '("J. Phys." "New. J. Phys." "J. Phys. Soc. Jap." "Phys. Plasmas")) How can I get a new list ordered by the decreasing ...
2
votes
2answers
196 views

Why is the . needed for adding suffixes to the 'auto-mode-alist?

In https://www.emacswiki.org/emacs/AutoModeAlist one needs to use this command in order to add .py for python-mode (add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode)) Why do we need the . ...
2
votes
2answers
426 views

Adding many items to a list

What is the proper way to add many items to a list? I assume just using many add-to-lists is not the normal way?
2
votes
2answers
286 views

Sort list by checked/unchecked state

I was wondering if org-mode offers a function to sort a list by the item states? So that checked items appear on top and unchecked items at the bottom? For example with this list - [ ] Not yet done - ...
2
votes
1answer
54 views

trying to make a self-modifying function [duplicate]

I'm trying to make a function that returns a list of perfect numbers: A perfect number is a positive integer that is equal to the sum of its proper positive divisors For instance, 28 is perfect ...
2
votes
1answer
189 views

Org Mode - Is there a way to reverse org-list-make-subtree?

By using org-list-make-subtree (C-c C-*) you turn an Org List into a sub tree: - Item 1 - Item 2 - Item 3 would turn into * Item 1 ** Item 2 *** Item 3 But is there a built in function (like ...
2
votes
1answer
2k views

Indenting lists in org-mode

I copied and paste text in an org file and formatted it as a list: - Funded pensions :: As explained more fully in Chapter 3, funded pensions are paid on behalf of its members. How can I have the ...
2
votes
2answers
122 views

Evaluating symbol in function arguments affected by destructive operations?

While evaluating a function call, the arguments is from left to right. For example, (let ((x '(1 2 3))) (list (nreverse x) x)) ;; => ((3 2 1) (1)) The first argument (nreverse x) evaluates to (...
2
votes
1answer
87 views

Compare all elements of 2 lists, not necessarily in same order

Q: How to test whether all elements of two lists are the same even though the order may be different? EXAMPLE: (test-fn '(answered cached unread) '(unread answered cached)) => t
2
votes
1answer
56 views

How to add-to-list to read-in file contents

When I do this (setq v (f-read-text "s.el")) where s.el is of this form ((thing1 . thing2) (thing3 . thing4)) I get v back as one long string. And (setq v (append (f-read-text "s.el"))) doesn't seem ...
2
votes
1answer
88 views

How to write a list literal with conditional items? [duplicate]

Given a list literal '(1 2 3) is it possible to conditionally include parts of the list? eg: '(1 2 3 (when thing-is-true '(10 11 12))) 4 5 6) Which would result in '(1 2 3 4 5 6) or '(1 2 3 ...
2
votes
1answer
342 views

How to sort an association list (alist)?

Is there a way to sort an association list? In my case I would like to sort the alist elements by the values in each list element (represented by the integers in the example below). Example: (setq ...
2
votes
1answer
1k views

How to enable type-check rules with flycheck-tslint

I want flycheck to use the tslint's --type-check flag to compile my typescript files. In order to see unused imports on the fly inside emacs. By tslint docs --type-check Enables the type ...
2
votes
1answer
201 views

Change level into list in org mode

I have following text which are expressed in level 5 ***** It's better to be first than it is to be better. ***** The basic issue in marketing is creating a category you can be first in. It’s the law ...
2
votes
1answer
317 views

how to automatically add org-emphasis properties to list titles?

I use org-mode lists, both plain-text lists and numbered lists. I precede my lists with a "list title," which for me is any line ending in a colon. Grocery list: - tomatoes - spinach - blueberries I'...
2
votes
1answer
99 views

turn a list into a set of arguments for a function [duplicate]

I have a defun get-quotes with one mandatory and 2 optional arguments. I would like to do something like this: (let ((articles '("/home/matt/art/mice.pdf" '("/home/matt/art/cats.pdf" "Smith, ...
2
votes
2answers
42 views

Arbitrary length lists as argument for interactive function

Say I have a defun with a dolist inside that I want to be able to pass a string that is then converted to a list with a length equal to the number of words in said string via the minibuffer, so ...