问题
I have the following code that runs figlet that has input as a range. How can I modify this code to check if b or e is not specified, make b to the start of the current buffer, and e end of the current buffer?
(defun figlet-region (&optional b e)
(interactive "r")
(shell-command-on-region b e "/opt/local/bin/figlet" (current-buffer) t)
(comment-region (mark) (point)))
(global-set-key (kbd "C-c C-x") 'figlet-region)
ADDED
Sean helped me to get an answer to this question
(defun figlet-region (&optional b e)
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
(shell-command-on-region b e "/opt/local/bin/figlet" (current-buffer) t)
(comment-region (mark) (point))))
(global-set-key (kbd "C-c C-x") 'figlet-region)
回答1:
Like this:
(defun figlet-region (&optional b e)
(interactive "r")
(shell-command-on-region
(or b (point-min))
(or e (point-max))
"/opt/local/bin/figlet" (current-buffer) t)
(comment-region (mark) (point)))
But note that b
and e
will always be set when this command is run interactively.
You could also do this:
(require 'cl)
(defun* figlet-region (&optional (b (point-min)) (e (point-max)))
# your original function body here
)
EDIT:
So I guess you mean you want to be able to run the command interactively even if the region is not active? Then maybe this will work for you:
(defun figlet-region ()
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
# ... rest of your original function body ...
))
回答2:
Try
(unless b (setq b (point-min)))
(unless e (setq e (point-max)))
来源:https://stackoverflow.com/questions/3569501/how-to-get-the-start-end-of-the-current-buffer-info-with-emacs-elisp