ghc-mod/elisp/ghc-command.el

98 lines
2.6 KiB
EmacsLisp
Raw Normal View History

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; ghc-command.el
;;;
;; Author: Kazu Yamamoto <Kazu@Mew.org>
;; Created: Apr 13, 2010
;;; Code:
2014-03-25 06:23:08 +00:00
(require 'ghc-process)
(require 'ghc-check)
(defun ghc-insert-template ()
(interactive)
(cond
((bobp)
(ghc-insert-module-template))
2014-03-20 09:06:04 +00:00
((ghc-check-overlay-at (point))
(or (ghc-check-insert-from-warning)
(ghc-try-case-split)))
(t
(unless (ghc-try-case-split)
(message "Nothing to be done")))))
(defun ghc-insert-module-template ()
2014-03-25 06:23:08 +00:00
(let* ((fullname (file-name-sans-extension (buffer-file-name)))
(rootdir (ghc-get-project-root))
(len (length rootdir))
(name (substring fullname (1+ len)))
(file (file-name-sans-extension (buffer-name)))
(case-fold-search nil)
(mod (if (string-match "^[A-Z]" name)
(ghc-replace-character name ?/ ?.)
(if (string-match "^[a-z]" file)
"Main"
file))))
2014-03-25 07:02:26 +00:00
(while (looking-at "^{-#")
(forward-line))
(insert "module " mod " where\n")))
2014-03-25 06:23:08 +00:00
;; (defun ghc-capitalize (str)
;; (let ((ret (copy-sequence str)))
;; (aset ret 0 (upcase (aref ret 0)))
;; ret))
(defun ghc-sort-lines (beg end)
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(let ((inhibit-field-text-motion t))
(sort-subr nil 'forward-line 'end-of-line
(lambda ()
2015-08-27 01:42:28 +00:00
(re-search-forward "^import +\\(qualified\\)? *" nil t)
nil)
2014-03-25 06:57:59 +00:00
'end-of-line))
(ghc-merge-lines))))
(defun ghc-merge-lines ()
(let ((case-fold-search nil))
(goto-char (point-min))
(while (not (eolp))
2014-03-25 06:59:53 +00:00
;; qualified modlues are not merged at this moment.
;; fixme if it is improper.
2015-08-27 01:42:28 +00:00
(if (looking-at "^import +\\([A-Z][^ \n]+\\) *(\\(.*\\))$")
2014-03-25 06:57:59 +00:00
(let ((mod (match-string-no-properties 1))
(syms (match-string-no-properties 2))
(beg (point)))
(forward-line)
(ghc-merge-line beg mod syms))
(forward-line)))))
(defun ghc-merge-line (beg mod syms)
2015-08-27 01:42:28 +00:00
(let ((regex (concat "^import +" (regexp-quote mod) " *(\\(.*\\))$"))
2014-03-25 06:57:59 +00:00
duplicated)
(while (looking-at regex)
(setq duplicated t)
(setq syms (concat syms ", " (match-string-no-properties 1)))
(forward-line))
(when duplicated
(delete-region beg (point))
(insert "import " mod " (" syms ")\n"))))
(defun ghc-save-buffer ()
(interactive)
2014-03-20 07:21:48 +00:00
;; fixme: better way then saving?
2014-03-27 03:27:09 +00:00
(if ghc-check-command ;; hlint
(if (buffer-modified-p)
(call-interactively 'save-buffer))
2014-04-07 05:59:53 +00:00
(unless buffer-read-only
(set-buffer-modified-p t)
(call-interactively 'save-buffer)))
(ghc-check-syntax))
(provide 'ghc-command)