Skip to content

Commit

Permalink
Break out header from rg.el
Browse files Browse the repository at this point in the history
  • Loading branch information
dajva committed Jan 21, 2018
1 parent 974876d commit da8f380
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cask
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

(package-file "rg.el")

(files "rg.el")
(files "rg.el" "rg-ibuffer.el" "rg-header.el")

(development
(depends-on "cl-lib" "0.5")
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package-test:
-@rm -r /tmp/$(PKG_FULL_NAME)-elpa 2> /dev/null || true
cask package
PKG_FULL_NAME=$(PKG_FULL_NAME) emacs -batch -Q -l test/package-bootstrap.el \
-eval "(package-install-file \"dist/$(PKG_FULL_NAME).el\") (rg \"rg\" \"elisp\" \"/tmp/$(PKG_FULL_NAME)-elpa\"))"
-eval "(package-install-file \"dist/$(PKG_FULL_NAME).tar\") (rg \"rg\" \"elisp\" \"/tmp/$(PKG_FULL_NAME)-elpa\"))"

style-check:
cask emacs -batch -Q $(STYLE_CHECK) -f run-emacs-lisp-flycheck-and-exit $(SOURCES)
Expand Down
111 changes: 111 additions & 0 deletions rg-header.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
;;; rg-header.el --- Header for rg mode -*- lexical-binding: t; -*-

;; Copyright (C) 2017 David Landell <david.landell@sunnyhill.email>
;;
;; Author: David Landell <david.landell@sunnyhill.email>

;; This file is not part of GNU Emacs.

;; 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 3
;; of the License, 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; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.

;;; Commentary:

;;; Code:

(defcustom rg-show-header t
"Show header in results buffer if non nil."
:type 'boolean
:group 'rg)

(defface rg-toggle-on-face
'((t :inherit rg-file-tag-face))
"face for toggle 'on' text in header."
:group 'rg-face)

(defface rg-toggle-off-face
'((t :inherit rg-error-face))
"face for toggle 'off' text in header."
:group 'rg-face)

(defface rg-literal-face
'((t :inherit rg-filename-face))
"face for literal label in header."
:group 'rg-face)

(defface rg-regexp-face
'((t :inherit compilation-line-number))
"face for regexp label in header."
:group 'rg-face)

(defvar rg-last-search)

(defun rg-header-render-label (labelform)
"Return a fontified header label.
LABELFORM is either a string to render or a form where the `car' is a
conditional and the two following items are then and else specs.
Specs are lists where the the `car' is the labels string and the
`cadr' is font to use for that string."
(list '(:propertize "[" font-lock-face (header-line bold))
(cond
((stringp labelform)
`(:propertize ,labelform font-lock-face (header-line bold)))
((listp labelform)
(let* ((condition (nth 0 labelform))
(then (nth 1 labelform))
(else (nth 2 labelform)))
`(,condition
(:propertize ,(nth 0 then) font-lock-face (,(nth 1 then) header-line bold))
(:propertize ,(nth 0 else) font-lock-face (,(nth 1 else) header-line bold)))))
(t (error "Not a string or list")))
'(:propertize "]" font-lock-face (header-line bold))
'(": ")))

(defun rg-header-render-toggle (on)
"Return a fontified toggle symbol.
If ON is non nil, render \"on\" string, otherwise render \"off\"
string."
`(:eval (let* ((on ,on)
(value (if on "on " "off"))
(face (if on 'rg-toggle-on-face 'rg-toggle-off-face)))
(propertize value 'font-lock-face `(bold ,face)))))

(defun rg-create-header-line ()
"Create the header line if `rg-show-header' is enabled."
(when rg-show-header
(let ((itemspace " "))
(setq header-line-format
(if (null rg-last-search)
(list
(rg-header-render-label "command line")
"no refinement")
(list
(rg-header-render-label '(rg-literal ("literal" rg-literal-face)
("regexp" rg-regexp-face)))
'(:eval (nth 0 rg-last-search)) itemspace
(rg-header-render-label "files")
'(:eval (nth 1 rg-last-search)) itemspace
(rg-header-render-label "case")
(rg-header-render-toggle
'(not (member "-i" rg-toggle-command-line-flags))) itemspace
(rg-header-render-label "ign")
(rg-header-render-toggle
'(not (member "--no-ignore" rg-toggle-command-line-flags))) itemspace
(rg-header-render-label "hits")
'(:eval (format "%d" rg-hit-count))))))))

(provide 'rg-header)

;;; rg-header.el ends here
26 changes: 1 addition & 25 deletions rg.el
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
(require 'cl-lib)
(require 'edmacro)
(require 'grep)
(require 'rg-header)
(require 'rg-ibuffer)
(require 's)
(require 'vc-hooks)
Expand Down Expand Up @@ -144,11 +145,6 @@ NIL means case sensitive search will be forced."
:type 'string
:group 'rg)

(defcustom rg-show-header t
"Show header in results buffer if non nil."
:type 'boolean
:group 'rg)

(defvar rg-filter-hook nil
"Hook for new content in the rg buffer.
This hook is called every time the rg buffer has been updated with
Expand Down Expand Up @@ -200,26 +196,6 @@ a string describing how the process finished.")
"face for file tag in grouped layout"
:group 'rg-face)

(defface rg-toggle-on-face
'((t :inherit rg-file-tag-face))
"face for toggle 'on' text in header."
:group 'rg-face)

(defface rg-toggle-off-face
'((t :inherit rg-error-face))
"face for toggle 'off' text in header."
:group 'rg-face)

(defface rg-literal-face
'((t :inherit rg-filename-face))
"face for literal label in header."
:group 'rg-face)

(defface rg-regexp-face
'((t :inherit compilation-line-number))
"face for regexp label in header."
:group 'rg-face)


;; Internal vars
(defvar rg-builtin-type-aliases nil
Expand Down

0 comments on commit da8f380

Please sign in to comment.