Hot answers tagged

26

There are various ways to do it. Kaushal's way can be made a good bit more efficient, with: (goto-char (point-min)) (while (not (eobp)) (let ((line (buffer-substring (point) (progn (forward-line 1) (point))))) ...)) But in Emacs it is much more customary to work on the buffer rather than on strings. So rather than ...


17

Use cl-map, instead: (cl-map 'vector #'1+ [1 2 3 4]) A little extra background: cl-map is the Common Lisp map function that generalizes to sequence types: (cl-map 'vector #'1+ '[1 2 3 4]) ;; ==> [2 3 4 5] (cl-map 'list #'1+ '(1 2 3 4)) ;; ==> (2 3 4 5) (cl-map 'string #'upcase "abc") ;; ==> "ABC" It can also convert between sequence types (eg,...


15

Since I was beat by 18 seconds, here's a simpler and safer way to do it without the cl library. It also doesn't evaluate the elements. (apply #'vector (mapcar #'1+ [1 2 3 4])) ;; => [2 3 4 5]


13

This would probably depend on a situation. In general, if I need to tie a number of values with a number of names, I'd use a hash-table, but if I have to use a property list, I'd use cl-loop. Below are some examples: (cl-loop for (key value) on '(:prop1 a :prop2 b :prop3 c) by 'cddr collect value) ;;; (a b c) And if you have a data-structure you ...


9

If you're happy using dash.el you can use -each and destructuring -let: (require 'dash) (--each packages (-let [(author . name) it] (git-clone author name))) Alternatively, you can use -lambda from dash.el to create an anonymous function with destructuring: (mapcar (-lambda ((author . name)) (git-clone author name)) packages)


9

You can create an anonymous lambda function to take each element of your list and apply your function to it. Example: (defvar packages '(("auto-complete" . "auto-complete") ("defunkt" . "markdown-mode"))) (defun toy-fnx (author name) "Just testing." (message "Package %s by author %s" name author) (sit-for 1)) (mapcar (lambda (...


8

It's really a function, which means add 1. you can try below example: ELISP> (1+ 3) 4 (#o4, #x4, ?\C-d) The implementation is at data.c: DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0, doc: /* Return NUMBER plus one. NUMBER may be a number or a marker. Markers are converted to integers. */) (register Lisp_Object number) { ...


8

You will likely get various loop and iteration answers. AFAIK, there is no idiomatic way to do this. I don't even think it's very common to want to accumulate only the property values (not associating them with the properties). Here is one simple way to do it: (defun prop-values (plist) "..." (let ((pl (cdr plist)) (vals ())) (while pl ...


7

I don't know of any idiomatic way but I came up with this: (defun my/walk-line-by-line () "Process each line in the buffer one by one." (interactive) (save-excursion (goto-char (point-min)) (while (not (eobp)) (let* ((lb (line-beginning-position)) (le (line-end-position)) (ln (buffer-substring-no-properties lb ...


6

With Emacs from the current Git HEAD which will become Emacs 25, you can do the following, i.e., turn the plist into an alist easily with the new seq-partition function, and then process the alist entries using standard mapcar. (let* ((my-plist (list :a 1 :b 2 :c 3 :more (list 4 5 6))) (my-alist (seq-partition my-plist 2)) (my-reverse-alist (...


6

The two built-in "zip-with" functions: seq-mapn from seq.el (Emacs ≥ 25.1) cl-mapcar from cl-lib.el (Emacs ≥ 24.3; was mapcar* in cl.el before that) and the most prominent third-party one: -zip-with from dash.el (Dash ≥ 1.1.0) all share the limitation you describe: all the map functions seem to quit at the shortest list The philosophical ...


6

Just syntax error. To create your alist, use to following: (mapcar (lambda (x) (cons x 'k)) '(a b c d e))


5

You want to apply the solution recursively. For example: (cl-labels ((upcase-nested (x) (cond ((consp x) (mapcar #'upcase-nested x)) ((stringp x) (upcase x)) (t x)))) (mapcar #'upcase-nested '(("kittens" ("spam") "puppies") ("otters" "bunnies" ("spam" ("spam" ("spam" ("...


5

To me the answer is to turn the plist into an alist, which is an equivalent data-structure, just half as deep and easier to manipulate.


4

I think the following is as idiomatic as it can get: (dolist (line (split-string (buffer-string) "\n")) ... process line here ... ) EDIT: Here is another solution with loop in place of dolist, and which also classifies the lines according to whether or not they match your regular expression: (loop for line in (split-string (buffer-string) &...


4

Taken literally, your sample line would translate to the following: (define-key evil-normal-state-map (kbd "<tab>") (kbd ">>")) Although I don't personally recommend this. It's much cleaner to reference the function you want to run by name instead of using a keyboard macro. This clears up the need for a distinction between nmap and nnoremap, ...


4

I think you've overcomplicated the problem. You're obtaining a list comprising hours, minutes, seconds, and microseconds, so I think your code would be simpler and more readable if you expressed that explicitly: (require 'cl-lib) (defvar time-stamp "00:00:52,010") (cl-destructuring-bind (hours minutes seconds microseconds) (mapcar #'string-to-number (...


4

It's mainly just a warning - really a suggestion. It's pointing out that if you don't care to produce a new list from the original one, where the returned list's elements are the results of applying the function argument to the original list's elements, then you might as well just use dolist or mapc instead of mapcar. In this case, your mapcar is, yes, ...


4

Without looking up any of the Org variables or functions you refer to, here's a guess at what you're trying to do. They both do the same thing: iterate over a list of file names, expanding them in directory org-gtd-folder, and setting the value of variable org-agenda-files to the resulting list of absolute file names. ;; Use `mapcar' (setq org-agenda-files ...


4

You need a nested mapcar and mapcan: (mapcar (lambda (l) (list (car l) (mapcan (lambda (x) (if (= x 2) (list x 7) (list x))) (cadr l)))) '((a (1 2 3 4)) (b (1 2)) (c (1 3 4 5)))) ==> ((a (1 2 7 3 4)) (b (1 2 7)) (c (1 3 4 5)))


3

The function -tree-map from the dash package does that: (-tree-map #'upcase '(("kittens" "puppies") ("otters" "bunnies"))) (("KITTENS" "PUPPIES") ("OTTERS" "BUNNIES"))


3

Library Dired+ offers a bunch of commands that let you act on the marked files and the files marked in marked subdirectories, defined recursively. These commands have names like diredp-*-recursive. It is also the case that you can insert any subdirs (and subsubdirs etc.) into the same Dired buffer, and then act on marked files there. One of the Dired+ ...


3

If you wanted this for a function you would use apply, but as or is a special form you can't do that. In particular, or only evaluates as many arguments as it needs to. You could write a macro: (defmacro or-list (list) `(or ,@(eval list))) (setq mylist '(a b c)) (or-list mylist) then expands to (or a b c)


2

For completeness, using seq: (require 'seq) (seq-into (seq-map #'1+ [1 2 3 4]) 'vector)


2

The not so elegant inplace-variant for the case that the original vector is no longer needed afterwards and memory allocation is time-critical (e.g. the vector is big). (setq x [1 2 3 4]) (cl-loop for var across-ref x do (setf var (1+ var))) The result is stored in x. If you need the form to return x in the end you can add finally return x as ...


2

If you haven't already try undo-tree.el. It gives Emacs a conventional undo, otherwise you need to undo the undos in order to redo--there is no redo function... With it you can add: (global-set-key (kbd "C-s-/") 'undo-tree-redo) to your emacs init file, or where ever you keep custom keybindings, and walk back and forward through any change.


2

Following up Basils comment on the problems with recursive solutions I give here an iterative solution. It exploits a breadth first search and therefore needs a queue. My first attempt with a depths-first search caused me some trouble with creating the structure and setting the values of the return tree. The queue impl comes with the answer but you can ...


2

Or... you can just loop for yourself: (let ((long '("1" "2" "3" "4")) (short '("a" "b" "c")) ret) (while (or long short) (setq ret (cons (concat (car-safe long) (car-safe short)) ret) long (cdr long) short (cdr short))) (nreverse ret))


2

Append the needed number of empty strings to short: (let* ((long '("1" "2" "3" "4")) (short '("a" "b" "c")) (_short (append short (make-list (- (list-length long) (list-length short)) "")))) (mapcar* 'concat long _short))


2

One way to improve speed is to parse the contents of your agenda files once in a temporary buffer collecting the effort of all the entries matching goal+PRIORITY="B" (see Test 1). With ~10K lines, I get "Elapsed time: 0.052280s" compared to "Elapsed time: 1.340006s" using org-map-entries (Test 2) which I think is what you were trying to do. For better ...


Only top voted, non community-wiki answers of a minimum length are eligible