Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several improvements in file local variable usage #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
87 changes: 59 additions & 28 deletions dtrt-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
;;
;; Configuration settings used at this stage:
;; `dtrt-indent-max-merge-deviation'
;
;;
;; Final Evaluation
;;
;; Finally, dtrt-indent looks at the highest probability of all
Expand Down Expand Up @@ -154,6 +154,22 @@
;; - Files for which `dtrt-indent-explicit-offset' is true; this can be
;; - used in `.dir-locals.el' files, for example.
;;
;; Overriding analysis on a file or directory basis
;;
;; Some local variables, as set in a file comment, .dir-locals.el,
;; .dir-locals-2.el or using `dir-locals-set-class-variables'
;; can be used to override dtrt-indent behavior.
;;
;; If the local variable `dtrt-indent-force-offset' is set, this will override
;; the offset found from analysis. This will not disable any other processing
;; such as setting related variables.
;;
;; This differs from specifying particular variables e.g. c-basic-offset,
;; or `dtrt-indent-explicit-offset' which disable setting any related variables.
;;
;; If the local variable `indent-tabs-mode' is set, this will be used
;; for the value of indent-tabs-mode instead of the value determined from analysis.
;;
;; Limitations:
;;
;; - dtrt-indent can't deal well with files that use variable
Expand Down Expand Up @@ -194,14 +210,19 @@ adjusted transparently."
:lighter " dtrt-indent"
:group 'dtrt-indent
(if dtrt-indent-mode
(if (and (featurep 'smie) (not (eq smie-grammar 'unset)))
(progn
(when (null smie-config--buffer-local) (smie-config-guess))
(when dtrt-indent-run-after-smie
(dtrt-indent-try-set-offset)))
(dtrt-indent-try-set-offset))
(add-hook 'hack-local-variables-hook #'dtrt-indent-post-local-variable-setup t t)
(remove-hook 'hack-local-variables-hook #'dtrt-indent-post-local-variable-setup t)
(dtrt-indent-undo)))

(defun dtrt-indent-post-local-variable-setup ()
"Processing to be done after local variables have been processed"
(if (and (featurep 'smie) (not (eq smie-grammar 'unset)))
(progn
(when (null smie-config--buffer-local) (smie-config-guess))
(when dtrt-indent-run-after-smie
(dtrt-indent-try-set-offset)))
(dtrt-indent-try-set-offset)))

;;;###autoload
(define-globalized-minor-mode dtrt-indent-global-mode dtrt-indent-mode
(lambda ()
Expand Down Expand Up @@ -586,6 +607,9 @@ using more than 8 spaces per indentation level are very rare."
(make-variable-buffer-local
'dtrt-indent-explicit-tab-mode)

(defvar dtrt-indent-force-offset)
(make-variable-buffer-local 'dtrt-indent-force-offset)

(defun dtrt-indent--replace-in-string (haystack
needle-regexp
replacement)
Expand Down Expand Up @@ -899,7 +923,8 @@ merged with offset %s (%.2f%% deviation, limit %.2f%%)"
(indent-tabs-mode-setting
(cdr (assoc :indent-tabs-mode-setting result)))
(best-indent-offset
(nth 0 best-guess))
(or dtrt-indent-force-offset
(nth 0 best-guess)))
(indent-offset-variable
(nth 1 language-and-variable))
(indent-offset-variables
Expand All @@ -910,8 +935,9 @@ merged with offset %s (%.2f%% deviation, limit %.2f%%)"
(lambda (x)
(let ((mode (car x))
(variable (cadr x)))
(when (and (boundp mode)
(symbol-value mode))
(when (or (and (boundp mode)
(symbol-value mode))
(derived-mode-p mode))
variable)))
dtrt-indent-hook-generic-mapping-list))))
(indent-offset-names
Expand Down Expand Up @@ -939,9 +965,12 @@ Indentation offset set with file variable; not adjusted")
indent-offset-variables))
(when (>= dtrt-indent-verbosity 1)
(let ((offset-info
(format "%s adjusted to %s%s"
(format "%s adjusted to %s%s%s"
indent-offset-names
best-indent-offset
(if dtrt-indent-force-offset
" [forced]"
"")
(if (>= dtrt-indent-verbosity 2)
(format " (%.0f%%%% confidence)"
(* 100 confidence))
Expand All @@ -962,23 +991,25 @@ Indentation offset set with file variable; not adjusted")
(cond
((and change-indent-tabs-mode
(not (eq indent-tabs-mode indent-tabs-mode-setting)))
(when (>= dtrt-indent-verbosity 1)
(let ((tabs-mode-info
(when (and change-indent-tabs-mode
(not (eql indent-tabs-mode-setting
indent-tabs-mode)))
(format "indent-tabs-mode adjusted to %s"
indent-tabs-mode-setting))))
(message (concat "Note: " tabs-mode-info))))
; backup indent-tabs-mode setting
(setq dtrt-indent-original-indent
(cons
(let ((x 'indent-tabs-mode))
(list x (symbol-value x) (local-variable-p x)))
dtrt-indent-original-indent))
; actually adapt indent-tabs-mode
(set (make-local-variable 'indent-tabs-mode)
indent-tabs-mode-setting))
(if dtrt-indent-explicit-tab-mode
(message "File variable indent-tabs-mode used; no adjustment done");
(when (>= dtrt-indent-verbosity 1)
(let ((tabs-mode-info
(when (and change-indent-tabs-mode
(not (eql indent-tabs-mode-setting
indent-tabs-mode)))
(format "indent-tabs-mode adjusted to %s"
indent-tabs-mode-setting))))
(message (concat "Note: " tabs-mode-info))))
; backup indent-tabs-mode setting
(setq dtrt-indent-original-indent
(cons
(let ((x 'indent-tabs-mode))
(list x (symbol-value x) (local-variable-p x)))
dtrt-indent-original-indent))
; actually adapt indent-tabs-mode
(set (make-local-variable 'indent-tabs-mode)
indent-tabs-mode-setting)))
(t
(when (>= dtrt-indent-verbosity 2)
(message "Note: indent-tabs-mode not adjusted"))
Expand Down