tags:

views:

34

answers:

2

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)
+2  A: 

Try

(unless b (setq b (point-min)))
(unless e (setq e (point-max)))
Trey Jackson
@Trey : I get "The mark is not set now, so there is no region", when I just open a doc and do "C-c C-k".
prosseek
+3  A: 

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 ...
      ))
Sean
@Sean : I tried, but it seems that when I don't make any region, point-min and point-max points to correct position (start and end of the buffer).
prosseek
If the positions are correct, then what's the problem? I don't understand.
Sean
@Sean : I tried to say 'point-min doesn't point to the start of the buffer', and 'point-max' doesn't point to the end of the buffer. As I mentioned, when I make any region, nothing happens.
prosseek
I'm still a little unclear on exactly what you want to accomplish, but I added another possible interpretation above...
Sean