【问题标题】:How to get the start/end of the current buffer info with emacs/elisp?如何使用 emacs/elisp 获取当前缓冲区信息的开始/结束?
【发布时间】:2010-08-26 04:33:31
【问题描述】:

我有以下代码运行 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)

添加

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

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

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    像这样:

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

    【讨论】:

    • 我试过了,但似乎当我不做任何区域时,point-min 和 point-max 指向正确的位置(缓冲区的开始和结束)。
    • 如果位置正确,那有什么问题?我不明白。
    • 我试图说'point-min 不指向缓冲区的开头',而'point-max' 不指向缓冲区的结尾。正如我所提到的,当我制作任何区域时,什么都不会发生。
    • 我还不清楚你到底想要完成什么,但我在上面添加了另一种可能的解释......
    【解决方案2】:

    试试

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

    【讨论】:

    • 当我打开一个文档并执行“C-c C-k”时,我得到“现在没有设置标记,所以没有区域”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多