branch: externals/ilist
commit 5362c106a8dbbece798f670fbc1d406d2de2dd2e
Author: JSDurand <[email protected]>
Commit: JSDurand <[email protected]>
more functionalities
* ilist.el (ilist-map-lines): Fix a bug of not executing the function
on the first line in the range.
(ilist-get-index): Get the index of the element at point.
(ilist-delete-from-list): Return a copy of the list with a sub-list
removed. The sub-list is given as a list of indices.
---
ilist.el | 44 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
diff --git a/ilist.el b/ilist.el
index 4223a27615..466bcfd17f 100644
--- a/ilist.el
+++ b/ilist.el
@@ -531,7 +531,6 @@ over which the function is executed."
(setq end nil)))
(save-excursion
(goto-char (cond (start) ((point-min))))
- (ilist-forward-line 1)
(let (res)
(while (and (not (ilist-boundary-buffer-p t))
(or (null end)
@@ -546,6 +545,14 @@ over which the function is executed."
(ilist-forward-line 1))
(nreverse res))))
+;;; Get index at point
+
+(defun ilist-get-index ()
+ "Return the index of the element at point.
+If point is not at an element, return nil."
+ (declare (side-effect-free t))
+ (get-text-property (point) 'ilist-index))
+
;;; marks related
;; It is possible that some user-package does not need the
@@ -815,6 +822,41 @@ to the bottom of the buffer."
(ilist-forward-group-header
(- (prefix-numeric-value arg)) rounded))
+;;; Delete from ALIST
+
+(defun ilist-delete-from-list (ls elements)
+ "Remove ELEMENTS from LS.
+ELEMENTS are indices of elements to be removed in LS.
+
+Assumes that ELEMENTS is sorted, so that the larger indices come
+later.
+
+And the indices are zero-based.
+
+This does not modify LS or ELEMENTS. It returns a copy of LS
+with ELEMENTS removed."
+ (declare (pure t) (side-effect-free t))
+ ;; REVIEW: In our case, since both LS and ELEMENTS are sorted, we
+ ;; might have a faster implementation which employs the sorted-ness
+ ;; of the arguments, but I think it is pre-mature optimisation.
+ (let* ((temp (copy-tree ls)))
+ ;; NOTE: Using `mapc' is faster than a while loop, as the manual
+ ;; says. Since `dolist' is in essence a while loop, using `mapc'
+ ;; will be faster. Of course for our purposes this is premature
+ ;; optimisation.
+ (mapc
+ (lambda (index)
+ (cond
+ ((> index 0)
+ ;; Using `setcdr' is more efficient but destructively
+ ;; modifies the list. So we used `copy-tree' to prevent the
+ ;; destructions.
+ (setcdr (nthcdr (1- index) temp)
+ (nthcdr (1+ index) temp)))
+ ((setq temp (cdr temp)))))
+ (reverse elements))
+ temp))
+
;;; major mode
;; This major mode is the basis that should be derived by