[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/objed 2a06126 143/166: Add functions to map over object
From: |
Clemens Radermacher |
Subject: |
[elpa] externals/objed 2a06126 143/166: Add functions to map over objects |
Date: |
Sun, 29 Dec 2019 08:21:16 -0500 (EST) |
branch: externals/objed
commit 2a06126498f1e255c6a401222c29ca8db2b9c776
Author: Clemens Radermacher <address@hidden>
Commit: Clemens Radermacher <address@hidden>
Add functions to map over objects
---
objed-objects.el | 99 ++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 72 insertions(+), 27 deletions(-)
diff --git a/objed-objects.el b/objed-objects.el
index 8d1f057..bf82c83 100644
--- a/objed-objects.el
+++ b/objed-objects.el
@@ -631,13 +631,18 @@ OBJ is the object to use and defaults to
`objed--current-obj'."
pos
(objed--skip-forward pos 'ws))))
-(defun objed--collect-backward (pos min &optional ends)
+(defun objed--collect-backward (pos min &optional collf)
"Collect object positions in backward direction.
-Start from position POS and stop at MIN position. The resulting
-list contains cons cells of the start positions of the objects
-and the current window. If ENDS is non-nil collect end positions
-instead."
+Start from position POS and stop at MIN position.
+
+The returned list contains cons cells of the start positions of
+the objects and the current window. If COLLF is t collect the
+end positions instead. Leading or trailing ws are ignored by
+default.
+
+If COLLF is function it recieves the object as argument and
+should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
@@ -650,20 +655,30 @@ instead."
(not (equal obj sobj)))
(setq sobj obj)
(goto-char (setq pos (objed--beg obj)))
- (push (cons (if ends
- (objed--skip-backward
- (objed--max obj) 'ws)
- (objed--skip-forward pos 'ws))
- cw)
- posns))
+ (cond ((and collf
+ (not (eq collf t)))
+ (push (funcall collf obj) posns))
+ (t
+ (push (cons (if (eq collf t)
+ (objed--skip-backward
+ (objed--max obj) 'ws)
+ (objed--skip-forward pos 'ws))
+ cw)
+ posns))))
posns)))
-(defun objed--collect-forward (pos max)
+(defun objed--collect-forward (pos max &optional collf)
"Collect object positions in forward direction.
-Start from position POS and stop at MAX position. The resulting
-list contains cons cells of the start positions of the objects
-and the current window."
+Start from position POS and stop at MAX position.
+
+The returned list contains cons cells of the start positions of
+the objects and the current window. If COLLF is t collect the
+end positions instead. Leading or trailing ws are ignored by
+default.
+
+If COLLF is function it recieves the object as argument and
+should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
@@ -677,10 +692,16 @@ and the current window."
(setq sobj obj)
(if (objed--no-skipper-p)
(goto-char (setq pos (objed--beg obj)))
- (goto-char (setq pos (objed--end obj))))
- (push (cons (objed--skip-forward (objed--beg obj) 'ws)
- cw)
- posns)))
+ (goto-char (setq pos (objed--end obj))))
+ (cond ((and collf (not (eq collf t)))
+ (push (funcall collf obj) posns))
+ (t
+ (push (cons (if (eq collf t)
+ (objed--skip-backward
+ (objed--max obj) 'ws)
+ (objed--skip-forward (objed--beg obj) 'ws))
+ cw)
+ posns)))))
(setq posns (nreverse posns))))
(defun objed--no-skipper-p ()
@@ -688,7 +709,7 @@ and the current window."
(get (objed--name2func objed--object)
'objed-no-skip))
-(defun objed--collect-object-positions (beg end &optional fromp)
+(defun objed--collect-object-positions (beg end &optional fromp collf)
"Collect object positions.
Returns object positions between BEG and END.
@@ -696,18 +717,42 @@ Returns object positions between BEG and END.
If FROMP is non-nil collect from that position otherwise collect before
and after current object.
-The resulting list contains cons cells of the start positions of
-the objects and the current window."
+By default the returned list contains cons cells of the start
+positions of the objects and the current window.
+
+COLLF has the same meaning as for `objed--collect-forward' and
+`objed--collect-backward'."
(save-restriction
(narrow-to-region beg end)
(append (objed--collect-backward
(or fromp (objed--min))
- beg)
+ beg collf)
(objed--collect-forward
(or fromp (if (objed--no-skipper-p)
(objed--min) (objed--max)))
- end))))
+ end collf))))
+(defun objed--map (fun &optional obj beg end)
+ "Call FUN with object data for each object of current type.
+
+Return a list of the results.
+
+If OBJ is on-nil it should be the symbol of the object type to
+search for. By default search the whole buffer, alternatively
+provide BEG and END position for region to search."
+ (let ((objed--object (or obj objed--object)))
+ (objed--collect-object-positions
+ (or beg (point-min))
+ (or end (point-max))
+ (point) fun)))
+
+(defun objed--objects (&optional obj beg end)
+ "Return list of all objects of current type.
+
+If OBJ is on-nil it should be the symbol of the object type to
+search for. By default search the whole buffer, alternatively
+provide BEG and END position for region to search."
+ (objed--map #'identity obj beg end))
(defun objed--collect-object-lines ()
"Collect first lines of objects before and after current object.
@@ -936,7 +981,6 @@ Does return nil when there is no such object at point."
(objed-bounds-at-point obj state)))
-
;; * Object creation/manipulation
(defun objed-make-empty-object (&optional pos)
@@ -1130,8 +1174,9 @@ from end of object FROM."
(goto-char obj)
(goto-char (objed--max obj))))
(unless (eobp)
- (when (<= (point) (objed--beg))
- (objed--skip-ws))
+ (when (and (not (number-or-marker-p from))
+ (<= (point) (objed--beg obj)))
+ (objed--skip-ws))
(ignore-errors
(objed--object :try-next)
(objed--get))))))
- [elpa] externals/objed d3ab36c 108/166: Add kill-this-buffer to command alist, (continued)
- [elpa] externals/objed d3ab36c 108/166: Add kill-this-buffer to command alist, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed be4c3cd 114/166: Make indenting defuns repeatable, too, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 4a22002 120/166: Improve reformat command, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 9267f38 125/166: Improve open line command, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed cab7cee 130/166: Update hl init, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 7374a35 128/166: Improve sexp fallback, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 42a553e 139/166: Don't override M-o binding, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 4d1a445 141/166: Add public object bounds functions, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed b6b7c3f 144/166: Disable during a company completion, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 70cf23e 145/166: Merge pull request #51 from flatwhatson/fix-company, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 2a06126 143/166: Add functions to map over objects,
Clemens Radermacher <=
- [elpa] externals/objed fea1148 148/166: Fixes #54, identifier movement for symbols with regex chars, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed cc46438 146/166: Syntactic changes, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed fdea65d 151/166: Adding `objed-switch-alist` #57, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed c3cd6ab 150/166: Merge pull request #55 from r-darwish/patch-1, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed e079a76 152/166: Add objed-init-hook #57, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed a5684df 156/166: Add objed-switch-functions (#57), Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 18eb298 161/166: Add objed-define-local-object-keys*, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 878b5eb 154/166: Don't use obsolete var names, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed 5c4ac71 159/166: Improve `objed-define-object` docstring, Clemens Radermacher, 2019/12/29
- [elpa] externals/objed e5648b4 165/166: Ensure minibuffer commands reset even without objed-mode, Clemens Radermacher, 2019/12/29