emacs.d

My Emacs configuration
git clone https://git.jamzattack.xyz/emacs.d
Log | Files | Refs | LICENSE

custom-helm-bookmark.el (6346B)


      1 ;;; custom-helm-bookmark.el --- Quickly define sources for `helm-bookmark-jump'  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020  Jamie Beardslee
      4 
      5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz>
      6 ;; Keywords: convenience, helm, completion
      7 
      8 ;; This file is not part of GNU Emacs.
      9 
     10 ;; This program is free software; you can redistribute it and/or modify
     11 ;; it under the terms of the GNU General Public License as published by
     12 ;; the Free Software Foundation, either version 3 of the License, or
     13 ;; (at your option) any later version.
     14 
     15 ;; This program is distributed in the hope that it will be useful,
     16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 ;; GNU General Public License for more details.
     19 
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     22 
     23 ;;; Commentary:
     24 
     25 ;; This package defines a macro to create Helm bookmark sources
     26 ;; quickly and easily.  It also uses the macro to create a few sources
     27 ;; that I use.
     28 
     29 ;; Although the macro `helm-bookmark-create-source-please' defines all
     30 ;; the necessary functions and variables, they are not enabled by
     31 ;; `helm-bookmark-jump'.  The following is how I enable these:
     32 ;;
     33 ;; (custom-set-variables
     34 ;;  '(helm-bookmark-default-filtered-sources
     35 ;;    '(helm-source-bookmark-university
     36 ;;      helm-source-bookmark-config
     37 ;;      helm-source-bookmark-org-misc
     38 ;;      helm-source-bookmark-elisp
     39 ;;      helm-source-bookmark-downloads
     40 ;;      helm-source-bookmark-dired
     41 ;;      helm-source-bookmark-info
     42 ;;      helm-source-bookmark-man
     43 ;;      helm-source-bookmark-other
     44 ;;      helm-source-bookmark-set)))
     45 
     46 
     47 ;;; Code:
     48 
     49 (require 'helm-bookmark)
     50 
     51 (defmacro helm-bookmark-create-source-please (name docstring conditions)
     52   "Create a helm source for helm-filtered-bookmarks.
     53 
     54 NAME is a generic name for the source.
     55 
     56 DOCSTRING is the name used for (helm-make-source), so it is the
     57 name as seen by (helm-filtered-bookmarks).
     58 
     59 CONDITIONS is a single predicate that checks whether to add a
     60 bookmark to a source.  BOOKMARK is the name of the bookmark.  For
     61 convenience, FILENAME is the expanded file name of the bookmark.
     62 
     63 An example, for bookmarked info documents in (coreutils):
     64 (helm-bookmark-create-source-please
     65  coreutils \"Coreutils info pages\"
     66  (and (string-match-p
     67        \"\\(coreutils\\).*\"
     68        (car bookmark)) ; the first element of a bookmark record is its name
     69       (eq (bookmark-get-handler bookmark) 'Info-bookmark-jump)))
     70 
     71 It can then be added to the list `helm-bookmark-default-filtered-sources':
     72 (add-to-list 'helm-bookmark-default-filtered-sources 'helm-source-bookmark-coreutils)
     73 
     74 "
     75   (let* ((name-as-string (symbol-name name))
     76 	 (predicate-name (intern (concat "helm-bookmark-"
     77 					 name-as-string
     78 					 "-p")))
     79 	 (helm-source-name (intern (concat "helm-source-bookmark-" name-as-string)))
     80 	 (alist-function-name (intern (concat "helm-bookmark-"
     81 					      name-as-string
     82 					      "-setup-alist"))))
     83     `(progn
     84        (defun ,predicate-name (bookmark)
     85 	 (let* ((filename (expand-file-name (or (bookmark-get-filename bookmark) ""))))
     86 	   ,conditions))
     87        (defun ,alist-function-name ()
     88 	 ,(concat "Create an alist for " docstring)
     89 	 (helm-bookmark-filter-setup-alist ',predicate-name))
     90        (defvar ,helm-source-name
     91 	 (helm-make-source ,docstring 'helm-source-filtered-bookmarks
     92 	   :init (lambda ()
     93 		   (bookmark-maybe-load-default-file)
     94 		   (helm-init-candidates-in-buffer
     95 		       'global (,alist-function-name))))))))
     96 
     97 
     98 ;;; University files -- matches anything in ~/Documents/uni
     99 (helm-bookmark-create-source-please
    100  university "University"
    101  (file-in-directory-p
    102   filename "~/Documents/uni"))
    103 
    104 ;;; Config files -- matches all org-mode files in ~/org/config/, as
    105 ;;; well as anything with "config" in the name.
    106 (helm-bookmark-create-source-please
    107  config "Org-mode config files"
    108  (and (string-suffix-p ".org" filename t)
    109       (or (file-in-directory-p
    110 	   filename "~/org/config/")
    111 	  (string-match-p ".*config.*" (car bookmark)))))
    112 
    113 ;;; Elisp files -- matches all elisp files
    114 (helm-bookmark-create-source-please
    115  elisp "Emacs lisp files"
    116  (string-suffix-p ".el" filename t))
    117 
    118 ;;; Downloads -- matches anything in ~/Downloads or ~/Videos
    119 (helm-bookmark-create-source-please
    120  downloads "Downloads/Videos"
    121  (or (file-in-directory-p filename "~/Downloads/")
    122      (file-in-directory-p filename "~/Videos/")))
    123 
    124 ;;; Misc. org-mode files -- only org-mode files that aren't already in
    125 ;;; one of the sources.
    126 (helm-bookmark-create-source-please
    127  org-misc "Miscellaneous org files"
    128  (and (string-suffix-p ".org" filename t)
    129       (not (helm-bookmark-config-p bookmark))
    130       (not (helm-bookmark-downloads-p bookmark))
    131       (not (helm-bookmark-university-p bookmark))))
    132 
    133 ;;; Magit
    134 (helm-bookmark-create-source-please
    135  magit "Git repositories"
    136  (eq (bookmark-get-handler bookmark) 'magit--handle-bookmark))
    137 
    138 ;;; Directories
    139 (helm-bookmark-create-source-please
    140  dired "Bookmarked directories"
    141  (and (file-directory-p filename)
    142       (not (helm-bookmark-elfeed-p bookmark))
    143       (not (helm-bookmark-gnus-p bookmark))
    144       (not (helm-bookmark-magit-p bookmark))))
    145 
    146 ;;; Elfeed
    147 (helm-bookmark-create-source-please
    148  elfeed "Elfeed entries and searches"
    149  (or (eq (bookmark-get-handler bookmark) 'elfeed-show-bookmark-handler)
    150      (eq (bookmark-get-handler bookmark) 'elfeed-search-bookmark-handler)))
    151 
    152 ;;; Mail
    153 ;; Gnus can create "normal" bookmarks, but it also provides a library
    154 ;; specifically for gnus bookmarks.  I'm on the fence about which to use.
    155 (helm-bookmark-create-source-please
    156  gnus "Gnus Articles"
    157  (eq (bookmark-get-handler bookmark) 'gnus-summary-bookmark-jump))
    158 
    159 ;;; Other bookmarks
    160 (helm-bookmark-create-source-please
    161  other "Other bookmarks"
    162  (and (not (file-directory-p filename))
    163       (cl-loop for pred in '(helm-bookmark-gnus-p
    164 			     helm-bookmark-university-p
    165 			     helm-bookmark-elisp-p
    166 			     helm-bookmark-config-p
    167 			     helm-bookmark-org-misc-p
    168 			     helm-bookmark-downloads-p
    169 			     helm-bookmark-dired-p
    170 			     helm-bookmark-magit-p
    171 			     helm-bookmark-info-bookmark-p
    172 			     helm-bookmark-man-bookmark-p
    173 			     helm-bookmark-elfeed-p)
    174                never (funcall pred bookmark))))
    175 
    176 
    177 (provide 'custom-helm-bookmark)
    178 
    179 ;;; custom-helm-bookmark.el ends here