Skip to content

Latest commit

 

History

History
216 lines (149 loc) · 3.79 KB

el-search.org

File metadata and controls

216 lines (149 loc) · 3.79 KB

el-search.el

Find undocumented defvar

`(defvar ,_ ,_)

Find unnecessary lambda such as (lambda (x) (f x))

`(lambda (,arg) (,fun ,arg))

Find references

For example, to find all (likely) callers of pcase-let, use M-x el-search-load-path with:

`(pcase-let . ,_)

Try the package elisp-refs.

Replace key into kbd such as [?\C-x ?l] or “\C-xl” into (kbd “C-x l”)

info:elisp#Key Sequences

To replace

(define-key (current-global-map) [?\C-x ?l] #'count-lines-page)
(define-key (current-global-map) "\C-xl"    #'count-lines-page)

into

(define-key (current-global-map) (kbd "C-x l") #'count-lines-page)

use

M-x el-search-query-replace RET

`(define-key ,map ,(and key (or (pred vectorp) (pred stringp))) ,cmd)
->
`(define-key ,map (kbd ,(key-description key)) ,cmd)

RET

Find string in code

(pred stringp)

Notes that dostring is also considered as string.

Swap function arguments such as (foo a b c) into (foo b a c)

`(foo ,a ,b . ,rest)
->
`(foo ,b ,a . ,rest)

if -> when/unless/cond

Change

(if x 100)

(if (not x) 100)

(if (< x 0)
    "x < 0"
  (if (= x 0) "x = 0" "x > 0"))

into

(when x 100)

(unless x 100)

(cond ((< x 0) "x < 0")
      ((= x 0) "x = 0")
      (t "x > 0"))

via

(iffy-if repl) -> repl

push -> add-to-list

For example, to replace

(push '("\\.rkt[dl]?\\'" . racket-mode) auto-mode-alist)

with

(add-to-list 'auto-mode-alist '("\\.rkt[dl]?\\'" . racket-mode))

use

`(push ,elt ,lst) -> `(add-to-list ',lst ,elt)

mapc -> dolist

For example, to change

(mapc
 (lambda (x)
   (message "%s" (+ x 100)))
 '(1 2 3))

into

(dolist (x '(1 2 3))
  (message "%s" (+ x 100)))

use

`(mapc (lambda (,var) . ,body) ,list)
->
`(dolist (,var ,list) . ,body)

mapcar -> –map

For example, to change

(mapcar (lambda (x) (* x x)) '(1 2 3))

into

(--map (* it it) '(1 2 3))

use

(defun transform-lambda-form-for---map (lambda-form)
  (pcase-let ((`(lambda (,var) . ,body) lambda-form))
    (macroexpand-1
     `(cl-symbol-macrolet ((,var it))
        ,@body))))

and

`(mapcar ,lambda-form ,list)
 ->
`(--map ,(transform-lambda-form-for---map lambda-form) ,list)

‘fun -> #’fun

(add-hook 'before-save-hook 'time-stamp)
(add-hook 'before-save-hook #'time-stamp)

`(add-hook ,hook (quote ,f))
->
`(add-hook ,hook (function ,f))
(define-key term-mode-map [?\C-c ?\C-j] 'term-char-mode)
(define-key term-mode-map [?\C-c ?\C-j] #'term-char-mode)

`(define-key ,m ,k (quote ,f))
->
`(define-key ,m ,k (function ,f))

Search nested list such as (catch … (throw …))

such as cl-block/cl-return, catch/throw, pcase/app etc

(el-search-emacs-elisp-sources '(l ^ 'catch __ (contains (l ^ 'throw))))
(el-search-emacs-elisp-sources '(l ^ 'pcase __ (contains (l ^ 'app))))

对于同时存在函数和变量的符号,只搜索变量

比如 auto-insert 这个符号函数定义和变量定义同时存在。

排除掉函数调用以及一些定义:

(l (pred (lambda (x) (not (memq x '(quote defun defgroup defcustom))))) __ 'auto-insert __)