emacs/old-config/lisp/elisp.el
2018-04-07 10:54:04 +08:00

112 lines
2.6 KiB
EmacsLisp

;;
;; Some functions which will be used
;; in initialization of EMACS
;;
(defun iterate-on-list (list action)
(when list
(let ((dir (car list)))
(let ((val (funcall action dir)))
(if val val
(iterate-on-list (cdr list)
action))))))
(defun which (prog)
(let ((app (executable-find prog)))
(if app
(file-truename app)
(iterate-on-list (split-string (getenv "PATH") ":")
#'(lambda (dir)
(let ((filename (concat dir "/" prog)))
(if (file-executable-p filename)
filename
nil)))))))
(defun partition-list (lis separator-p sublis acc)
(if (null lis)
(if (null sublis)
acc
(append acc (list sublis)))
(let ((item (car lis)))
(if (funcall separator-p item)
(partition-list (cdr lis) separator-p ()
(if (null sublis)
acc
(append acc (list sublis))))
(partition-list (cdr lis) separator-p
(append sublis (list item))
acc)))))
(defun nth-op (n op arg)
(if (= n 0)
arg
(nth-op (- n 1) op (funcall op arg))))
(defun compose (f g &rest args)
(apply f (apply g args)))
(defun my-reduce (acc lis op)
(cond ((and (not (null acc))
(not (null lis)))
(cons (funcall op (car acc) (car lis))
(my-reduce (cdr acc) (cdr lis) op)))
((null lis) acc)
((null acc) lis)
(t nil)))
(defun merge-xlist (lis op acc)
(if (null lis)
acc
(merge-xlist (cdr lis) op
(my-reduce acc (car lis) op))))
(defun sudo-shell-command (command)
(let ((proc (start-process-shell-command
"*sudo*"
"*sudo*"
(concat "sudo bash -c "
(shell-quote-argument command)))))
(process-send-string proc
(read-passwd "Your Password:"))
(process-send-string proc "\r")
(process-send-eof proc)))
(defun find-file-by-search-dir (dir name)
(let ((find-cmd (concat "find "
dir
" -name \""
name "\""
" -print | head -n 1")))
(let ((data (shell-command-to-string find-cmd)))
(if (> (length data) 0)
(substring data 0 -1)
nil))))
(defun my-fold-left (op acc lst)
(if lst
(my-fold-left op (funcall op acc (car lst))
(cdr lst))
acc))
(defun my-replace-string (s old new)
(replace-regexp-in-string (regexp-quote old)
new s t t))
(defun my-zap-multiple-empty-lines ()
(while (re-search-forward "^\\([ ]*\n?\\)*\n")
(replace-match "\n")))
(defun first-n-elem (list n action)
(if (or (= n 0)
(not list))
nil
(let ((ele (car list))
(rem (cdr list)))
(if (funcall action ele)
(cons ele (first-n-elem rem (- n 1) action))
(first-n-elem rem n action)))))
;; define key bindings
;;