Skip to content

Commit

Permalink
Config segments with customizable variable instead of toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
gexplorer committed Oct 29, 2020
1 parent 08197f4 commit f0b983b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A simple mode-line configuration for Emacs.
* Simple design
* Lightweight
* Split layout with left and right align segments
* Customization option for modeline segments
* Several predefined segments:
- Modified/readonly indicator
- Buffer name
Expand All @@ -20,7 +21,6 @@ A simple mode-line configuration for Emacs.
- Process info
- Input method
- Major mode
* Simple creation of new segments with a macro

# Configuration

Expand Down
42 changes: 21 additions & 21 deletions simple-modeline-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@
(defvar simple-modeline--default-mode-line mode-line-format
"The former value of `mode-line-format'.")

;;
;; Options
;;

(defcustom simple-modeline-segments
'((simple-modeline-segment-modified
simple-modeline-segment-buffer-name
simple-modeline-segment-position)
(simple-modeline-segment-minor-modes
simple-modeline-segment-input-method
simple-modeline-segment-eol
simple-modeline-segment-encoding
simple-modeline-segment-vc
simple-modeline-segment-misc-info
simple-modeline-segment-process
simple-modeline-segment-major-mode))
"Simple modeline segments."
:type '(list (repeat :tag "Left aligned" function)
(repeat :tag "Right aligned" function))
:package-version '(simple-modeline . "1.2"))

;;
;; Faces
;;
Expand Down Expand Up @@ -61,27 +82,6 @@
;; Helpers
;;

(defmacro simple-modeline-create-segment (name doc &rest body)
"Create a new segment with NAME, DOC and BODY function for `simple-modeline-mode'."
(let ((segment (intern (format "simple-modeline-segment-%s" name)))
(toggle (intern (format "simple-modeline-toggle-%s" name)))
(show (intern (format "simple-modeline-show-%s" name))))
`(progn
(defcustom ,show t
,(format "Visibility of the %s segment of the mode-line." name)
:group 'simple-modeline
:type 'boolean)
(defun ,toggle ()
,(format "Toggle visibility of %s segment of the mode-line." name)
(interactive)
(customize-save-variable (quote ,show) (not ,show)))
(defalias
(quote ,segment)
(lambda ()
(when ,show
,@body))
,doc))))

(defun simple-modeline--format (left-segments right-segments)
"Return a string of `window-width' length containing LEFT-SEGMENTS and RIGHT-SEGMENTS, aligned respectively."
(let* ((left (simple-modeline--format-segments left-segments))
Expand Down
78 changes: 33 additions & 45 deletions simple-modeline-segments.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

;;; Code:

(require 'simple-modeline-core)
(require 'subr-x)

(defun simple-modeline-make-mouse-map (mouse function)
Expand All @@ -32,37 +31,34 @@ corresponding to the mode line clicked."
(define-key map (vector 'mode-line mouse) function)
map))

(simple-modeline-create-segment
"modified"
"Displays a color-coded buffer modification/read-only indicator in the mode-line."
(if (not (string-match-p "\\*.*\\*" (buffer-name)))
(let* ((read-only (and buffer-read-only (buffer-file-name)))
(modified (buffer-modified-p)))
(propertize
(if read-only "" (if modified "" ""))
'face `(:inherit
,(if modified 'simple-modeline-status-modified
(if read-only 'simple-modeline-status-error
'simple-modeline-unimportant)))
'help-echo (format
"Buffer is %s and %smodified\nmouse-1: Toggle read-only status."
(if read-only "read-only" "writable")
(if modified "" "not "))
'local-map (purecopy (simple-modeline-make-mouse-map
'mouse-1
(lambda (event)
(interactive "e")
(with-selected-window (posn-window (event-start event))
(read-only-mode 'toggle)))))
'mouse-face 'mode-line-highlight))))

(simple-modeline-create-segment
"buffer-name"
(defun simple-modeline-segment-modified ()
"Displays a color-coded buffer modification/read-only indicator in the mode-line."
(if (not (string-match-p "\\*.*\\*" (buffer-name)))
(let* ((read-only (and buffer-read-only (buffer-file-name)))
(modified (buffer-modified-p)))
(propertize
(if read-only "" (if modified "" ""))
'face `(:inherit
,(if modified 'simple-modeline-status-modified
(if read-only 'simple-modeline-status-error
'simple-modeline-unimportant)))
'help-echo (format
"Buffer is %s and %smodified\nmouse-1: Toggle read-only status."
(if read-only "read-only" "writable")
(if modified "" "not "))
'local-map (purecopy (simple-modeline-make-mouse-map
'mouse-1
(lambda (event)
(interactive "e")
(with-selected-window (posn-window (event-start event))
(read-only-mode 'toggle)))))
'mouse-face 'mode-line-highlight))))

(defun simple-modeline-segment-buffer-name ()
"Displays the name of the current buffer in the mode-line."
(propertize " %b" 'face 'mode-line-buffer-id))

(simple-modeline-create-segment
"position"
(defun simple-modeline-segment-position ()
"Displays the current cursor position in the mode-line."
`((line-number-mode
((column-number-mode
Expand All @@ -83,8 +79,7 @@ corresponding to the mode line clicked."
(region-bounds))))
'font-lock-face 'font-lock-variable-name-face))))

(simple-modeline-create-segment
"vc"
(defun simple-modeline-segment-vc ()
"Displays color-coded version control information in the mode-line."
'(vc-mode vc-mode))

Expand All @@ -105,8 +100,7 @@ corresponding to the mode line clicked."
(purecopy map))
"Local keymap for the coding-system part of the simple-modeline.")

(simple-modeline-create-segment
"encoding"
(defun simple-modeline-segment-encoding ()
"Displays the encoding style of the buffer in the mode-line."
`(" "
,(propertize
Expand All @@ -122,8 +116,7 @@ corresponding to the mode line clicked."
'mouse-face 'mode-line-highlight
'local-map simple-modeline-segment-encoding-map)))

(simple-modeline-create-segment
"eol"
(defun simple-modeline-segment-eol ()
"Displays the EOL style of the current buffer in the mode-line."
(let* ((eol (coding-system-eol-type buffer-file-coding-system))
(mnemonic (pcase eol
Expand All @@ -150,15 +143,13 @@ corresponding to the mode line clicked."
(cond ((eq eol 0) 'dos) ((eq eol 1) 'mac) (t 'unix))))))))
'mouse-face 'mode-line-highlight)))

(simple-modeline-create-segment
"misc-info"
(defun simple-modeline-segment-misc-info ()
"Displays the current value of `mode-line-misc-info' in the mode-line."
(let ((misc-info (string-trim (format-mode-line mode-line-misc-info 'simple-modeline-unimportant))))
(unless (string= misc-info "")
(concat " " misc-info))))

(simple-modeline-create-segment
"input-method"
(defun simple-modeline-segment-input-method ()
"Displays the input-method of the buffer in the mode-line."
`(""
(current-input-method
Expand All @@ -175,22 +166,19 @@ corresponding to the mode line clicked."
(describe-current-input-method)))))
mouse-face 'mode-line-highlight))))

(simple-modeline-create-segment
"minor-modes"
(defun simple-modeline-segment-minor-modes ()
"Displays the current minor modes in the mode-line."
(replace-regexp-in-string
"%" "%%%%"
(format-mode-line minor-mode-alist)
t t))

(simple-modeline-create-segment
"process"
(defun simple-modeline-segment-process ()
"Displays the current value of `mode-line-process' in the mode-line."
(when mode-line-process
(concat " " (string-trim (format-mode-line mode-line-process)))))

(simple-modeline-create-segment
"major-mode"
(defun simple-modeline-segment-major-mode ()
"Displays the current major mode in the mode-line."
(propertize
(concat " "
Expand Down
15 changes: 3 additions & 12 deletions simple-modeline.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Author: Eder Elorriaga <gexplorer8@gmail.com>
;; URL: https://github.com/gexplorer/simple-modeline
;; Keywords: mode-line faces
;; Version: 1.1
;; Version: 1.2
;; Package-Requires: ((emacs "26.1"))

;; Copyright (C) 2019 Eder Elorriaga
Expand Down Expand Up @@ -46,17 +46,8 @@
(defvar simple-modeline--mode-line
'((:eval
(simple-modeline--format
'(simple-modeline-segment-modified
simple-modeline-segment-buffer-name
simple-modeline-segment-position)
'(simple-modeline-segment-minor-modes
simple-modeline-segment-input-method
simple-modeline-segment-eol
simple-modeline-segment-encoding
simple-modeline-segment-vc
simple-modeline-segment-misc-info
simple-modeline-segment-process
simple-modeline-segment-major-mode)))))
(car simple-modeline-segments)
(cadr simple-modeline-segments)))))

;;;###autoload
(define-minor-mode simple-modeline-mode
Expand Down

0 comments on commit f0b983b

Please sign in to comment.