Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 regels
4.0 KiB

4 jaren geleden
  1. ;;; haskell-collapse.el --- Collapse expressions -*- lexical-binding: t -*-
  2. ;; Copyright (c) 2014 Chris Done. All rights reserved.
  3. ;; Copyright (c) 2017 Vasantha Ganesh Kanniappan <vasanthaganesh.k@tuta.io>.
  4. ;; This file is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation; either version 3, or (at your option)
  7. ;; any later version.
  8. ;; This file is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'hideshow)
  16. ;;; TODO:
  17. ;;; -> Make it work for braces
  18. (defun haskell-hide-toggle ()
  19. "Toggle visibility of existing forms at point. "
  20. (interactive)
  21. (hs-minor-mode 1)
  22. (save-excursion
  23. (let* ((modified (buffer-modified-p))
  24. (inhibit-read-only t)
  25. (position (haskell-indented-block))
  26. (beg (car position))
  27. (end (cdr position)))
  28. (if (and beg end)
  29. (if (overlays-in beg end)
  30. (hs-discard-overlays beg end)
  31. (hs-make-overlay beg end 'code)))
  32. (set-buffer-modified-p modified))))
  33. (defun haskell-blank-line-p ()
  34. "Returns `t' if line is empty or composed only of whitespace."
  35. (save-excursion
  36. (beginning-of-line)
  37. (= (point-at-eol)
  38. (progn (skip-chars-forward "[:blank:]") (point)))))
  39. (defun haskell-indented-block ()
  40. "return (start-of-indentation . end-of-indentation)"
  41. (let ((cur-indent (current-indentation))
  42. (nxt-line-indent (haskell-next-line-indentation 1))
  43. (prev-line-indent (haskell-next-line-indentation -1))
  44. (beg-of-line (save-excursion (end-of-line)
  45. (point))))
  46. (cond ((and (= cur-indent 0)
  47. (= nxt-line-indent 0)) nil)
  48. ((haskell-blank-line-p) nil)
  49. ((> nxt-line-indent cur-indent)
  50. (cons beg-of-line
  51. (haskell-find-line-with-indentation '> 1)))
  52. ((or (= nxt-line-indent cur-indent)
  53. (<= prev-line-indent cur-indent))
  54. (cons (haskell-find-line-with-indentation '>= -1)
  55. (haskell-find-line-with-indentation '>= 1)))
  56. (t nil))))
  57. (defun haskell-next-line-indentation (dir)
  58. "returns (integer) indentation of the next if dir=1, previous line
  59. indentation if dir=-1"
  60. (save-excursion
  61. (progn
  62. (while (and (zerop (forward-line dir))
  63. (haskell-blank-line-p)))
  64. (current-indentation))))
  65. (defun haskell-find-line-with-indentation (comparison direction)
  66. "comparison is >= or >, direction if 1 finds forward, if -1 finds backward"
  67. (save-excursion
  68. (let ((start-indent (current-indentation)))
  69. (progn
  70. (while (and (zerop (forward-line direction))
  71. (or (haskell-blank-line-p)
  72. (funcall comparison (current-indentation) start-indent))))
  73. (when (= direction 1) (forward-line -1))
  74. (end-of-line)
  75. (point)))))
  76. (defun haskell-hide-toggle-all ()
  77. "hides all top level functions"
  78. (interactive)
  79. (save-excursion
  80. (goto-char (point-max))
  81. (while (zerop (forward-line -1))
  82. (goto-char (point-at-bol))
  83. (when (= (current-indentation) 0) (haskell-hide-toggle)))))
  84. (defvar haskell-collapse-mode-map
  85. (let ((map (make-sparse-keymap)))
  86. (define-key map (kbd "C-c @ C-c") 'haskell-hide-toggle)
  87. (define-key map (kbd "C-c @ C-M-c") 'haskell-hide-toggle-all)
  88. (define-key map (kbd "C-c @ C-M-s") 'haskell-hide-toggle-all)
  89. (define-key map (kbd "C-c @ C-M-h") 'haskell-hide-toggle-all)
  90. map)
  91. "Keymap for using `haskell-collapse-mode'.")
  92. ;;;###autoload
  93. (define-minor-mode haskell-collapse-mode
  94. "Minor mode to collapse and expand haskell expressions"
  95. :init-value nil
  96. :lighter " Haskell-Collapse"
  97. :keymap haskell-collapse-mode-map)
  98. (provide 'haskell-collapse)