Skip to content

Instantly share code, notes, and snippets.

@drvink
Last active May 14, 2018 20:41
Show Gist options
  • Save drvink/db5ddb101e18e3e94662 to your computer and use it in GitHub Desktop.
Save drvink/db5ddb101e18e3e94662 to your computer and use it in GitHub Desktop.
haskell-indentation-mode delete behavior prior to merge of hi2.el
;;; old-haskell-indentation-mode.el --- pre-hi2 behaviors
;; Keywords: languages
;;; Commentary:
;; haskell-indentation-mode delete behavior prior to hi2 merge
;;; Code:
(require 'cl-lib)
(require 'syntax)
(require 'haskell-indentation)
(defcustom haskell-indentation-delete-backward-indentation t
"Delete backward removes indentation."
:type 'boolean
:group 'haskell-indentation)
(defcustom haskell-indentation-delete-backward-jump-line nil
"Delete backward jumps to the previous line when removing last indentation."
:type 'boolean
:group 'haskell-indentation)
(defcustom haskell-indentation-delete-indentation t
"Delete removes indentation."
:type 'boolean
:group 'haskell-indentation)
(defcustom haskell-indentation-birdtrack-extra-space t
"Append a space after every birdtrack in literate mode."
:type 'boolean
:group 'haskell-indentation)
(eval-after-load "haskell-mode"
'(progn
(define-key haskell-mode-map (kbd "RET") 'haskell-newline-and-indent)
(define-key haskell-mode-map (kbd "DEL")
'haskell-indentation-delete-backward-char)
(define-key haskell-mode-map (kbd "<deletechar>")
'haskell-indentation-delete-char)))
(defun haskell-current-column ()
"Compute current column according to haskell syntax rules,
correctly ignoring composition."
(save-excursion
(let ((start (point))
(cc 0))
(beginning-of-line)
(while (< (point) start)
(if (= (char-after) ?\t)
(setq cc (* 8 (+ 1 (/ cc 8))))
(cl-incf cc))
(forward-char))
cc)))
(defun haskell-indentation-reindent (col)
(beginning-of-line)
(delete-region (point)
(progn
(when (and (haskell-indentation-birdp)
(eq (char-after) ?>))
(forward-char))
(skip-syntax-forward "-")
(point)))
(when (haskell-indentation-birdp)
(insert ">"))
(indent-to col))
(defun haskell-indentation-butlast (indentations)
(when (consp (cdr indentations))
(while (cddr indentations)
(setq indentations (cdr indentations))))
(car indentations))
(defun haskell-indentation-matching-indentation (col indentations)
"Find the leftmost indentation which is greater than or equal to COL."
(catch 'return
(while indentations
(if (or (<= col (car indentations))
(null (cdr indentations)))
(throw 'return (car indentations))
(setq indentations (cdr indentations))))
col))
(defun haskell-newline-and-indent ()
(interactive)
(if (haskell-indentation-bird-outside-codep)
(progn
(delete-horizontal-space)
(newline))
(catch 'parse-error
(newline)
(let* ((cc (haskell-current-column))
(ci (haskell-indentation-current-indentation))
(indentations (haskell-indentation-find-indentations)))
(skip-syntax-forward "-")
(if (prog1 (and (eolp)
(not (= (haskell-current-column) ci)))
(delete-horizontal-space)
(if (not (haskell-indentation-birdp))
(newline)
(when haskell-indentation-birdtrack-extra-space
(indent-to 2))
(newline)
(insert "> ")))
(haskell-indentation-reindent
(max (haskell-indentation-butlast indentations)
(haskell-indentation-matching-indentation
ci indentations)))
(haskell-indentation-reindent (haskell-indentation-matching-indentation
cc indentations)))))))
(defun haskell-indentation-delete-backward-char (n)
(interactive "p")
(catch 'parse-error
(cond
((haskell-indentation-bird-outside-codep)
(delete-char (- n)))
((and (use-region-p)
delete-active-region
(not (= (point) (mark))))
(delete-region (mark) (point)))
((or (= (haskell-current-column) 0)
(> (haskell-current-column) (haskell-indentation-current-indentation))
(nth 8 (syntax-ppss)))
(delete-char (- n)))
(haskell-indentation-delete-backward-indentation
(let* ((ci (haskell-indentation-current-indentation))
(pi (haskell-indentation-previous-indentation
ci (haskell-indentation-find-indentations))))
(save-excursion
(cond (pi
(move-to-column pi)
(delete-region (point)
(progn (move-to-column ci)
(point))))
(t
(if (not haskell-indentation-delete-backward-jump-line)
(delete-char (- 1))
(beginning-of-line)
(delete-region (max (point-min) (- (point) 1))
(progn (move-to-column ci)
(point)))))))))
(t (delete-char (- n))))))
(defun haskell-indentation-delete-char (n)
(interactive "p")
(if (haskell-indentation-bird-outside-codep)
(delete-char n)
(catch 'parse-error
(cond
((and delete-selection-mode
mark-active
(not (= (point) (mark))))
(delete-region (mark) (point)))
((and (haskell-indentation-birdp)
(looking-at "\n> "))
(delete-char (+ n 2)))
((or (eolp)
(>= (haskell-current-column) (haskell-indentation-current-indentation))
(nth 8 (syntax-ppss)))
(delete-char n))
(haskell-indentation-delete-indentation
(let* ((ci (haskell-indentation-current-indentation))
(pi (haskell-indentation-previous-indentation
ci (haskell-indentation-find-indentations))))
(save-excursion
(if (and pi (> pi (haskell-current-column)))
(move-to-column pi))
(delete-region (point)
(progn (move-to-column ci)
(point))))))
(t (delete-char (- n)))))))
(provide 'old-haskell-indentation-mode)
;;; old-haskell-indentation-mode.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment