Change behaviour of `org-in-regexps-block-p'

* lisp/org.el (org-in-regexps-block-p): return an useful value when
  point is between START-RE and END-RE. No incomplete block is allowed
  anymore. Add another optional argument to bound the bottom part of
  the search.
(org-narrow-to-block, org-in-block-p): apply modifications.
This commit is contained in:
Nicolas Goaziou 2011-07-30 00:14:14 +02:00
parent 274823c858
commit 226f8c873d
1 changed files with 40 additions and 29 deletions

View File

@ -7670,17 +7670,11 @@ If yes, remember the marker and the distance to BEG."
(defun org-narrow-to-block () (defun org-narrow-to-block ()
"Narrow buffer to the current block." "Narrow buffer to the current block."
(interactive) (interactive)
(let ((bstart "^[ \t]*#\\+begin") (let* ((case-fold-search t)
(bend "[ \t]*#\\+end") (blockp (org-in-regexps-block-p "^[ \t]*#\\+begin_.*"
(case-fold-search t) ;; allow #+BEGIN "^[ \t]*#\\+end_.*")))
b_start b_end) (if blockp
(if (org-in-regexps-block-p bstart bend) (narrow-to-region (car blockp) (cdr blockp))
(progn
(save-excursion (re-search-backward bstart nil t)
(setq b_start (match-beginning 0)))
(save-excursion (re-search-forward bend nil t)
(setq b_end (match-end 0)))
(narrow-to-region b_start b_end))
(error "Not in a block")))) (error "Not in a block"))))
(eval-when-compile (eval-when-compile
@ -19067,23 +19061,37 @@ really on, so that the block visually is on the match."
(throw 'exit t))) (throw 'exit t)))
nil)))) nil))))
(defun org-in-regexps-block-p (start-re end-re &optional bound) (defun org-in-regexps-block-p (start-re end-re &optional lim-up lim-down)
"Return t if the current point is between matches of START-RE and END-RE. "Non-nil when point is between matches of START-RE and END-RE.
This will also return t if point is on one of the two matches or
in an unfinished block. END-RE can be a string or a form
returning a string.
An optional third argument bounds the search for START-RE. It Also return a non-nil value when point is on one of the matches.
defaults to previous heading or `point-min'."
Optional arguments LIM-UP and LIM-DOWN bound the search; they are
buffer positions. Default values are the positions of headlines
surrounding the point.
The functions returns a cons cell whose car (resp. cdr) is the
position before START-RE (resp. after END-RE)."
(save-match-data
(let ((pos (point)) (let ((pos (point))
(limit (or bound (save-excursion (outline-previous-heading))))) (limit-up (or lim-up (save-excursion (outline-previous-heading))))
(limit-down (or lim-down (save-excursion (outline-next-heading))))
beg end)
(save-excursion (save-excursion
;; we're on a block when point is on start-re... ;; Point is on a block when on START-RE or if START-RE can be
(or (org-at-regexp-p start-re) ;; found before it...
;; ... or start-re can be found above... (and (or (org-at-regexp-p start-re)
(and (re-search-backward start-re limit t) (re-search-backward start-re limit-up t))
;; ... but no end-re between start-re and point. (setq beg (match-beginning 0))
(not (re-search-forward (eval end-re) pos t))))))) ;; ... and END-RE after it...
(goto-char (match-end 0))
(re-search-forward end-re limit-down t)
(> (setq end (match-end 0)) pos)
;; ... without another START-RE in-between.
(goto-char (match-beginning 0))
(not (re-search-backward start-re pos t))
;; Return value.
(cons beg end))))))
(defun org-in-block-p (names) (defun org-in-block-p (names)
"Is point inside any block whose name belongs to NAMES? "Is point inside any block whose name belongs to NAMES?
@ -19091,12 +19099,15 @@ defaults to previous heading or `point-min'."
NAMES is a list of strings containing names of blocks." NAMES is a list of strings containing names of blocks."
(save-match-data (save-match-data
(catch 'exit (catch 'exit
(let ((case-fold-search t)) (let ((case-fold-search t)
(lim-up (save-excursion (outline-previous-heading)))
(lim-down (save-excursion (outline-next-heading))))
(mapc (lambda (name) (mapc (lambda (name)
(let ((n (regexp-quote name))) (let ((n (regexp-quote name)))
(when (org-in-regexps-block-p (when (org-in-regexps-block-p
(concat "^[ \t]*#\\+begin_" n) (concat "^[ \t]*#\\+begin_" n)
(concat "^[ \t]*#\\+end_" n)) (concat "^[ \t]*#\\+end_" n)
lim-up lim-down)
(throw 'exit t)))) (throw 'exit t))))
names)) names))
nil))) nil)))