Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Some improvement on ghc-show-type #480

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/emacs.piki
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
?C-cC-i
!Displays the info of this expression in another window.
?C-cC-t
!Displays the type of this expression in the minibuffer. Type C-cC-t multiple time to enlarge the expression.
!Displays the type of this expression in the minibuffer. Type C-cC-t multiple time to enlarge the expression. The selected region can be killed or copied by C-w or M-w respectively. The type string can be copied by M-t. The face of the region can be customized by "ghc-type-region" face.
?C-cC-a
!Selects one of possible cases.
?C-cC-f
Expand Down
37 changes: 36 additions & 1 deletion elisp/ghc-info.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,25 @@
;;; type
;;;

(defface ghc-type-region
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This face makes text under it quite unreadable, at least with my theme. How about we use an underline face or something less intrusive instead? Or maybe base the default colors for this theme on ones from the default faces of the current theme and modify them a bit.

'((default :inherit region)
(((background light))
:background "LightBlue")
(((background dark))
:background "RoyalBlue"))
"Face used to mark region selected by `ghc-show-type'.")

(defvar ghc-type-overlay nil)

(make-variable-buffer-local 'ghc-type-overlay)

(defvar ghc-type-map
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're overriding all these operations why not just set the region temporarily, that'll eliminate the type highlighting vs. region confusion. It looks like the region highlighting doesn't survive a C-c C-t invocation but C-x C-x C-x C-x will restore it. So if we restore the original region plus highlighting when the user moves point that should be even better.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably do that with push-mark and pop-mark.

(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-w") 'ghc-type-kill-region)
(define-key map (kbd "M-w") 'ghc-type-copy-region)
(define-key map (kbd "M-t") 'ghc-type-copy-type)
map))

(defun ghc-type-set-ix (n)
(overlay-put ghc-type-overlay 'ix n))

Expand All @@ -57,7 +72,8 @@

(defun ghc-type-init ()
(setq ghc-type-overlay (make-overlay 0 0))
(overlay-put ghc-type-overlay 'face 'region)
(overlay-put ghc-type-overlay 'face 'ghc-type-region)
(overlay-put ghc-type-overlay 'keymap ghc-type-map)
(ghc-type-clear-overlay)
(setq after-change-functions
(cons 'ghc-type-clear-overlay after-change-functions))
Expand Down Expand Up @@ -119,6 +135,25 @@
(while (search-forward "[Char]" nil t)
(replace-match "String"))))

(defun ghc-type-copy-region ()
"Copy the region selected by `ghc-show-type'."
(interactive)
(kill-new (filter-buffer-substring
(overlay-start ghc-type-overlay) (overlay-end ghc-type-overlay)))
(ghc-type-clear-overlay))

(defun ghc-type-kill-region ()
"Kill the region selected by `ghc-show-type'."
(interactive)
(kill-region (overlay-start ghc-type-overlay) (overlay-end ghc-type-overlay))
(ghc-type-clear-overlay))

(defun ghc-type-copy-type ()
"Copy the current type given by `ghc-show-type'."
(interactive)
(let ((tinfo (nth (ghc-type-get-ix) (ghc-type-get-types))))
(kill-new (ghc-tinfo-get-info tinfo))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Expanding Template Haskell
Expand Down