Move indentation segment to its own file

This commit is contained in:
Jessie Hildebrandt 2022-12-29 08:05:12 -05:00
parent 466a0292c9
commit 5c573fea9f
2 changed files with 213 additions and 139 deletions

View File

@ -0,0 +1,192 @@
;;; mood-line-segment-indentation.el --- An indentation info segment for mood-line -*- lexical-binding: t; -*-
;;
;; Author: Alynx Zhou <alynx.zhou@gmail.com>
;; Jessie Hildebrandt <jessieh.net>
;; Homepage: https://gitlab.com/jessieh/mood-line
;; This file is not part of GNU Emacs.
;;; Commentary:
;;
;;; License:
;;
;; This program 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 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Code:
;; -------------------------------------------------------------------------- ;;
;;
;; Custom definitions
;;
;; -------------------------------------------------------------------------- ;;
;; ---------------------------------- ;;
;; Variable definitions
;; ---------------------------------- ;;
;; Assembled from `editorconfig-indentation-alist' and `doom-modeline-indent-alist':
;; https://github.com/editorconfig/editorconfig-emacs/blob/b8043702f3d977db0e030c6c64ee4a810cad5f45/editorconfig.el#L175
;; https://github.com/seagle0128/doom-modeline/blob/fe9ee5a2a950f9ded10261a05a12adc577ae9e36/doom-modeline-core.el#L284
(defcustom mood-line-segment-indentation-mode-offset-alist
'((apache-mode apache-indent-level)
(awk-mode c-basic-offset)
(bpftrace-mode c-basic-offset)
(c++-mode c-basic-offset)
(c-mode c-basic-offset)
(cmake-mode cmake-tab-width)
(coffee-mode coffee-tab-width)
(cperl-mode cperl-indent-level)
(crystal-mode crystal-indent-level)
(csharp-mode c-basic-offset)
(css-mode css-indent-offset)
(d-mode c-basic-offset)
(emacs-lisp-mode lisp-indent-offset)
(enh-ruby-mode enh-ruby-indent-level)
(erlang-mode erlang-indent-level)
(ess-mode ess-indent-offset)
(f90-mode f90-associate-indent
f90-continuation-indent
f90-critical-indent
f90-do-indent
f90-if-indent
f90-program-indent
f90-type-indent)
(feature-mode feature-indent-offset
feature-indent-level)
(fsharp-mode fsharp-continuation-offset
fsharp-indent-level
fsharp-indent-offset)
(groovy-mode groovy-indent-offset)
(haskell-mode haskell-indent-spaces
haskell-indent-offset
haskell-indentation-layout-offset
haskell-indentation-left-offset
haskell-indentation-starter-offset
haskell-indentation-where-post-offset
haskell-indentation-where-pre-offset
shm-indent-spaces)
(haxor-mode haxor-tab-width)
(idl-mode c-basic-offset)
(jade-mode jade-tab-width)
(java-mode c-basic-offset)
(js-mode js-indent-level)
(js-jsx-mode js-indent-level sgml-basic-offset)
(js2-mode js2-basic-offset)
(js2-jsx-mode js2-basic-offset sgml-basic-offset)
(js3-mode js3-indent-level)
(json-mode js-indent-level)
(json-ts-mode json-ts-mode-indent-offset)
(julia-mode julia-indent-offset)
(kotlin-mode kotlin-tab-width)
(latex-mode tex-indent-basic)
(lisp-mode lisp-indent-offset)
(livescript-mode livescript-tab-width)
(lua-mode lua-indent-level)
(matlab-mode matlab-indent-level)
(meson-mode meson-indent-basic)
(mips-mode mips-tab-width)
(mustache-mode mustache-basic-offset)
(nasm-mode nasm-basic-offset)
(nginx-mode nginx-indent-level)
(nxml-mode nxml-child-indent)
(objc-mode c-basic-offset)
(octave-mode octave-block-offset)
(perl-mode perl-indent-level)
(php-mode c-basic-offset)
(pike-mode c-basic-offset)
(ps-mode ps-mode-tab)
(pug-mode pug-tab-width)
(puppet-mode puppet-indent-level)
(python-mode python-indent-offset)
(rjsx-mode js-indent-level sgml-basic-offset)
(ruby-mode ruby-indent-level)
(rust-mode rust-indent-offset)
(rustic-mode rustic-indent-offset)
(scala-mode scala-indent:step)
(scss-mode css-indent-offset)
(sgml-mode sgml-basic-offset)
(sh-mode sh-basic-offset sh-indentation)
(slim-mode slim-indent-offset)
(sml-mode sml-indent-level)
(tcl-mode tcl-indent-level
tcl-continued-indent-level)
(terra-mode terra-indent-level)
(typescript-mode typescript-indent-level)
(typescript-ts-base-mode typescript-ts-mode-indent-offset)
(verilog-mode verilog-indent-level
verilog-indent-level-behavioral
verilog-indent-level-declaration
verilog-indent-level-module
verilog-cexp-indent
verilog-case-indent)
(web-mode web-mode-attr-indent-offset
web-mode-attr-value-indent-offset
web-mode-code-indent-offset
web-mode-css-indent-offset
web-mode-markup-indent-offset
web-mode-sql-indent-offset
web-mode-block-padding
web-mode-script-padding
web-mode-style-padding)
(yaml-mode yaml-indent-offset))
"Alist mapping major mode names to their respective indent offset variables.
When multiple variables are specified for a given mode, the offset value will
be retrieved from the first variable that resolves to a value (evaluated in the
order provided)."
:group 'mood-line
:type '(alist :key-type symbol :value-type sexp))
;; -------------------------------------------------------------------------- ;;
;;
;; Indentation style info segment
;;
;; -------------------------------------------------------------------------- ;;
;; ---------------------------------- ;;
;; Segment function
;; ---------------------------------- ;;
(defun mood-line-segment-indentation--segment ()
"Display the indentation style of the current buffer."
(format "%s %s %d "
(if indent-tabs-mode "TAB" "SPC")
(let ((lookup-var
(seq-find (lambda (var) (and var (boundp var)))
(cdr (assoc major-mode
mood-line-segment-indentation-mode-offset-alist))
nil)))
;; If we don't know the indent offset variable of this major mode,
;; display a "-".
(if lookup-var
;; Some major modes, for example lisp-mode, works better when
;; lisp-indent-offset is nil, we use "-" for nil, too.
(if (not (null (symbol-value lookup-var)))
(format "%d" (symbol-value lookup-var))
"-")
"-"))
tab-width))
;; -------------------------------------------------------------------------- ;;
;;
;; Provide package
;;
;; -------------------------------------------------------------------------- ;;
(provide 'mood-line-segment-indentation)
;;; mood-line-segment-indentation.el ends here

View File

@ -71,6 +71,8 @@
(declare-function flymake-running-backends "flymake" ()) (declare-function flymake-running-backends "flymake" ())
(declare-function flymake-reporting-backends "flymake" ()) (declare-function flymake-reporting-backends "flymake" ())
(declare-function mood-line-segment-indentation--segment "mood-line-segment-indentation" ())
(declare-function mc/num-cursors "multiple-cursors" ()) (declare-function mc/num-cursors "multiple-cursors" ())
(declare-function string-blank-p "subr-x" (string)) (declare-function string-blank-p "subr-x" (string))
@ -169,123 +171,11 @@
;; Variable definitions ;; Variable definitions
;; ---------------------------------- ;; ;; ---------------------------------- ;;
(defcustom mood-line-show-indentation-information nil (defcustom mood-line-show-indentation-style nil
"When non-nil, show the indentation information of the current buffer." "When non-nil, show the indentation style of the current buffer."
:group 'mood-line :group 'mood-line
:type 'boolean) :type 'boolean)
;; Based on `editorconfig-indentation-alist' and `doom-modeline-indent-alist'.
;; See <https://github.com/editorconfig/editorconfig-emacs/blob/master/editorconfig.el#L175>.
;; See <https://github.com/seagle0128/doom-modeline/blob/master/doom-modeline-core.el#L282>.
(defcustom mood-line-indent-alist
'((apache-mode apache-indent-level)
(awk-mode c-basic-offset)
(bpftrace-mode c-basic-offset)
(c++-mode c-basic-offset)
(c-mode c-basic-offset)
(cmake-mode cmake-tab-width)
(coffee-mode coffee-tab-width)
(cperl-mode cperl-indent-level)
(crystal-mode crystal-indent-level)
(csharp-mode c-basic-offset)
(css-mode css-indent-offset)
(d-mode c-basic-offset)
(emacs-lisp-mode lisp-indent-offset)
(enh-ruby-mode enh-ruby-indent-level)
(erlang-mode erlang-indent-level)
(ess-mode ess-indent-offset)
(f90-mode f90-associate-indent
f90-continuation-indent
f90-critical-indent
f90-do-indent
f90-if-indent
f90-program-indent
f90-type-indent)
(feature-mode feature-indent-offset
feature-indent-level)
(fsharp-mode fsharp-continuation-offset
fsharp-indent-level
fsharp-indent-offset)
(groovy-mode groovy-indent-offset)
(haskell-mode haskell-indent-spaces
haskell-indent-offset
haskell-indentation-layout-offset
haskell-indentation-left-offset
haskell-indentation-starter-offset
haskell-indentation-where-post-offset
haskell-indentation-where-pre-offset
shm-indent-spaces)
(haxor-mode haxor-tab-width)
(idl-mode c-basic-offset)
(jade-mode jade-tab-width)
(java-mode c-basic-offset)
(js-mode js-indent-level)
(js-jsx-mode js-indent-level sgml-basic-offset)
(js2-mode js2-basic-offset)
(js2-jsx-mode js2-basic-offset sgml-basic-offset)
(js3-mode js3-indent-level)
(json-mode js-indent-level)
(json-ts-mode json-ts-mode-indent-offset)
(julia-mode julia-indent-offset)
(kotlin-mode kotlin-tab-width)
(latex-mode tex-indent-basic)
(lisp-mode lisp-indent-offset)
(livescript-mode livescript-tab-width)
(lua-mode lua-indent-level)
(matlab-mode matlab-indent-level)
(meson-mode meson-indent-basic)
(mips-mode mips-tab-width)
(mustache-mode mustache-basic-offset)
(nasm-mode nasm-basic-offset)
(nginx-mode nginx-indent-level)
(nxml-mode nxml-child-indent)
(objc-mode c-basic-offset)
(octave-mode octave-block-offset)
(perl-mode perl-indent-level)
(php-mode c-basic-offset)
(pike-mode c-basic-offset)
(ps-mode ps-mode-tab)
(pug-mode pug-tab-width)
(puppet-mode puppet-indent-level)
(python-mode python-indent-offset)
(rjsx-mode js-indent-level sgml-basic-offset)
(ruby-mode ruby-indent-level)
(rust-mode rust-indent-offset)
(rustic-mode rustic-indent-offset)
(scala-mode scala-indent:step)
(scss-mode css-indent-offset)
(sgml-mode sgml-basic-offset)
(sh-mode sh-basic-offset sh-indentation)
(slim-mode slim-indent-offset)
(sml-mode sml-indent-level)
(tcl-mode tcl-indent-level
tcl-continued-indent-level)
(terra-mode terra-indent-level)
(typescript-mode typescript-indent-level)
(typescript-ts-base-mode typescript-ts-mode-indent-offset)
(verilog-mode verilog-indent-level
verilog-indent-level-behavioral
verilog-indent-level-declaration
verilog-indent-level-module
verilog-cexp-indent
verilog-case-indent)
(web-mode web-mode-attr-indent-offset
web-mode-attr-value-indent-offset
web-mode-code-indent-offset
web-mode-css-indent-offset
web-mode-markup-indent-offset
web-mode-sql-indent-offset
web-mode-block-padding
web-mode-script-padding
web-mode-style-padding)
(yaml-mode yaml-indent-offset))
"Indentation retrieving variables matched to major modes.
Which is used when `mood-line-show-indentation-information' is non-nil.
When multiple variables are specified for a mode, they will be tried resolved
in the given order."
:group 'mood-line
:type '(alist :key-type symbol :value-type sexp))
(defcustom mood-line-show-eol-style nil (defcustom mood-line-show-eol-style nil
"When non-nil, show the EOL style of the current buffer." "When non-nil, show the EOL style of the current buffer."
:group 'mood-line :group 'mood-line
@ -528,6 +418,22 @@ Modal modes checked, in order: `evil-mode', `meow-mode', `god-mode'."
((featurep 'god-mode) ((featurep 'god-mode)
(mood-line-segment-modal-god)))) (mood-line-segment-modal-god))))
;; -------------------------------------------------------------------------- ;;
;;
;; Optional segments
;;
;; -------------------------------------------------------------------------- ;;
;; ---------------------------------- ;;
;; Indentation style
;; ---------------------------------- ;;
(defun mood-line-segment-indentation ()
"Display the indentation style of the current buffer (if enabled)."
(when mood-line-show-indentation-style
(require 'mood-line-segment-indentation)
(mood-line-segment-indentation--segment)))
;; -------------------------------------------------------------------------- ;; ;; -------------------------------------------------------------------------- ;;
;; ;;
;; Anzu segment ;; Anzu segment
@ -853,30 +759,6 @@ Checkers checked, in order: `flycheck', `flymake'."
(propertize " %p%% " (propertize " %p%% "
'face 'mood-line-unimportant))) 'face 'mood-line-unimportant)))
;; ---------------------------------- ;;
;; Indentation segment
;; ---------------------------------- ;;
(defun mood-line-segment-indent ()
"Display the indentation information of the current buffer."
(when mood-line-show-indentation-information
(format "%s %s %d "
(if indent-tabs-mode "TAB" "SPC")
(let ((lookup-var
(seq-find (lambda (var) (and var (boundp var)))
(cdr (assoc major-mode mood-line-indent-alist))
nil)))
;; If we don't know the indent offset variable of this major mode,
;; display a "-".
(if lookup-var
;; Some major modes, for example lisp-mode, works better when
;; lisp-indent-offset is nil, we use "-" for nil, too.
(if (not (null (symbol-value lookup-var)))
(format "%d" (symbol-value lookup-var))
"-")
"-"))
tab-width)))
;; ---------------------------------- ;; ;; ---------------------------------- ;;
;; EOL segment ;; EOL segment
;; ---------------------------------- ;; ;; ---------------------------------- ;;
@ -999,7 +881,7 @@ Checkers checked, in order: `flycheck', `flymake'."
;; Right ;; Right
(format-mode-line (format-mode-line
'((:eval (mood-line-segment-indent)) '((:eval (mood-line-segment-indentation))
(:eval (mood-line-segment-eol)) (:eval (mood-line-segment-eol))
(:eval (mood-line-segment-encoding)) (:eval (mood-line-segment-encoding))
(:eval (mood-line-segment-vc)) (:eval (mood-line-segment-vc))