Skip to content

Commit d3b6f4d

Browse files
committed
[Fix #911] Don't overwrite local user indentation settings
1 parent a1e318c commit d3b6f4d

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

lisp/ess-mode.el

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,10 @@ indentation style. See `ess-style-alist' for predefined styles."
311311
;; completion
312312
(add-hook 'comint-dynamic-complete-functions 'ess-complete-filename nil 'local)
313313
(delq t comint-dynamic-complete-functions)
314-
(set (make-local-variable 'comint-completion-addsuffix)
315-
(cons "/" ""))
316-
(add-hook 'hack-local-variables-hook #'ess--set-style-in-buffer nil t)
314+
(setq-local comint-completion-addsuffix (cons "/" ""))
315+
(add-hook 'hack-local-variables-hook 'ess-set-style nil t)
317316
(add-hook 'ess-idle-timer-functions 'ess-synchronize-dirs nil 'local))
318317

319-
(defun ess--set-style-in-buffer ()
320-
"Set `ess-style' taking into account file and directory local variables.
321-
This is added to `hack-local-variables-hook'."
322-
(setq-local ess-style (or (alist-get 'ess-style file-local-variables-alist)
323-
ess-style))
324-
(ess-set-style ess-style t))
325-
326318
(defun ess--get-mode-line-indicator ()
327319
"Get `ess--mode-line-process-indicator' from process buffer.
328320
Internal function to be used for dynamic mode-line dysplay in
@@ -430,31 +422,34 @@ current function."
430422

431423
;;; Formatting / indentation
432424

433-
(defun ess-set-style (&optional style quiet)
425+
(defun ess-set-style (&optional style _quiet)
434426
"Set up the `ess-mode' style variables from the `ess-style' variable.
435427
If STYLE argument is given, use that instead. It makes the ESS
436-
indentation style variables buffer local. When non-nil, QUIET
437-
suppresses messaging."
438-
(interactive)
439-
(let ((ess-styles (mapcar 'symbol-name (mapcar 'car ess-style-alist))))
440-
(unless style
441-
(setq style
442-
(intern (ess-completing-read "Set ESS mode indentation style"
443-
ess-styles nil t nil nil ess-style))))
444-
(setq style (or style ess-style))
445-
(make-local-variable 'ess-style)
446-
(if (memq (symbol-name style) ess-styles)
447-
(setq ess-style style)
448-
(error (format "Bad ESS style: %s" style)))
449-
(if (not quiet)
450-
(message "ESS-style: %s" ess-style))
451-
;; finally, set the indentation style variables making each one local
452-
(mapc (lambda (ess-style-pair)
453-
(make-local-variable (car ess-style-pair))
454-
(set (car ess-style-pair)
455-
(cdr ess-style-pair)))
456-
(cdr (assq ess-style ess-style-alist)))
457-
ess-style))
428+
indentation style variables buffer local. QUIET is for backward
429+
compatibility and is ignored."
430+
(interactive
431+
(list (let ((styles (mapcar (lambda (x) (symbol-name (car x)))
432+
ess-style-alist)))
433+
(intern (ess-completing-read
434+
"Set ESS mode indentation style"
435+
styles nil t nil nil ess-style)))))
436+
(let* ((keep-local (null style))
437+
(style (or style ess-style))
438+
(style-alist (or (cdr (assq style ess-style-alist))
439+
(error (format "Bad ESS style: %s" style))))
440+
(vars (if keep-local
441+
;; Install, but Keep user's buffer-local settings.
442+
(cl-loop for (var . _) in (cdr (assq 'DEFAULT ess-style-alist))
443+
unless (local-variable-p var)
444+
collect var)
445+
(mapcar #'car style-alist))))
446+
(when (called-interactively-p 'any)
447+
(message "ESS-style: %s" style))
448+
(mapc (lambda (var)
449+
(make-local-variable var)
450+
(set var (cdr (assq var style-alist))))
451+
vars)
452+
style))
458453

459454
(defun ess-indent-command (&optional whole-exp)
460455
"Indent current line as ESS code, or in some cases insert a tab character.

test/ess-test-indentation.el

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
,@(cdr (assq 'RStudio- ess-style-alist)))
5454
(C++
5555
,@(cdr (assq 'C++ ess-style-alist)))
56+
(DEFAULT
57+
,@(cdr (assq 'DEFAULT ess-style-alist)))
5658
(misc1
5759
(ess-indent-offset . 3)
5860
(ess-offset-block . open-delim)
@@ -86,7 +88,9 @@ where the edit took place. Return nil if E represents no real change.
8688
(with-current-buffer buffer
8789
(setq buffer-undo-list nil)
8890
(indent-region (point-min) (point-max))
89-
(not (buffer-modified-p buffer))))
91+
(if (buffer-modified-p buffer)
92+
(progn (switch-to-buffer buffer) nil)
93+
t)))
9094

9195
(defun ess-test-explain-change-on-indent (buffer)
9296
"Explainer function for `not-change-on-indent'."
@@ -106,6 +110,18 @@ where the edit took place. Return nil if E represents no real change.
106110

107111
(put 'not-change-on-indent 'ert-explainer 'ess-test-explain-change-on-indent)
108112

113+
;; Don't overwrite user settings (#911).
114+
(ert-deftest ess-r-no-user-style-overwrite-test ()
115+
(let ((ess-style 'RRR)
116+
(ess-r-mode-hook '((lambda () (setq-local ess-indent-offset 1)))))
117+
(with-temp-buffer
118+
(ess-r-mode)
119+
(should (eq ess-indent-offset 1))
120+
(ess-set-style)
121+
(should (eq ess-indent-offset 1))
122+
(ess-set-style 'C++)
123+
(should (eq ess-indent-offset 4)))))
124+
109125
(ert-deftest test-ess-R-indentation-RRR ()
110126
(ess-test-R-indentation "styles/RRR.R" 'RRR))
111127

test/ess-test-r-utils.el

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ split arbitrary."
145145
;; (buffer-substring-no-properties (point-min) (point-max))
146146
(replace-regexp-in-string
147147
prompt-regexp "> "
148-
(buffer-substring-no-properties (point-min) (point-max))))
149-
)
148+
(buffer-substring-no-properties (point-min) (point-max)))))
150149
(kill-process proc)
151150
;; fixme: kill in sentinel; this doesn't work in batch mode
152151
;; (kill-buffer (process-buffer proc))

test/ess-test-r.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ my_mean2 <- function(z){
500500

501501
(ert-deftest ess-r-comment-dwim-test ()
502502
"Test `comment-dwim' and Bug #434."
503-
(let ((ess-default-style 'RRR))
503+
(let ((ess-style 'RRR))
504504
(ess-r-test-with-temp-text ""
505505
(let ((ess-indent-with-fancy-comments t))
506506
(comment-dwim nil)

0 commit comments

Comments
 (0)