ghc-func.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;
  3. ;;; ghc-func.el
  4. ;;;
  5. ;; Author: Kazu Yamamoto <Kazu@Mew.org>
  6. ;; Created: Sep 25, 2009
  7. ;;; Code:
  8. (defvar ghc-module-command "ghc-mod"
  9. "*The command name of \"ghc-mod\"")
  10. (defvar ghc-ghc-options nil "*GHC options")
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. (defun ghc-replace-character (string from to)
  13. "Replace characters equal to FROM to TO in STRING."
  14. (let ((ret (copy-sequence string)))
  15. (dotimes (cnt (length ret) ret)
  16. (if (char-equal (aref ret cnt) from)
  17. (aset ret cnt to)))))
  18. (defun ghc-replace-character-buffer (from-c to-c)
  19. (let ((from (char-to-string from-c))
  20. (to (char-to-string to-c)))
  21. (save-excursion
  22. (goto-char (point-min))
  23. (while (search-forward from nil t)
  24. (replace-match to)))))
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. (defmacro ghc-add (sym val)
  27. `(setq ,sym (cons ,val ,sym)))
  28. (defun ghc-set (vars vals)
  29. (dolist (var vars)
  30. (if var (set var (car vals))) ;; var can be nil to skip
  31. (setq vals (cdr vals))))
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. (defun ghc-filter (pred lst)
  34. (let (ret)
  35. (dolist (x lst (reverse ret))
  36. (if (funcall pred x) (ghc-add ret x)))))
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. (defun ghc-uniq-lol (lol)
  39. (let ((hash (make-hash-table :test 'equal))
  40. ret)
  41. (dolist (lst lol)
  42. (dolist (key lst)
  43. (puthash key key hash)))
  44. (maphash (lambda (key val) (ghc-add ret key)) hash)
  45. ret))
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. (defun ghc-read-lisp (func)
  48. (with-temp-buffer
  49. (funcall func)
  50. (goto-char (point-min))
  51. (condition-case nil
  52. (read (current-buffer))
  53. (error ()))))
  54. (defun ghc-read-lisp-list (func n)
  55. (with-temp-buffer
  56. (funcall func)
  57. (goto-char (point-min))
  58. (condition-case nil
  59. (let ((m (set-marker (make-marker) 1 (current-buffer)))
  60. ret)
  61. (dotimes (i n (nreverse ret))
  62. (ghc-add ret (read m))))
  63. (error ()))))
  64. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. (defun ghc-mapconcat (func list)
  66. (apply 'append (mapcar func list)))
  67. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  68. (defconst ghc-null 0)
  69. (defconst ghc-newline 10)
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. (defun ghc-things-at-point ()
  72. (thing-at-point 'sexp))
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. (defun ghc-keyword-number-pair (spec)
  75. (let ((len (length spec)) key ret)
  76. (dotimes (i len (nreverse ret))
  77. (setq key (intern (concat ":" (symbol-name (car spec)))))
  78. (setq ret (cons (cons key i) ret))
  79. (setq spec (cdr spec)))))
  80. (defmacro ghc-defstruct (type &rest spec)
  81. `(progn
  82. (ghc-defstruct-constructor ,type ,@spec)
  83. (ghc-defstruct-s/getter ,type ,@spec)))
  84. (defmacro ghc-defstruct-constructor (type &rest spec)
  85. `(defun ,(intern (concat "ghc-make-" (symbol-name type))) (&rest args)
  86. (let* ((alist (quote ,(ghc-keyword-number-pair spec)))
  87. (struct (make-list (length alist) nil))
  88. key val key-num)
  89. (while args ;; cannot use dolist
  90. (setq key (car args))
  91. (setq args (cdr args))
  92. (setq val (car args))
  93. (setq args (cdr args))
  94. (unless (keywordp key)
  95. (error "'%s' is not a keyword" key))
  96. (setq key-num (assoc key alist))
  97. (if key-num
  98. (setcar (nthcdr (cdr key-num) struct) val)
  99. (error "'%s' is unknown" key)))
  100. struct)))
  101. (defmacro ghc-defstruct-s/getter (type &rest spec)
  102. `(let* ((type-name (symbol-name ',type))
  103. (keys ',spec)
  104. (len (length keys))
  105. member-name setter getter)
  106. (dotimes (i len)
  107. (setq member-name (symbol-name (car keys)))
  108. (setq setter (intern (format "ghc-%s-set-%s" type-name member-name)))
  109. (fset setter (list 'lambda '(struct value) (list 'setcar (list 'nthcdr i 'struct) 'value) 'struct))
  110. (setq getter (intern (format "ghc-%s-get-%s" type-name member-name)))
  111. (fset getter (list 'lambda '(struct) (list 'nth i 'struct)))
  112. (setq keys (cdr keys)))))
  113. (defun ghc-make-ghc-options ()
  114. (ghc-mapconcat (lambda (x) (list "-g" x)) ghc-ghc-options))
  115. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  116. (defconst ghc-error-buffer-name "*GHC Info*")
  117. (defun ghc-display (fontify ins-func)
  118. (let ((cdir default-directory)
  119. (buf (get-buffer-create ghc-error-buffer-name)))
  120. (with-current-buffer buf
  121. (erase-buffer)
  122. (funcall ins-func cdir)
  123. (ghc-replace-character-buffer ghc-null ghc-newline)
  124. (goto-char (point-min))
  125. (if (not fontify)
  126. (turn-off-haskell-font-lock)
  127. (haskell-font-lock-defaults-create)
  128. (turn-on-haskell-font-lock)))
  129. (display-buffer buf)))
  130. (provide 'ghc-func)