[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/m-buffer ce7b6aa0fc 001/115: Initial Checkin
From: |
ELPA Syncer |
Subject: |
[elpa] externals/m-buffer ce7b6aa0fc 001/115: Initial Checkin |
Date: |
Tue, 19 Jul 2022 15:58:44 -0400 (EDT) |
branch: externals/m-buffer
commit ce7b6aa0fc60fa87891d2e0a749b530104f63f99
Author: Phillip Lord <phillip.lord@newcastle.ac.uk>
Commit: Phillip Lord <phillip.lord@newcastle.ac.uk>
Initial Checkin
---
dev/.nosearch | 0
dev/m-buffer-test.el | 111 ++++++++++++++++++++++++++++++++
dev/match-data.txt | 6 ++
dev/with-temp-buffer.txt | 3 +
m-buffer.el | 164 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 284 insertions(+)
diff --git a/dev/.nosearch b/dev/.nosearch
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/dev/m-buffer-test.el b/dev/m-buffer-test.el
new file mode 100644
index 0000000000..eab3e8aaaf
--- /dev/null
+++ b/dev/m-buffer-test.el
@@ -0,0 +1,111 @@
+;;; m-buffer-test.el --- Tests for m-buffer
+
+;; The contents of this file are subject to the GPL License, Version 3.0.
+;;
+;; Copyright (C) 2014, Phillip Lord, Newcastle University
+;;
+;; 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, see <http://www.gnu.org/licenses/>.
+
+
+(require 'm-buffer)
+
+;;; Code:
+(defmacro with-temp-buffer-of-file (file &rest body)
+ "Run BODY in a temp buffer with the contents of FILE inserted."
+ `(with-temp-buffer
+ (insert-file-contents ,file)
+ ,@body))
+
+(ert-deftest m-with-temp-buffer-of-file ()
+ (should
+ (equal
+ "one\ntwo\nthree\n"
+ (with-temp-buffer-of-file
+ "with-temp-buffer.txt"
+ (buffer-string)))))
+
+(ert-deftest m-buffer-matches ()
+ (should
+ (= 3
+ (length
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (m-buffer-matches-data
+ (current-buffer)
+ "^one$")))))
+ (should
+ (-every?
+ 'markerp
+ (-flatten
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (m-buffer-matches-data
+ (current-buffer)
+ "^one$"))))))
+
+(ert-deftest m-buffer-matches-beginning ()
+ (should
+ (-every?
+ 'markerp
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (m-buffer-matches-beginning
+ (current-buffer)
+ "^one$")))))
+
+(ert-deftest m-buffer-matches-beginning-pos ()
+ (should
+ (equal
+ '(1 9 17)
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (m-buffer-matches-beginning-pos
+ (current-buffer)
+ "^one$")))))
+
+(ert-deftest m-buffer-nil-markers ()
+ (should
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (-all?
+ (lambda (marker)
+ (and
+ (marker-position marker)
+ (marker-buffer marker)))
+ (m-buffer-matches-beginning (current-buffer) "^one$"))))
+ (should
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (-all?
+ (lambda (marker)
+ (and
+ (not (marker-position marker))
+ (not (marker-buffer marker))))
+ (m-buffer-nil-markers
+ (m-buffer-matches-beginning (current-buffer) "^one$"))))))
+
+
+(ert-deftest replace-matches ()
+ (should
+ (equal
+ "three\ntwo\nthree\ntwo\nthree\ntwo\n"
+ (with-temp-buffer-of-file
+ "match-data.txt"
+ (m-buffer-replace-matches
+ (m-buffer-matches-data
+ (current-buffer) "^one$") "three")
+ (buffer-string)))))
+
+(provide 'm-buffer-test)
+;;; m-buffer-test.el ends here
diff --git a/dev/match-data.txt b/dev/match-data.txt
new file mode 100644
index 0000000000..96cc0db602
--- /dev/null
+++ b/dev/match-data.txt
@@ -0,0 +1,6 @@
+one
+two
+one
+two
+one
+two
diff --git a/dev/with-temp-buffer.txt b/dev/with-temp-buffer.txt
new file mode 100644
index 0000000000..4cb29ea38f
--- /dev/null
+++ b/dev/with-temp-buffer.txt
@@ -0,0 +1,3 @@
+one
+two
+three
diff --git a/m-buffer.el b/m-buffer.el
new file mode 100644
index 0000000000..67db2794f0
--- /dev/null
+++ b/m-buffer.el
@@ -0,0 +1,164 @@
+;;; m-buffer.el --- Buffer Manipulation Functions -*- lexical-binding: t -*-
+
+;; This file is not part of Emacs
+
+;; Author: Phillip Lord <phillip.lord@newcastle.ac.uk>
+;; Maintainer: Phillip Lord <phillip.lord@newcastle.ac.uk>
+;; Version: 0.1
+
+;; The contents of this file are subject to the GPL License, Version 3.0.
+;;
+;; Copyright (C) 2014, Phillip Lord, Newcastle University
+;;
+;; 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, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; This file provides a set of list orientated functions for operating over
+;; the contents of buffers. Functions are generally purish: i.e. they may
+;; change the state of one buffer by side-effect, but should not affect point,
+;; current buffer, match data or so forth. Generally, markers are returned
+;; rather than point locations, so that it is possible for example, to search
+;; for regexp matches, and then replace them all without the early replacement
+;; invalidating the location of the later ones.
+;;
+
+;;; Status:
+;;
+;; This library is early release at the moment. I write it become I got fed up
+;; with writing (while (re-search-forward) do-stuff) forms. I found that it
+;; considerably simplified writing `linked-buffer'. I make no guarantees about
+;; the API at the moment.
+
+;;; Code:
+(require 'dash)
+
+
+(defun m-buffer-matches-data (buffer regexp &optional beginning end)
+ "Return a list of markers to all matches in BUFFER to REGEXP.
+After use, call `m-buffer-nil-markers' to ensure that markers no longer
+point to anything which may otherwise slow buffer movement down."
+ (save-match-data
+ (save-excursion
+ (with-current-buffer
+ buffer
+ (let ((rtn nil))
+ (goto-char
+ (or beginning
+ (point-min)))
+ (while
+ (re-search-forward
+ regexp
+ (or end (point-max))
+ t)
+ (setq rtn
+ (cons
+ (match-data)
+ rtn)))
+ (reverse rtn))))))
+
+(defun m-buffer-matches-beginning-n (matches n)
+ "Given match-data returns beginning of nth group.
+Use `m-buffer-matches' to generate matches and
+`m-buffer-nil-markers' after the markers have been finished with
+or they will slow future use of the buffer."
+ (-map
+ (lambda (match)
+ (nth
+ (* 2 n) match))
+ matches))
+
+(defun m-buffer-matches-beginning (buffer regexp &optional beginning end)
+ "Returns a list of markers to the start of matches to regexp in buffer.
+Use `m-buffer-nil-markers' after the markers have been used or
+they will slow future changes to the buffer."
+ (-map
+ (lambda (match-data)
+ (car match-data))
+ (m-buffer-matches-data buffer regexp beginning end)))
+
+(defun m-buffer-matches-beginning-pos (buffer regexp &optional beginning end)
+ (m-buffer-markers-to-pos
+ (m-buffer-matches-beginning buffer regexp beginning end)))
+
+(defun m-buffer-matches-end-n (matches n)
+ "Given match-data returns end of nth group.
+Use `m-buffer-matches' to generate matches."
+ (-map
+ (lambda (match)
+ (nth
+ (+ 1 (* 2 n))
+ match))
+ matches))
+
+(defun m-buffer-matches-end (buffer regexp &optional beginning end)
+ "Returns a list of markers to the end of matches to regexp in buffer.
+Use `m-buffer-nil-markers' after the markers have been used or they will slow
+future changes to the buffer."
+ (-map
+ (lambda (match-data)
+ (nth 1 match-data))
+ (m-buffer-matches-data buffer regexp beginning end)))
+
+(defun m-buffer-matches-end-pos (buffer regexp &optional beginning end)
+ "Returns a list of positions of the end of matches in BUFFER to
+REGEXP."
+ (m-buffers-markers-to-pos
+ (m-buffer-matches-end buffer regexp beginning end)))
+
+;; marker/position utility functions
+(defun m-buffer-nil-markers (markers)
+ "Takes a (nested) list of markers and nils them all."
+ (-map
+ (lambda (marker)
+ (set-marker marker nil))
+ (-flatten markers)))
+
+(defun m-buffer-markers-to-pos (markers &optional postnil)
+ "Transforms a list of markers to a list of positions.
+If the markers are no longer needed, set postnil to true, or call
+`m-buffer-nil-markers' manually after use to speed future buffer
+movement."
+ (-map
+ (lambda (marker)
+ (prog1
+ (marker-position marker)
+ (when postnil
+ (set-marker marker nil))))
+ markers))
+
+(defun m-buffer-pos-to-markers (buffer positions)
+ "Translates a set of positions to markers."
+ (-map
+ (lambda (pos)
+ (set-marker
+ (make-marker) pos buffer))
+ positions))
+
+(defun m-buffer-replace-matches (matches replacement &optional subexp)
+ "Given a list of MATCHES, replace with REPLACEMENT.
+SUBEXP should be a number indicating the regexp group to replace."
+ (-map
+ (lambda (match)
+ (with-current-buffer
+ (marker-buffer (car match))
+ (save-match-data
+ (set-match-data match)
+ (replace-match
+ replacement nil nil nil
+ (or subexp 0)))))
+ matches))
+
+(provide 'm-buffer)
+;;; m-buffer.el ends here
- [elpa] branch externals/m-buffer created (now d2e35bf929), ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer ce7b6aa0fc 001/115: Initial Checkin,
ELPA Syncer <=
- [elpa] externals/m-buffer 1ecae2db10 007/115: Argument handling reworked., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer a3c5ab72c9 010/115: Added test file., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 20daafd37d 003/115: Lots more changes!, ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 48166c86e6 020/115: Whitespace changes., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer d4aeb66bb3 028/115: Removed pluralisation from nil-markers., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 710ae5ae1a 029/115: Second attempt to remove pluralisation from nil-markers, ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 0450374662 002/115: Lots of changes!, ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 76b65c025d 016/115: Test file for previous tests., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 94251c2abe 019/115: Package metadata added., ELPA Syncer, 2022/07/19
- [elpa] externals/m-buffer 2e1e7b5d29 021/115: Version Update., ELPA Syncer, 2022/07/19