diff --git a/highlight-indentation.el b/highlight-indentation.el index 463838c..003be70 100644 --- a/highlight-indentation.el +++ b/highlight-indentation.el @@ -35,7 +35,7 @@ major mode. This value is always used by `highlight-indentation-mode' if set buffer local. Set buffer local with `highlight-indentation-set-offset'" - :group 'highlight-indendation) + :group 'highlight-indentation) (defvar highlight-indentation-current-regex nil) @@ -51,23 +51,22 @@ on spaces" (set (make-local-variable 'highlight-indentation-current-regex) nil) (when highlight-indentation-mode ;; ON - (set (make-local-variable 'highlight-indentation-offset) - ;; Set indentation offset from highlight-indentation-offset if set, otherwise - ;; according to major mode - (cond ((local-variable-p 'highlight-indentation-offset) - highlight-indentation-offset) - ((eq major-mode 'python-mode) - (if (boundp 'python-indent) - python-indent - py-indent-offset)) - ((eq major-mode 'ruby-mode) - ruby-indent-level) - ((eq major-mode 'nxml-mode) - nxml-child-indent) - ((local-variable-p 'c-basic-offset) - c-basic-offset) - (t - (default-value 'highlight-indentation-offset)))) + (when (not (local-variable-p 'highlight-indentation-offset)) + (set (make-local-variable 'highlight-indentation-offset) + ;; Set indentation offset from highlight-indentation-offset if set, otherwise + ;; according to major mode + (cond ((eq major-mode 'python-mode) + (if (boundp 'python-indent) + python-indent + py-indent-offset)) + ((eq major-mode 'ruby-mode) + ruby-indent-level) + ((eq major-mode 'nxml-mode) + nxml-child-indent) + ((local-variable-p 'c-basic-offset) + c-basic-offset) + (t + (default-value 'highlight-indentation-offset))))) (set (make-local-variable 'highlight-indentation-current-regex) (format "\\( \\) \\{%s\\}" (- highlight-indentation-offset 1))) (font-lock-add-keywords nil `((,highlight-indentation-current-regex @@ -79,15 +78,63 @@ on spaces" "Set indentation offset localy in buffer, will prevent highlight-indentation from trying to guess indentation offset from major mode" - (interactive - (if (and current-prefix-arg (not (consp current-prefix-arg))) - (list (prefix-numeric-value current-prefix-arg)) - (list (read-number "Indentation offset: ")))) - (set (make-local-variable 'highlight-indentation-offset) offset) - (when highlight-indentation-mode - (highlight-indentation-mode))) + (interactive + (if (and current-prefix-arg (not (consp current-prefix-arg))) + (list (prefix-numeric-value current-prefix-arg)) + (list (read-number "Indentation offset: ")))) + (set (make-local-variable 'highlight-indentation-offset) offset) + (when highlight-indentation-mode + (highlight-indentation-mode))) + + +;;; +;;; Copyright (C) Kresten Krab Thorup +;;; Available under Apache License, Version 2. +;;; +;;; This minor mode will highlight the indentation of the current line +;;; as a vertical bar (grey background color) aligned with the column of the +;;; first character of the current line. +;;; +(defface highlight-indentation-current-column-face + ;; Fringe has non intrusive color in most color-themes + '((t :inherit fringe)) + "Basic face for highlighting indentation guides." + :group 'highlight-indentation) + +;; used to hold the last regex we installed +(defvar highlight-indentation-current-column-regex nil) + +;;;###autoload +(define-minor-mode + highlight-indentation-current-column-mode + "Hilight Indentation minor mode displays +a vertical bar corresponding to the indentation of the current line" + :lighter " |" + + (when highlight-indentation-current-column-regex + (font-lock-remove-keywords nil highlight-indentation-current-column-regex)) + + (set (make-local-variable 'highlight-indentation-current-column-regex) nil) + (cond (highlight-indentation-current-column-mode + (add-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook nil t)) + (t + (remove-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook t) + + (font-lock-fontify-buffer)))) + +(defun highlight-indentation-current-column-post-command-hook () + "This hook runs after every keystroke" + (when highlight-indentation-current-column-regex + (font-lock-remove-keywords nil highlight-indentation-current-column-regex)) + (let ((indent (save-excursion (back-to-indentation) (current-column)))) + (when (and highlight-indentation-current-column-mode + (> indent 1)) + (let* ((re (format "^ \\{%d\\}\\( \\)" indent)) + (arg `((,re (1 'highlight-indentation-current-column-face prepend))))) + (set (make-local-variable 'highlight-indentation-current-column-regex) arg) + (font-lock-add-keywords nil arg)))) + (font-lock-fontify-buffer)) (provide 'highlight-indentation) - + ;;; highlight-indentation.el ends here -