ghc-mod/elisp/ghc-func.el

149 lines
4.2 KiB
EmacsLisp
Raw Normal View History

2010-01-06 05:38:06 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; ghc-func.el
;;;
;; Author: Kazu Yamamoto <Kazu@Mew.org>
;; Created: Sep 25, 2009
;;; Code:
2010-06-14 03:03:14 +00:00
(defvar ghc-module-command "ghc-mod"
"*The command name of \"ghc-mod\"")
2010-05-04 07:35:40 +00:00
(defvar ghc-ghc-options nil "*GHC options")
2010-05-04 07:35:40 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-01-06 05:38:06 +00:00
(defun ghc-replace-character (string from to)
"Replace characters equal to FROM to TO in STRING."
2010-03-10 07:51:42 +00:00
(let ((ret (copy-sequence string)))
(dotimes (cnt (length ret) ret)
(if (char-equal (aref ret cnt) from)
(aset ret cnt to)))))
2010-01-06 05:38:06 +00:00
2010-05-04 07:35:40 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro ghc-add (sym val)
`(setq ,sym (cons ,val ,sym)))
(defun ghc-set (vars vals)
(dolist (var vars)
(if var (set var (car vals))) ;; var can be nil to skip
(setq vals (cdr vals))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-05-04 08:52:16 +00:00
(defun ghc-filter (pred lst)
(let (ret)
(dolist (x lst (reverse ret))
(if (funcall pred x) (ghc-add ret x)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-01-06 05:38:06 +00:00
(defun ghc-which (cmd)
(catch 'loop
2010-03-30 03:05:35 +00:00
(dolist (suffix '("" ".exe"))
(let ((cmds (concat cmd suffix)))
(dolist (dir exec-path)
(let ((path (expand-file-name cmds dir)))
(if (file-exists-p path)
(throw 'loop path))))))))
2010-01-06 05:38:06 +00:00
2010-05-04 07:35:40 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-01-06 05:38:06 +00:00
(defun ghc-uniq-lol (lol)
(let ((hash (make-hash-table :test 'equal))
ret)
(dolist (lst lol)
(dolist (key lst)
(puthash key key hash)))
2010-05-04 07:35:40 +00:00
(maphash (lambda (key val) (ghc-add ret key)) hash)
2010-01-06 05:38:06 +00:00
ret))
2010-05-04 07:35:40 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-01-06 05:38:06 +00:00
(defun ghc-read-lisp (func)
(with-temp-buffer
(funcall func)
(goto-char (point-min))
(condition-case nil
(read (current-buffer))
(error ()))))
2010-05-04 03:49:22 +00:00
(defun ghc-read-lisp-list (func n)
(with-temp-buffer
(funcall func)
(goto-char (point-min))
(condition-case nil
(let ((m (set-marker (make-marker) 1 (current-buffer)))
ret)
2010-05-04 08:52:16 +00:00
(dotimes (i n (nreverse ret))
(ghc-add ret (read m))))
2010-05-04 03:49:22 +00:00
(error ()))))
2010-05-06 06:29:55 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ghc-mapconcat (func list)
(apply 'append (mapcar func list)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-05-06 06:29:55 +00:00
(defconst ghc-null 0)
(defconst ghc-newline 10)
2010-10-28 07:24:45 +00:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2012-01-23 06:12:24 +00:00
(defun ghc-things-at-point ()
(thing-at-point 'sexp))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2010-10-28 07:24:45 +00:00
(defun ghc-keyword-number-pair (spec)
(let ((len (length spec)) key ret)
(dotimes (i len (nreverse ret))
(setq key (intern (concat ":" (symbol-name (car spec)))))
(setq ret (cons (cons key i) ret))
(setq spec (cdr spec)))))
(defmacro ghc-defstruct (type &rest spec)
`(progn
(ghc-defstruct-constructor ,type ,@spec)
(ghc-defstruct-s/getter ,type ,@spec)))
(defmacro ghc-defstruct-constructor (type &rest spec)
`(defun ,(intern (concat "ghc-make-" (symbol-name type))) (&rest args)
(let* ((alist (quote ,(ghc-keyword-number-pair spec)))
(struct (make-list (length alist) nil))
key val key-num)
(while args ;; cannot use dolist
(setq key (car args))
(setq args (cdr args))
(setq val (car args))
(setq args (cdr args))
(unless (keywordp key)
(error "'%s' is not a keyword" key))
(setq key-num (assoc key alist))
(if key-num
(setcar (nthcdr (cdr key-num) struct) val)
(error "'%s' is unknown" key)))
struct)))
(defmacro ghc-defstruct-s/getter (type &rest spec)
`(let* ((type-name (symbol-name ',type))
(keys ',spec)
(len (length keys))
member-name setter getter)
(dotimes (i len)
(setq member-name (symbol-name (car keys)))
(setq setter (intern (format "ghc-%s-set-%s" type-name member-name)))
(fset setter (list 'lambda '(struct value) (list 'setcar (list 'nthcdr i 'struct) 'value) 'struct))
(setq getter (intern (format "ghc-%s-get-%s" type-name member-name)))
(fset getter (list 'lambda '(struct) (list 'nth i 'struct)))
(setq keys (cdr keys)))))
(defun ghc-make-ghc-options ()
(ghc-mapconcat (lambda (x) (list "-g" x)) ghc-ghc-options))
2010-01-06 05:38:06 +00:00
(provide 'ghc-func)