[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] scratch/mheerdegen-preview 216fd06 07/32: WIP: New file el-search
From: |
Michael Heerdegen |
Subject: |
[elpa] scratch/mheerdegen-preview 216fd06 07/32: WIP: New file el-search/el-search-pp.el |
Date: |
Sat, 20 Oct 2018 18:18:58 -0400 (EDT) |
branch: scratch/mheerdegen-preview
commit 216fd0646246e9243283dbf7e1d223de41fd5c07
Author: Michael Heerdegen <address@hidden>
Commit: Michael Heerdegen <address@hidden>
WIP: New file el-search/el-search-pp.el
---
packages/el-search/el-search-pp.el | 133 +++++++++++++++++++++++++++++++++++++
packages/el-search/el-search.el | 15 +++--
2 files changed, 144 insertions(+), 4 deletions(-)
diff --git a/packages/el-search/el-search-pp.el
b/packages/el-search/el-search-pp.el
new file mode 100644
index 0000000..53112a3
--- /dev/null
+++ b/packages/el-search/el-search-pp.el
@@ -0,0 +1,133 @@
+;;; el-search-pp.el --- Further prettifications for pp with means of el-search
-*- lexical-binding:t -*-
+
+;; Copyright (C) 2018 Free Software Foundation, Inc
+
+;; Author: Michael Heerdegen <address@hidden>
+;; Maintainer: Michael Heerdegen <address@hidden>
+;; Created: 2018_01_14
+;; Keywords: lisp
+
+
+;; This file is not part of GNU Emacs.
+
+;; GNU Emacs 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.
+
+;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
+
+
+;;; Commentary:
+
+;; This files provides a minor mode `el-search-pretty-pp-mode' that
+;; enhances pp.el to produce even prettier results. Since
+;; el-search-query-replace uses pp to format replacement, this has
+;; also an effect on the insertions done by this command.
+;;
+;;
+;; Bugs, Known Limitations:
+;;
+;; This doesn't work with `cl-print'ed contents
+
+
+
+;;; Code:
+
+(require 'el-search)
+(require 'el-search-x)
+
+(defun el-search-prettify-let-likes ()
+ ;; Remove possible line break directly after the macro name
+ (let ((let-like-matcher (el-search-make-matcher
el-search--match-let-like-pattern)))
+ (save-excursion
+ (while (el-search--search-pattern-1 let-like-matcher t)
+ (when (looking-at "(\\(\\_<\\(\\w\\|\\s_\\)+\\_>\\*?\\) *\n")
+ (save-excursion
+ (goto-char (match-end 1))
+ (delete-region
+ (point)
+ (progn (skip-chars-forward " \t\n") (point)))
+ (insert " "))
+ (indent-sexp))
+ (el-search--skip-expression nil 'read)))))
+
+(defun el-search-prettify-let-like-bindings ()
+ (let ((let-like-binding-matcher (el-search-make-matcher '(and
(let-like-binding) `(,_ ,_)))))
+ (save-excursion
+ (while (el-search--search-pattern-1 let-like-binding-matcher t)
+ (let ((deleted-line-break nil))
+ (save-excursion
+ (when (setq deleted-line-break
+ (progn (down-list 1)
+ (goto-char (scan-sexps (point) 1))
+ (looking-at "[\s\t]*\n[\s\t]+")))
+ (delete-region (match-beginning 0) (match-end 0))
+ (insert " ")))
+ (when deleted-line-break (indent-sexp))
+ (el-search--skip-expression nil 'read))))))
+
+(defun el-search-prettify-huge-lists ()
+ (save-excursion
+ (while (el-search--search-pattern-1 (el-search-make-matcher '(pred listp))
t nil)
+ (pcase-let ((`(,this-list ,bound) (save-excursion (list (el-search-read
(current-buffer))
+ (copy-marker
(point))))))
+ (when (or (and
+ (null (cdr (last this-list))) ;FIXME: what about dotted or
circular lists?
+ (nthcdr 10 this-list)
+ (not (cl-every (lambda (elt) (and (atom elt) (not (stringp
elt))))
+ this-list)))
+ (< 60 (- bound (point))))
+ (save-excursion
+ (down-list 1)
+ (while (el-search-forward '_ bound t)
+ (goto-char (scan-sexps (point) 1))
+ (unless (or (looking-at "$") (not (save-excursion
(el-search-forward '_ bound t))))
+ (insert "\n"))))
+ (indent-sexp)))
+ (el-search--skip-expression nil 'read)))
+ (indent-sexp))
+
+(defun el-search-prettify-tiny-lists ()
+ (save-excursion
+ (while (el-search--search-pattern-1 (el-search-make-matcher '(pred listp))
t nil)
+ (pcase-let ((bound (copy-marker (scan-sexps (point) 1))))
+ (when (and (< (count-matches "[^[:space:]]" (point) bound) 45)
+ (save-excursion (search-forward-regexp "\n" bound t)))
+ (save-excursion
+ (while (search-forward-regexp "\n[[:space:]]*" bound t)
+ (replace-match " ")))
+ (indent-sexp)))
+ (el-search--skip-expression nil 'read)))
+ (indent-sexp))
+
+
+(defvar el-search-prettify-functions
+ '(el-search-prettify-let-likes
+ el-search-prettify-let-like-bindings
+ el-search-prettify-huge-lists
+ el-search-prettify-tiny-lists))
+
+(defgroup el-search-pp '() "Doc..." :group 'el-search)
+
+(defcustom el-search-pretty-pp nil
+ "Doc..."
+ :type 'boolean)
+
+(defun el-search-pp-buffer ()
+ (emacs-lisp-mode)
+ (goto-char (point-min))
+ (mapc (lambda (fun) (save-excursion (funcall fun)))
+ el-search-prettify-functions))
+
+
+(provide 'el-search-pp)
+
+;;; el-search-pp.el ends here
+
diff --git a/packages/el-search/el-search.el b/packages/el-search/el-search.el
index 297af5e..efb9033 100644
--- a/packages/el-search/el-search.el
+++ b/packages/el-search/el-search.el
@@ -433,9 +433,6 @@
;; (ambiguous reader syntaxes; lost comments, comments that can't
;; non-ambiguously be assigned to rewritten code)
;;
-;; - There could be something much better than pp to format the
-;; replacement, or pp should be improved.
-;;
;;
;; NEWS:
;;
@@ -769,11 +766,21 @@ nil."
(read stream)))
#'read))
+(defvar el-search-pretty-pp)
+(declare-function el-search-pp-buffer 'el-search-pp)
+
(defun el-search--pp-to-string (expr)
(let ((print-length nil)
(print-level nil)
(print-circle nil))
- (string-trim-right (pp-to-string expr))))
+ (let ((result (pp-to-string expr)))
+ (when el-search-pretty-pp
+ (setq result
+ (with-temp-buffer
+ (insert result)
+ (el-search-pp-buffer)
+ (buffer-string))))
+ (string-trim-right result))))
(defun el-search--setup-minibuffer ()
(let ((inhibit-read-only t))
- [elpa] scratch/mheerdegen-preview 562b2db 18/32: WIP [el-search] Fix search setup when occur flag bound, (continued)
- [elpa] scratch/mheerdegen-preview 562b2db 18/32: WIP [el-search] Fix search setup when occur flag bound, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 1f46601 15/32: WIP: Additions to "Mb hints", Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview a8483cd 22/32: WIP: [el-search] Fine tune separator for splicing replace, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 5bdc539 27/32: WIP: Include leading comments in occur defun context, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview f11f566 31/32: WIP: Small fix in el-search--changed-files-in-repo, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview fc2c159 29/32: WIP: Fix C-A and C-J after finished single-buffer search, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview d6a3158 01/32: WIP: [el-search] Fix nested match issues in *El Occur*, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 74cc15a 08/32: WIP: New command 'el-search-repository', Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview e43d95b 10/32: WIP [el-search] Implement 'el-search-keyboard-quit', Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 163a7c7 03/32: WIP: Add package "sscell", Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 216fd06 07/32: WIP: New file el-search/el-search-pp.el,
Michael Heerdegen <=
- [elpa] scratch/mheerdegen-preview 4f3d9bf 11/32: WIP [el-search] Add quick help command, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 5b994a5 17/32: WIP [el-search] Fix C-j with numeric arg in error case, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 54e3b67 16/32: WIP: Optimize caching, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 3aa418f 20/32: WIP: Improvements for change and changed, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 8c8e364 30/32: WIP: Small fix in 'el-search--reset-wrap-flag', Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 009dc4d 32/32: WIP: [el-search] Enhance doc of el-search-occur-mode, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview 4ce7e1d 06/32: WIP: Add el-search-hi-lock.el, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview e4bf180 25/32: WIP: [el-search] Some minor tweaks, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview df185f6 24/32: WIP: Test: Make mouse clicks not abort the search, Michael Heerdegen, 2018/10/20
- [elpa] scratch/mheerdegen-preview a1b1039 21/32: WIP: More colorful match count, Michael Heerdegen, 2018/10/20