[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] scratch/mheerdegen-preview 2f72331 08/35: WIP: New file el-search
From: |
Michael Heerdegen |
Subject: |
[elpa] scratch/mheerdegen-preview 2f72331 08/35: WIP: New file el-search/el-search-pp.el |
Date: |
Mon, 29 Oct 2018 22:24:05 -0400 (EDT) |
branch: scratch/mheerdegen-preview
commit 2f72331f59671aff5ecad33c613960e0c86050d8
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 | 135 +++++++++++++++++++++++++++++++++++++
packages/el-search/el-search.el | 15 +++--
2 files changed, 146 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..053401f
--- /dev/null
+++ b/packages/el-search/el-search-pp.el
@@ -0,0 +1,135 @@
+;;; 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 (and (not (macrop (car this-list))) ; FIXME: find a solution for
funs and macros
+ (or
+ (< 60 (- bound (point)))
+ (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)))))
+ (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 66a3556..8838e33 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] branch scratch/mheerdegen-preview created (now cdfaec4), Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 76163ac 01/35: WIP: [el-search] Fix an infloop, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview ee441a0 03/35: WIP: Add diverse "sloppy" pattern types, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 9805060 02/35: WIP: [el-search] Fix nested match issues in *El Occur*, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 220f349 04/35: WIP: Add package "sscell", Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview bef717d 06/35: WIP: New :key arg for "filename" and new pattern types "file" and "dir", Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview d2faca2 09/35: WIP: New command 'el-search-repository', Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 2f72331 08/35: WIP: New file el-search/el-search-pp.el,
Michael Heerdegen <=
- [elpa] scratch/mheerdegen-preview f2ec15d 13/35: WIP [el-search] Fix more "redundant _ pattern" cases, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview f025458 12/35: WIP [el-search] Add quick help command, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview f23fe5e 17/35: WIP: Optimize caching, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview b4b94b0 11/35: WIP [el-search] Implement 'el-search-keyboard-quit', Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 44715aa 05/35: WIP: New package "gnus-article-notes", Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 5057b57 14/35: WIP [el-search] Discourage using symbols as LPATS in `append' and `l', Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 5e2aea1 20/35: WIP [el-search] Adjust prev/next match commands for search and occur, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 38def8b 25/35: WIP: Test: Make mouse clicks not abort the search, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview feede7d 23/35: WIP: [el-search] Fine tune separator for splicing replace, Michael Heerdegen, 2018/10/29
- [elpa] scratch/mheerdegen-preview 82abecf 16/35: WIP: Additions to "Mb hints", Michael Heerdegen, 2018/10/29