我有以下代码运行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)
添加
肖恩(Sean)帮助我得到了这个问题的答案
(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)
最佳答案
像这样:
(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)))
但是请注意,以交互方式运行此命令时,将始终设置
b
和e
。您也可以这样做:
(require 'cl)
(defun* figlet-region (&optional (b (point-min)) (e (point-max)))
# your original function body here
)
编辑:
因此,我想您的意思是即使该区域处于非 Activity 状态,您也希望能够以交互方式运行该命令?然后,这可能会为您工作:
(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 ...
))
关于emacs - 如何使用emacs/elisp获取当前缓冲区信息的开始/结束?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3569501/