Fork me on GitHub
#emacs
<
2018-02-06
>
qqq06:02:16

In elisp, is there a simple way to alias do as progn ?

qqq06:02:39

in particular, I want to rewrite the following in elisp:

(defmacro do [& args]
  `(progn [email protected]))

qqq16:02:13

in Emads, what is the fastest way o find all lines that match a particular regex? I'm looking for all lines that start with

;; *
;; **
;; ***
;; ****
to generate a TOC of sorts. All solutions on SO basically show using find-next-regexp repeatedly until end of buffer. Is there a better solution ?

caio16:02:20

no idea, but you can look at occurs source to search for insights: https://github.com/emacs-mirror/emacs/blob/master/lisp/replace.el#L1394

bostonaholic16:02:34

@qqq if you’re doing this in org-mode, there is already a way to generate TOC https://orgmode.org/manual/Table-of-contents.html

qqq16:02:59

What does the matches in the let form of

(defun re-seq (regexp string)
  "Get a list of all regexp matches in a string"
  (save-match-data
    (let ((pos 0)
          matches)
      (while (string-match regexp string pos)
        (push (match-string 0 string) matches)
        (setq pos (match-end 0)))
      matches)))
mean ?

qqq16:02:26

@bostonaholic: I'm working on something custom where I pipe it to ivy

dpsutton16:02:34

it's a nil variable binding

dpsutton16:02:51

emacs lisp allows for a binding pair, or just a symbol which means it is bound to nil

bostonaholic16:02:58

the matches is short for (matches nil)

bostonaholic16:02:43

you can also test it out yourself by evaluating

(let ((pos 0)
      matches)
  matches)

bostonaholic16:02:59

that expression returns nil

qqq16:02:55

clojure has a builtin of a hashmap; does emacs have anything similar for storing 'structs' ?

qqq17:02:12

by hashmap I'm referring to a {} where the keys are keywords

chris17:02:11

emacs has a hash-table

chris17:02:26

or if you're on an ancient version you can use alists instead

dpsutton17:02:03

depending on size alist can be faster and easier. hashmaps have some overhead and can be overkill for simple things

qqq17:02:30

'((1 2 3) (4 5 6) (7 8 9))
==> (3 6 9) how do I write a ELISP function that does that? I've tried (map #'caddr lst) already, but it doesn't work as map wants 3 arguments

qqq17:02:51

ah, it's mapcar, not map

chris17:02:26

if you happen to have dash.el (which you probably do, lots of libraries use it) you can just use the functions you expect map, filter, reduce and their friends prefixed with a dash and they will work roughly like the functions you expect

chris17:02:49

-map in dash.el directly wraps mapcar, for example

qqq18:02:17

(defun heading-keep? (s)
      (not (string-match "[ *=]*" s)))

    (heading-keep? "=====")
    (heading-keep? "* =====")
    (heading-keep? "hello world")
I expect the response to be false false true, but I get nil nil nil basicaly, I want to keep all headings, except those made of the chars space, star, equal-sign

caio20:02:39

(defun heading-keep? (s)
  (not (string-match "^[ *=]+$" s)))

caio20:02:57

your version is matching the space between hello and world

caio20:02:06

btw, + instead of * will make a difference for empty strings. using * will return t and with + will return nil. idk what your requirements are in this case

qqq20:02:09

@caio: right, thanks for the ^$ -- forgot about the substring matching

qqq20:02:56

(defmacro do (&rest exps)
 `(progn ,@exps) )
^-- what is wrong with this macro? I'm trying to make do an alias to progn (loses less space to nested indentations)

qqq20:02:18

oh, emacs has a builtin do macro, lol

tanzoniteblack20:02:17

it does have a do, but I don't think it does the same as progn / Clojure do(if memory serves, it's more like do-while)

tanzoniteblack20:02:02

but because it does already have that macro, re-defining it with a different meaning will cause breakage with any packages that use the original macro

tanzoniteblack20:02:10

What's wrong with just using progn?

qqq21:02:36

There's nothing wrong with progn; I just was not aware do was a builtin, and wanted to use it.

tanzoniteblack21:02:38

fair enough, I have done many things in emacs "just because" 🙂