代码之家  ›  专栏  ›  技术社区  ›  prosseek

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

  •  6
  • prosseek  · 技术社区  · 14 年前

    我有以下运行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)
    
    2 回复  |  直到 14 年前
        1
  •  7
  •   Sean    14 年前

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

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

    (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 ...
          ))
    
        2
  •  4
  •   Trey Jackson    14 年前

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