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

pro*_*eek 6 emacs elisp

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

(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)
Run Code Online (Sandbox Code Playgroud)

添加

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

(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)
Run Code Online (Sandbox Code Playgroud)

Sea*_*ean 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)))
Run Code Online (Sandbox Code Playgroud)

但请注意,be当这个命令交互运行将一直进行设置.

你也可以这样做:

(require 'cl)

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

编辑:

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

(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 ...
      ))
Run Code Online (Sandbox Code Playgroud)