Fix narrow-or-widen-dwim by improved error handling

This commit is contained in:
Gerard Vermeulen 2022-05-28 17:44:19 +02:00
parent 2a122fcdf6
commit 6b22c96e64

View File

@ -3685,46 +3685,50 @@ focussing out on the whole buffer. This allows to concentrate temporarily on
for instance a particular function or paragraph by removing clutter. The "Do for instance a particular function or paragraph by removing clutter. The "Do
What I Mean" [[https://endlessparentheses.com/emacs-narrow-or-widen-dwim.html][narrow-or-widen-dwim]] function allows to toggle between narrowed and What I Mean" [[https://endlessparentheses.com/emacs-narrow-or-widen-dwim.html][narrow-or-widen-dwim]] function allows to toggle between narrowed and
widened buffer states. Here, the function =narrow-or-widen-dwim= operates also widened buffer states. Here, the function =narrow-or-widen-dwim= operates also
on tables by means of =org-narrow-to-table=. on any Org table, Org source block, Org block, or Org subtree.
#+caption[Configure =narrow-or-widen-dwim=]: #+caption[Configure =narrow-or-widen-dwim=]:
#+caption: Configure =narrow-or-widen-dwim=. #+caption: Configure =narrow-or-widen-dwim=.
#+name: lst:configure-narrow-or-widen-dwim #+name: lst:configure-narrow-or-widen-dwim
#+begin_src emacs-lisp #+begin_src emacs-lisp
(with-eval-after-load 'emacs (with-eval-after-load 'emacs
(autoload 'org-at-table "org-table") (autoload 'org-at-table-p "org-table")
(defun org-narrow-to-table () (defun org-narrow-to-table ()
"Narrow buffer to current table." "Narrow buffer to table at point."
(interactive) (interactive)
(if (org-at-table-p) (if (org-at-table-p)
(narrow-to-region (org-table-begin) (org-table-end)) (narrow-to-region (org-table-begin) (org-table-end))
(user-error "Not in a table"))) (user-error "Not in a table")))
(defun narrow-or-widen-dwim (p) (defun narrow-or-widen-dwim (p)
"Widen if buffer is narrowed, narrow-dwim otherwise. "Widen if buffer is narrowed, narrow \"Do What I Mean\" otherwise.
Dwim means: region, org-src-block, org-table, org-subtree, LaTeX DWIM means: region, Org table, Org source block, Org block, Org
environment, TeX group, or defun, whichever applies first. subtree, LaTeX environment, TeX group, or defun, whichever
Narrowing to org-src-block actually calls `org-edit-src-code'. applies first. Narrowing to org-src-block actually calls
`org-edit-src-code'.
With prefix P, don't widen, just narrow even if buffer is already With prefix P, don't widen, just narrow even if buffer is already
narrowed." narrowed."
(interactive "P") (interactive "P")
(declare (interactive-only)) (declare (interactive-only))
(cond ((and (buffer-narrowed-p) (not p)) (widen)) (cond ((and (buffer-narrowed-p) (not p))
(widen))
((and (bound-and-true-p org-src-mode) (not p)) ((and (bound-and-true-p org-src-mode) (not p))
(org-edit-src-exit)) (org-edit-src-exit))
((region-active-p) ((region-active-p)
(narrow-to-region (region-beginning) (region-end))) (narrow-to-region (region-beginning) (region-end)))
((derived-mode-p 'org-mode) ((derived-mode-p 'org-mode)
(or (ignore-errors (org-edit-src-code)) (or (with-demoted-errors "DWIM: %S" (org-narrow-to-table) t)
(ignore-errors (org-narrow-to-block)) (with-demoted-errors "DWIM: %S" (org-edit-src-code) t)
(ignore-errors (org-narrow-to-table)) (with-demoted-errors "DWIM: %S" (org-narrow-to-block) t)
(org-narrow-to-subtree))) (org-narrow-to-subtree)))
((derived-mode-p 'latex-mode) ((derived-mode-p 'latex-mode)
(LaTeX-narrow-to-environment)) (LaTeX-narrow-to-environment))
((derived-mode-p 'tex-mode) ((derived-mode-p 'tex-mode)
(TeX-narrow-to-group)) (TeX-narrow-to-group))
(t (narrow-to-defun)))) (t
(narrow-to-defun))))
(define-key ctl-x-map (kbd "n t") #'org-narrow-to-table) (define-key ctl-x-map (kbd "n t") #'org-narrow-to-table)
(define-key ctl-x-map (kbd "C-n") #'narrow-or-widen-dwim)) (define-key ctl-x-map (kbd "C-n") #'narrow-or-widen-dwim))