如何使用emacs / elisp获取当前缓冲区信息的开始/结束?

时间:2010-08-25 19:26:59

标签: emacs elisp

我有以下代码运行作为范围输入的figlet。 如何修改此代码以检查是否未指定b或e,将b设置为当前缓冲区的开头,以及当前缓冲区的结束?

(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

肖恩帮助我得到了这个问题的答案

(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 个答案:

答案 0 :(得分:7)

像这样:

(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)))

但请注意,当此命令以交互方式运行时,将始终设置be

你也可以这样做:

(require 'cl)

(defun* figlet-region (&optional (b (point-min)) (e (point-max)))
  # your original function body here
    )

编辑:

所以我猜你的意思是你希望能够以交互方式运行命令,即使该区域不活跃?那么也许这对你有用:

(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 ...
      ))

答案 1 :(得分:3)

尝试

(unless b (setq b (point-min)))
(unless e (setq e (point-max)))