Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the start/end of the current buffer info with emacs/elisp?

Tags:

emacs

elisp

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)
like image 339
prosseek Avatar asked Aug 25 '10 19:08

prosseek


People also ask

Why is buffer read-only Emacs?

A buffer visiting a write-protected file is normally read-only. Here, the purpose is to inform the user that editing the buffer with the aim of saving it in the file may be futile or undesirable. The user who wants to change the buffer text despite this can do so after clearing the read-only flag with C-x C-q .

How do I change the read-only buffer in Emacs?

To change the read-only status of a buffer, use C-x C-q (toggle read-only-mode ). To change file permissions, you can run dired on the file's directory ( C-x d ), search for the file by C-s and use M to change its mode.

What is a buffer on Emacs?

Buffers in Emacs editing are objects that have distinct names and hold text that can be edited. Buffers appear to Lisp programs as a special data type. You can think of the contents of a buffer as a string that you can extend; insertions and deletions may occur in any part of the buffer.


1 Answers

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 ...
      ))
like image 144
Sean Avatar answered Sep 20 '22 13:09

Sean