branch: scratch/editorconfig commit caebcd482dff9e5532056f4965e7dd4973503d91 Author: Stefan Monnier <monn...@iro.umontreal.ca> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
Turn all the editorconfig-set-* into editorconfig-get-* --- editorconfig.el | 240 +++++++++++++++++++++++++++----------------------------- 1 file changed, 117 insertions(+), 123 deletions(-) diff --git a/editorconfig.el b/editorconfig.el index 57ceb51e9e..470c7b5d5a 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -47,13 +47,7 @@ (require 'cl-lib) -(eval-when-compile - (require 'rx) - (require 'subr-x) - (defvar tex-indent-basic) - (defvar tex-indent-item) - (defvar tex-indent-arg) - (defvar evil-shift-width)) +(eval-when-compile (require 'subr-x)) (require 'editorconfig-core) @@ -397,13 +391,11 @@ number - `lisp-indent-offset' is not set only if indent_size is (defcustom editorconfig-override-file-local-variables t "Non-nil means editorconfig will override file local variable values." - :type 'boolean - :group 'editorconfig) + :type 'boolean) (defcustom editorconfig-override-dir-local-variables t "Non-nil means editorconfig will override values defined in dir-locals.el ." - :type 'boolean - :group 'editorconfig) + :type 'boolean) (define-error 'editorconfig-error "Error thrown from editorconfig lib") @@ -481,8 +473,8 @@ See `editorconfig-lisp-use-default-indent' for details." t) -(defun editorconfig-set-indentation (style &optional size tab_width) - "Set indentation type from STYLE, SIZE and TAB_WIDTH." +(defun editorconfig-get-indentation (style &optional size tab_width) + "Get indentation vars according to STYLE, SIZE and TAB_WIDTH." (setq size (cond ((editorconfig-string-integer-p size) (string-to-number size)) @@ -491,51 +483,47 @@ See `editorconfig-lisp-use-default-indent' for details." (t nil))) - (cond ((not (editorconfig--should-set 'tab-width)) - nil) - (tab_width - (setq tab-width (string-to-number tab_width))) - ((numberp size) - (setq tab-width size))) - - (when (equal size "tab") - (setq size tab-width)) - - (cond ((not (editorconfig--should-set 'indent-tabs-mode)) - nil) - ((equal style "space") - (setq indent-tabs-mode nil)) - ((equal style "tab") - (setq indent-tabs-mode t))) - - (when size - (when (and (featurep 'evil) - (editorconfig--should-set 'evil-shift-width)) - (setq-local evil-shift-width size)) - (let ((parent major-mode) - entry) - ;; Find the closet parent mode of `major-mode' in - ;; `editorconfig-indentation-alist'. - (while (and (not (setq entry (assoc parent editorconfig-indentation-alist))) - (setq parent (get parent 'derived-mode-parent)))) - (when entry - (let ((fn-or-list (cdr entry))) - (cond ((functionp fn-or-list) - (pcase-dolist (`(,var . ,val) (funcall fn-or-list size)) - (when (editorconfig--should-set var size) - (set (make-local-variable var) val)))) - ((listp fn-or-list) - (dolist (elem fn-or-list) - (cond ((and (symbolp elem) - (editorconfig--should-set elem size)) - (set (make-local-variable elem) size)) - ((and (consp elem) - (editorconfig--should-set (car elem) size)) - (let ((spec (cdr elem))) - (set (make-local-variable (car elem)) - (cond ((functionp spec) (funcall spec size)) - ((integerp spec) (* spec size)) - (t spec)))))))))))))) + `(,@(cond (tab_width + `((tab-width . ,(string-to-number tab_width)))) + ;; FIXME: This seems wrong: `tab-width' controls the display width + ;; of TAB characters in the buffer, which is largely independent + ;; from the indentation step. + ((numberp size) + `((tab-width . ,size)))) + + ,@(when (equal size "tab") + (setq size tab-width) + nil) + + ,@(cond ((equal style "space") + `((indent-tabs-mode . nil))) + ((equal style "tab") + `((indent-tabs-mode . t)))) + + ,@(when (and size (featurep 'evil)) + `((evil-shift-width . ,size))) + ,@(when size + (let ((parent major-mode) + entry) + ;; Find the closet parent mode of `major-mode' in + ;; `editorconfig-indentation-alist'. + (while (and (not (setq entry (assoc parent editorconfig-indentation-alist))) + (setq parent (get parent 'derived-mode-parent)))) + (when entry + (let ((fn-or-list (cdr entry))) + (cond ((functionp fn-or-list) (funcall fn-or-list size)) + ((listp fn-or-list) + (mapcar + (lambda (elem) + (cond ((and (symbolp elem)) `(,elem . ,size)) + ((and (consp elem)) + `(,(car elem) + . ,(let ((spec (cdr elem))) + (cond ((functionp spec) + (funcall spec size)) + ((integerp spec) (* spec size)) + (t spec))))))) + fn-or-list))))))))) (defvar-local editorconfig--apply-coding-system-currently nil "Used internally.") @@ -594,49 +582,44 @@ This function will revert buffer when the coding-system has been changed." (revert-buffer-with-coding-system coding-system))) (setq editorconfig--apply-coding-system-currently nil))))) -;; FIXME: This is from Johan Sundström, who didn't sign the paperwork yet -;; (well, he didn't use `pcase'). Actually, not sure we need to -;; set both `require-final-newline' and `mode-require-final-newline'. -;;(defun editorconfig-set-trailing-nl (final-newline) -;; "Set up requiring final newline by FINAL-NEWLINE. -;; -;;This function will set `require-final-newline' and `mode-require-final-newline' -;;to non-nil when FINAL-NEWLINE is true." -;; (pcase final-newline -;; ("true" -;; ;; keep prefs around how/when the nl is added, if set - otherwise add on save -;; (setq-local require-final-newline (or require-final-newline t)) -;; (setq-local mode-require-final-newline (or mode-require-final-newline t))) -;; ("false" -;; ;; FIXME: Add functionality for actually REMOVING any trailing newlines here! -;; ;; (rather than just making sure we don't automagically ADD a new one) -;; (setq-local require-final-newline nil) -;; (setq-local mode-require-final-newline nil)))) - -(defun editorconfig-set-trailing-ws (trim-trailing-ws) - "Set up trimming of trailing whitespace at end of lines by TRIM-TRAILING-WS." - (make-local-variable 'write-file-functions) ;; just current buffer - (when (and (equal trim-trailing-ws "true") - (not buffer-read-only)) - ;; when true we push delete-trailing-whitespace (emacs > 21) - ;; to write-file-functions - (if editorconfig-trim-whitespaces-mode - (funcall editorconfig-trim-whitespaces-mode 1) - (add-to-list 'write-file-functions 'delete-trailing-whitespace))) - (when (or (equal trim-trailing-ws "false") - buffer-read-only) - ;; when false we remove every delete-trailing-whitespace - ;; from write-file-functions - (when editorconfig-trim-whitespaces-mode - (funcall editorconfig-trim-whitespaces-mode 0)) - (setq write-file-functions - (remove 'delete-trailing-whitespace write-file-functions)))) - -(defun editorconfig-set-line-length (length) - "Set the max line length (`fill-column') to LENGTH." +(defun editorconfig-get-trailing-nl (final-newline) + "Get the vars to require final newline according to FINAL-NEWLINE." + (pcase final-newline + ("true" + ;; Keep prefs around how/when the nl is added, if set - otherwise add on + ;; save + `((require-final-newline . ,(or require-final-newline t)) + ;; FIXME: Why do we set `mode-require-final-newline'? + (mode-require-final-newline . ,(or mode-require-final-newline t)))) + ("false" + ;; FIXME: Add functionality to actually REMOVE any trailing newlines here! + ;; (rather than just making sure we don't automagically ADD a new one) + `((require-final-newline . nil) + (mode-require-final-newline . nil))))) + +(defun editorconfig-get-trailing-ws (trim-trailing-ws) + "Get vars to trim of trailing whitespace according to TRIM-TRAILING-WS." + `(,@(when (and (equal trim-trailing-ws "true") + ;; FIXME: Test this in `before-save-hook'? + (not buffer-read-only)) + `((eval + . ,(if editorconfig-trim-whitespaces-mode + `(,editorconfig-trim-whitespaces-mode 1) + '(add-hook 'before-save-hook 'delete-trailing-whitespace nil t))))) + ,@(when (or (equal trim-trailing-ws "false") + buffer-read-only) + ;; Just do it right away rather than return a (VAR . VAL), which + ;; would be probably more trouble than it's worth. + (when editorconfig-trim-whitespaces-mode + (funcall editorconfig-trim-whitespaces-mode 0)) + (remove-hook 'before-save-hook #'delete-trailing-whitespace t) + nil))) + +(defun editorconfig-get-line-length (length) + "Get the max line length (`fill-column') to LENGTH." (when (and (editorconfig-string-integer-p length) (> (string-to-number length) 0)) - (setq fill-column (string-to-number length)))) + `((fill-column . ,(string-to-number length))))) (defun editorconfig--execute-editorconfig-exec (filename) @@ -721,15 +704,20 @@ This function also removes `unset' properties and calls when (equal v "unset") do (remhash k props)) props)) -(defun editorconfig-set-local-variables (props) - "Set buffer variables according to EditorConfig PROPS." - (editorconfig-set-indentation (gethash 'indent_style props) +(defun editorconfig-get-local-variables (props) + "Get variables settings according to EditorConfig PROPS." + (editorconfig-get-indentation (gethash 'indent_style props) (gethash 'indent_size props) (gethash 'tab_width props)) - (editorconfig-set-trailing-nl (gethash 'insert_final_newline props)) - (editorconfig-set-trailing-ws (gethash 'trim_trailing_whitespace props)) - (editorconfig-set-line-length (gethash 'max_line_length props))) + (editorconfig-get-trailing-nl (gethash 'insert_final_newline props)) + (editorconfig-get-trailing-ws (gethash 'trim_trailing_whitespace props)) + (editorconfig-get-line-length (gethash 'max_line_length props))) +(defun editorconfig-set-local-variables (props) + "Set buffer variables according to EditorConfig PROPS." + (pcase-dolist (`(,var . ,val) (editorconfig-get-local-variables props)) + (when (editorconfig--should-set var) + (set (make-local-variable var) val)))) (defun editorconfig-major-mode-hook () "Function to run when `major-mode' has been changed. @@ -859,6 +847,7 @@ F is that function, and FILENAME and ARGS are arguments passed to F." (defun editorconfig--get-coding-system (_size) + (defvar auto-coding-file-name) ;; Emacs≥30 (when (and (stringp auto-coding-file-name) (file-name-absolute-p auto-coding-file-name) ;; Don't recurse infinitely. @@ -974,7 +963,7 @@ To disable EditorConfig in some buffers, modify (advice-add 'insert-file-contents :around #'editorconfig--advice-insert-file-contents) (dolist (hook modehooks) (add-hook hook - 'editorconfig-major-mode-hook + #'editorconfig-major-mode-hook t))) (advice-remove 'find-file-noselect #'editorconfig--advice-find-file-noselect) (advice-remove 'insert-file-contents #'editorconfig--advice-insert-file-contents) @@ -989,9 +978,6 @@ To disable EditorConfig in some buffers, modify ;; (lm-version)) ;; "EditorConfig version.") -(declare-function find-library-name "find-func" (library)) -(declare-function lm-version "lisp-mnt" nil) - ;;;###autoload (defun editorconfig-version (&optional show-version) "Get EditorConfig version as string. @@ -999,20 +985,28 @@ To disable EditorConfig in some buffers, modify If called interactively or if SHOW-VERSION is non-nil, show the version in the echo area and the messages buffer." (interactive (list t)) - (let* ((version (with-temp-buffer - (require 'find-func) - (insert-file-contents (find-library-name "editorconfig")) - (require 'lisp-mnt) - (lm-version))) - (pkg (and (eval-and-compile (require 'package nil t)) - (cadr (assq 'editorconfig - package-alist)))) - (pkg-version (and pkg - (package-version-join (package-desc-version pkg)))) - (version-full (if (and pkg-version - (not (string= version pkg-version))) - (concat version "-" pkg-version) - version))) + ;; FIXME: Use `package-get-version'? + (let ((version-full + (if (fboundp 'package-get-version) + (package-get-version) + (let* ((version (with-temp-buffer + (require 'find-func) + (declare-function find-library-name + "find-func" (library)) + (insert-file-contents + (find-library-name "editorconfig")) + (require 'lisp-mnt) + (declare-function lm-version "lisp-mnt" nil) + (lm-version))) + (pkg (and (eval-and-compile (require 'package nil t)) + (cadr (assq 'editorconfig + package-alist)))) + (pkg-version (and pkg (package-version-join + (package-desc-version pkg))))) + (if (and pkg-version + (not (string= version pkg-version))) + (concat version "-" pkg-version) + version))))) (when show-version (message "EditorConfig Emacs v%s" version-full)) version-full))