mark-naylor-1701

goto line, do what I mean

Oct 21st, 2023 (edited)
2,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.88 KB | None | 0 0
  1. ;; If the buffer is narrowed, the normal mapping of M-g g calls
  2. ;; `goto-line', which widens the buffer. Default behavior reguires the
  3. ;; user to remember to call `goto-line-relative'. This function honors
  4. ;; narrowing, if in place. Binding to M-g g lets the user have normal
  5. ;; work flow without being concerned with narrow/wide status.
  6.  
  7. (defun goto-line-dwim (line)
  8.   "Go to line, do what I mean. Call `goto-line' or
  9. `goto-line-relative', depending on whether the buffer is narrowed or
  10. not. Honors the current line number display setting for the buffer."
  11.   (interactive "p")
  12.   (let ((line-numbers display-line-numbers-mode))
  13.     (unless line-numbers
  14.       (display-line-numbers-mode 1))
  15.     (unwind-protect
  16.         (call-interactively #'goto-line-relative)
  17.         (unless line-numbers
  18.           (display-line-numbers-mode -1)))))
  19.  
  20. (global-set-key (kbd "M-g g") #'goto-line-dwim)
  21.  
Tags: elisp
Advertisement
Add Comment
Please, Sign In to add comment