From 2d91f00b4f5a1f54176fbf4c4b2565fc96b69488 Mon Sep 17 00:00:00 2001 From: Sandra Snan Date: Sat, 18 Mar 2023 08:38:04 +0100 Subject: [PATCH] Add bc-maybe-set that only sets if there's no nearby crumb I still have the original bc-set bound to Ctrl+F2 but I found this useful for another key that I bound to #'(lambda () (interactive) (and (bc-maybe-set) (bc-previous)) (bc-previous)) --- breadcrumb.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/breadcrumb.el b/breadcrumb.el index 8c2fc99..2492151 100755 --- a/breadcrumb.el +++ b/breadcrumb.el @@ -44,6 +44,10 @@ The list is (Bookmark1 Bookmark2 ...) where each Bookmark is (TYPE FILENAME . PO "The current bookmark. `bc-next' and `bc-previous' would use this as the starting point." ) +(defvar *bc-threshold* 240 + "Threshold for how many characters counts as being too close to an existing bookmark for `bc-maybe-set'." + ) + (defvar *bc-bookmark-just-added* nil "Flag indicates a bookmark has just been added. `bc-next' and `bc-previous' use this to determine whether to increment or decrement." ) @@ -114,6 +118,13 @@ The list is (Bookmark1 Bookmark2 ...) where each Bookmark is (TYPE FILENAME . PO )))) ) +;;;###autoload +(defun bc-maybe-set () + "Set a bookmark at the current buffer and current position, unless there is a bookmark nearby already." + (interactive) + (if (bc-nearby-bookmarks-p) nil (and (bc-set) t)) + ) + ;;;###autoload (defun bc-previous () "Jump to the previous bookmark." @@ -278,6 +289,29 @@ INDEX the bookmark index (0-based) into the bookmark queue." bookmark)) ) +(defun bc-local-bookmarks () + "Get all local bookmarks." + (let* ((buffer-type (bc-get-buffer-type)) + (buffer-filename (bc-get-buffer-filename buffer-type))) + (cl-remove-if + (lambda (bookmark) + (not (and (equal buffer-type (bc-bookmark-type bookmark)) + (equal buffer-filename (bc-bookmark-filename bookmark)))) + ) + *bc-bookmarks*)) + ) + +(defun bc-nearby-bookmarks-p () + "Return true if there are any local bookmarks within *bc-threshold* characters from the current location." + (and + (let ((position (bc-get-buffer-position (bc-get-buffer-type)))) + (cl-remove-if + (lambda (bookmark) + (> (abs (- position (bc-bookmark-position bookmark))) *bc-threshold*)) + (bc-local-bookmarks))) + t) + ) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Bookmark current position functions