Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
318 lines (273 sloc) 11.5 KB
;;; hindent.el --- Indent haskell code using the "hindent" program
;; Copyright (c) 2014 Chris Done. All rights reserved.
;; Author: Chris Done <>
;; URL:
;; Package-Requires: ((cl-lib "0.5"))
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <>.
;;; Commentary:
;; Provides a minor mode and commands for easily using the "hindent"
;; program to reformat Haskell code.
;; Add `hindent-mode' to your `haskell-mode-hook' and use the provided
;; keybindings as needed. Set `hindent-reformat-buffer-on-save' to
;; `t' globally or in local variables to have your code automatically
;; reformatted.
;;; Code:
(require 'cl-lib)
;; Customization properties
(defgroup hindent nil
"Integration with the \"hindent\" reformatting program."
:prefix "hindent-"
:group 'haskell)
(defcustom hindent-style
"The style to use for formatting.
For hindent versions lower than 5, you must set this to a non-nil string."
:group 'hindent
:type 'string
:safe #'stringp)
(make-obsolete-variable 'hindent-style nil "hindent 5")
(defcustom hindent-process-path
"Location where the hindent executable is located."
:group 'hindent
:type 'string
:safe #'stringp)
(defcustom hindent-extra-args nil
"Extra arguments to give to hindent"
:group 'hindent
:type 'sexp
:safe #'listp)
(defcustom hindent-reformat-buffer-on-save nil
"Set to t to run `hindent-reformat-buffer' when a buffer in `hindent-mode' is saved."
:group 'hindent
:type 'boolean
:safe #'booleanp)
;; Minor mode
(defvar hindent-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap indent-region] #'hindent-reformat-region)
(define-key map [remap fill-paragraph] #'hindent-reformat-decl-or-fill)
"Keymap for `hindent-mode'.")
(define-minor-mode hindent-mode
"Indent code with the hindent program.
Provide the following keybindings:
:init-value nil
:keymap hindent-mode-map
:lighter " HI"
:group 'hindent
:require 'hindent
(if hindent-mode
(add-hook 'before-save-hook 'hindent--before-save nil t)
(remove-hook 'before-save-hook 'hindent--before-save t)))
(defun hindent--before-save ()
"Optionally reformat the buffer on save."
(when hindent-reformat-buffer-on-save
;; Interactive functions
(defun hindent-reformat-decl ()
"Re-format the current declaration.
The declaration is parsed and pretty printed. Comments are
preserved, although placement may be funky."
(let ((start-end (hindent-decl-points)))
(when start-end
(let ((beg (car start-end))
(end (cdr start-end)))
(hindent-reformat-region beg end t)))))
(defun hindent-reformat-buffer ()
"Reformat the whole buffer."
(hindent-reformat-region (point-min)
(defun hindent-reformat-decl-or-fill (justify)
"Re-format current declaration, or fill paragraph.
Fill paragraph if in a comment, otherwise reformat the current
declaration. When filling, the prefix argument JUSTIFY will
cause the text to be justified, as per `fill-paragraph'."
(interactive (progn
;; Copied from `fill-paragraph'
(list (if current-prefix-arg 'full))))
(if (hindent-in-comment)
(fill-paragraph justify t)
(defun hindent-reformat-region (beg end &optional drop-newline)
"Reformat the region from BEG to END, accounting for indentation.
If DROP-NEWLINE is non-nil, don't require a newline at the end of
the file."
(interactive "r")
(let ((inhibit-read-only t))
(if (= (save-excursion (goto-char beg)
(hindent-reformat-region-as-is beg end drop-newline)
(let* ((column (- beg (line-beginning-position)))
(string (buffer-substring-no-properties beg end))
(new-string (with-temp-buffer
(insert (make-string column ? ) string)
(hindent-reformat-region-as-is (point-min)
(delete-region (point-min) (1+ column))
(buffer-substring (point-min)
(goto-char beg)
(delete-region beg end)
(insert new-string))))))
(define-obsolete-function-alias 'hindent/reformat-decl 'hindent-reformat-decl)
;; Internal library
(defun hindent-reformat-region-as-is (beg end &optional drop-newline)
"Reformat the given region from BEG to END as-is.
This is the place where hindent is actually called.
If DROP-NEWLINE is non-nil, don't require a newline at the end of
the file."
(let* ((original (current-buffer))
(orig-str (buffer-substring-no-properties beg end)))
(let ((temp (current-buffer)))
(with-current-buffer original
(let ((ret (apply #'call-process-region
(append (list beg
nil ; delete
temp ; output
((= ret 1)
(let ((error-string
(with-current-buffer temp
(let ((string (progn (goto-char (point-min))
(buffer-substring (line-beginning-position)
(if (string= error-string "hindent: Parse error: EOF")
(message "language pragma")
(error error-string))))
((= ret 0)
(let* ((last-decl (= end (point-max)))
(new-str (with-current-buffer temp
(when (and drop-newline (not last-decl))
(goto-char (point-max))
(when (looking-back "\n" (1- (point)))
(delete-char -1)))
(if (not (string= new-str orig-str))
(let ((line (line-number-at-pos))
(col (current-column)))
(delete-region beg
(let ((new-start (point)))
(insert new-str)
(let ((new-end (point)))
(goto-char (point-min))
(forward-line (1- line))
(goto-char (+ (line-beginning-position) col))
(when (looking-back "^[ ]+" (line-beginning-position))
(delete-trailing-whitespace new-start new-end)))
(message "Formatted."))
(message "Already formatted.")))))))))))
(defun hindent-decl-points ()
"Get the start and end position of the current declaration.
This assumes that declarations start at column zero and that the
rest is always indented by one space afterwards, so Template
Haskell uses with it all being at column zero are not expected to
;; If we're in a block comment spanning multiple lines then let's
;; see if it starts at the beginning of the line (or if any comment
;; is at the beginning of the line, we don't care to treat it as a
;; proper declaration.
((and (hindent-in-comment)
(save-excursion (goto-char (line-beginning-position))
(goto-char (line-beginning-position))
(or (looking-at "^-}$")
(looking-at "^{-$")))
;; Otherwise we just do our line-based hack.
(let ((start
(or (cl-letf
(((symbol-function 'jump)
#'(lambda ()
(search-backward-regexp "^[^ \n]" nil t 1)
((save-excursion (goto-char (line-beginning-position))
(looking-at "|]"))
(t (unless (or (looking-at "^-}$")
(looking-at "^{-$"))
(goto-char (line-end-position))
(goto-char (1+ (point)))
(or (cl-letf
(((symbol-function 'jump)
#'(lambda ()
(when (search-forward-regexp "[\n]+[^ \n]" nil t 1)
((save-excursion (goto-char (line-beginning-position))
(looking-at "|]"))
(t (forward-char -1)
(search-backward-regexp "[^\n ]" nil t)
(cons start end))))))
(defun hindent-in-comment ()
"Are we currently in a comment?"
(when (and (= (line-end-position)
(/= (line-beginning-position) (point)))
(forward-char -1))
(elt (syntax-ppss) 4)
;; Pragmas {-# SPECIALIZE .. #-} etc are not to be treated as
;; comments, even though they are highlighted as such
(not (save-excursion (goto-char (line-beginning-position))
(looking-at "{-# "))))))
(defun hindent-extra-arguments ()
"Extra command line arguments for the hindent invocation."
(when (boundp 'haskell-language-extensions)
(when hindent-style
(list "--style" hindent-style))
(when hindent-extra-args
(provide 'hindent)
;;; hindent.el ends here
You can’t perform that action at this time.