home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

ol-rg.el (2167B)


      1 ;;; ol-rg.el --- Links to rg -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020 Vincent Demeester
      4 
      5 ;; Author: Vincent Demeester <vincent@sbr.pm>
      6 ;; Keywords: org link ripgrep rg.el
      7 ;; Version: 0.1
      8 ;; URL: https://gitlab.com/vdemeester/vorg
      9 ;; Package-Requires: ((emacs "26.0") (org "9.0") (rg "1.8.0"))
     10 ;;
     11 ;; This file is not part of GNU Emacs.
     12 
     13 ;; This program is free software; you can redistribute it and/or
     14 ;; modify it under the terms of the GNU General Public License as
     15 ;; published by the Free Software Foundation; either version 3.0, or
     16 ;; (at your option) any later version.
     17 
     18 ;; This program is distributed in the hope that it will be useful,
     19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21 ;; GNU General Public License for more details.
     22 
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with this program; if not, write to the Free Software
     25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     27 ;;
     28 ;;; Commentary:
     29 
     30 ;; This file implements links to Ripgrep from within Org mode.
     31 ;; rg:orgmode             : run ripgrep on current working dir with orgmode expression
     32 ;; rg:orgmode:config/     : run ripgrep on config/ dir with orgmode expression
     33 ;; rg:orgmode:config/#org : run ripgrep on config/ dir with orgmode expression
     34 
     35 ;;; Code:
     36 
     37 (require 'rg)
     38 (require 'ol)
     39 
     40 ;; Install the link type
     41 (org-link-set-parameters "rg"
     42                          :follow #'org-rg-follow-link
     43                          :face '(:foreground "DarkGreen" :underline t))
     44 
     45 (defun org-rg-follow-link (regexp)
     46   "Run `rg` with REXEP as argument,
     47 like this : [[rg:REGEXP:FOLDER#FILTER]]"
     48   (setq expressions (split-string regexp ":"))
     49   (setq exp (nth 0 expressions))
     50   (setq folderpart (nth 1 expressions))
     51   (setq files (split-string folderpart "#"))
     52   (setq folder (nth 0 files))
     53   (setq filter (nth 1 files))
     54   (if folderpart
     55       (if filter
     56           (rg exp (concat "*." filter) folder)
     57         (rg exp "*" folder))
     58     (rg exp "*" "./")))
     59 
     60 (provide 'ol-rg)
     61 ;;; ol-rg.el ends here