narrow-x

Extra functions for narrowing in Emacs
git clone https://git.jamzattack.xyz/narrow-x
Log | Files | Refs | LICENSE

narrow-x.el (4019B)


      1 ;;; narrow-x.el --- Extra functions for narrowing    -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020  Jamie Beardslee
      4 
      5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz>
      6 ;; Version: 2020.09.17
      7 ;; Keywords:
      8 
      9 ;; This program is free software; you can redistribute it and/or modify
     10 ;; it under the terms of the GNU General Public License as published by
     11 ;; the Free Software Foundation, either version 3 of the License, or
     12 ;; (at your option) any later version.
     13 
     14 ;; This program is distributed in the hope that it will be useful,
     15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 ;; GNU General Public License for more details.
     18 
     19 ;; You should have received a copy of the GNU General Public License
     20 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     21 
     22 ;;; Commentary:
     23 
     24 ;; This library provides a few extra functions for narrowing.  Most
     25 ;; are for moving between text blocks.  Also provided is the missing
     26 ;; `narrow-to-paragraph'.
     27 
     28 ;; Recommended bindings are:
     29 ;;   ;; paragraph
     30 ;;   (define-key narrow-map (kbd "h") #'narrow-to-paragraph)
     31 ;;   (define-key narrow-map (kbd "M-n") #'narrow-to-next-paragraph)
     32 ;;   (define-key narrow-map (kbd "M-p") #'narrow-to-prev-paragraph)
     33 ;;   ;; page
     34 ;;   (define-key narrow-map (kbd "SPC") #'narrow-to-next-page)
     35 ;;   (define-key narrow-map (kbd "DEL") #'narrow-to-prev-page)
     36 ;;   ;; defun
     37 ;;   (define-key narrow-map (kbd "b") #'narrow-to-prev-defun)
     38 ;;   (define-key narrow-map (kbd "f") #'narrow-to-next-defun))
     39 
     40 ;;; Code:
     41 
     42 (require 'page)
     43 
     44 
     45 ;;; next/prev page
     46 
     47 ;;;###autoload
     48 (defun narrow-to-next-page (&optional count)
     49   "Narrow to the next page.
     50 Prefix arg COUNT is passed to `forward-page', rather than
     51 `narrow-to-page'."
     52   (interactive "p")
     53   (widen)
     54   (forward-page count)
     55   (narrow-to-page))
     56 
     57 ;;;###autoload
     58 (defun narrow-to-prev-page (&optional count)
     59   "Narrow to the previous page.
     60 Negated prefix arg COUNT is passed to `forward-page', rather than
     61 `narrow-to-page'."
     62   (interactive "p")
     63   (narrow-to-next-page (- (1+ count))))
     64 
     65 
     66 ;;; next/prev defun
     67 
     68 ;;;###autoload
     69 (defun narrow-to-next-defun (&optional count)
     70   "Narrow to the next defun.
     71 Point will be moved to the beginning of the defun.  Prefix arg
     72 COUNT is passed to `beginning-of-defun', rather than
     73 `narrow-to-defun'."
     74   (interactive "p")
     75   (widen)
     76   (beginning-of-defun (- count))
     77   (narrow-to-defun))
     78 
     79 ;;;###autoload
     80 (defun narrow-to-prev-defun (&optional count)
     81   "Narrow to the previous defun.
     82 Point will be moved to the beginning of the defun.  Negated
     83 prefix arg COUNT is passed to `beginning-of-defun', rather than
     84 `narrow-to-defun'."
     85   (interactive "p")
     86   (narrow-to-next-defun (- count)))
     87 
     88 
     89 ;;; Paragraphs
     90 
     91 ;; Need to define `narrow-to-paragraph', as this isn't provided in
     92 ;; core.
     93 ;;;###autoload
     94 (defun narrow-to-paragraph (&optional arg)
     95   "Narrow to the next ARG paragraphs."
     96   (interactive "p")
     97   (setq arg (or arg 1))
     98   (let ((bounds
     99 	 (save-excursion
    100 	   (cons (progn (forward-paragraph (if (< arg 0)
    101 					       (1- arg)
    102 					     arg))
    103 			(forward-line)
    104 			(goto-char (line-beginning-position)))
    105 		 (progn (backward-paragraph (1+ arg))
    106 			(forward-line (if (eq (point) (point-min))
    107 					  0
    108 					1))
    109 			(goto-char (line-beginning-position)))))))
    110     (narrow-to-region (car bounds) (cdr bounds))))
    111 
    112 ;;;###autoload
    113 (defun narrow-to-next-paragraph (&optional count)
    114   "Narrow to the next paragraph.
    115 Point will be moved to the beginning of the paragraph.  Prefix
    116 arg COUNT is passed to `forward-paragraph', rather than
    117 `narrow-to-paragraph'."
    118   (interactive "p")
    119   (widen)
    120   (forward-paragraph count)
    121   (narrow-to-paragraph))
    122 
    123 ;;;###autoload
    124 (defun narrow-to-prev-paragraph (&optional count)
    125   "Narrow to the previous paragraph.
    126 Point will be moved to the beginning of the paragraph.  Negated
    127 prefix arg COUNT is passed to `forward-paragraph', rather than
    128 `narrow-to-paragraph'."
    129   (interactive "p")
    130   (narrow-to-next-paragraph (- (1+ count))))
    131 
    132 (provide 'narrow-x)
    133 ;;; narrow-x.el ends here