将对象与缓冲区相关联

时间:2012-11-14 20:43:41

标签: emacs elisp

我正在尝试做的与make-local-variable非常相似,只是我不希望为每个创建的缓冲区声明变量(仅适用于属于特定模式的缓冲区)。这主要是一个性能问题,创建的对象可能很大。更重要的是,我需要像(get-buffer-property buffer property-name)这样的函数来确定特定缓冲区的状态。

基本上,我是在追求类似于get-buffer-proccess的东西,除了我需要的不是一个过程。

在主要模式的声明中创建这样的对象并在钩子中销毁这些对象是否有意义,以防止这样的缓冲区被杀死,或者有更好的方法吗?

这是我到目前为止所做的:

(defun haxe-get-buffer-property (buffer property)
  "Pops to BUFFER, reads the value of the PROPERTY and returns it."
  (let ((result
         (save-excursion
           (pop-to-buffer buffer)
           (symbol-value property))))
    result))
(defmacro haxe-buffer-property (buffer property)
`(haxe-get-buffer-property ,buffer ',property))

(defmacro deflocal (var &rest body)
  (let ((symb var)
        (val (car body))
        (doc (cadr body)))
    `(progn
       (defvar ,symb nil ,doc)
       (unless ,symb (setq ,symb ,val))
       (make-local-variable ',symb))))

但我不喜欢我必须访问缓冲区,因为我不确定副作用。

编辑:更多信息。

会发生什么情况,有多个缓冲区可以与网络连接进程交互。此过程可以由缓冲区组共享(缓冲区共享此过程很重要),但也可能同时存在多个缓冲区,这些缓冲区具有彼此分配的不同进程。除了流程本身,还有很多关于流程状态的信息(收到了多少数据,发送了什么,错误等)。同样,这些数据必须由缓冲区组共享。

EDIT2:

这就是上面的代码,以防任何人需要它。

(defmacro deflocal (var &rest body)
  (let ((symb var)
        (val (car body))
        (doc (cadr body)))
    `(progn
       (set (make-local-variable ',symb) ,val)
       (put ',symb 'variable-documentation ,doc))))

(defun haxe-get-buffer-property (buffer property)
  "Pops to BUFFER, reads the value of the PROPERTY and returns it."
  (let ((result
         (with-current-buffer buffer
           (symbol-value property))))
    result))

(defun haxe-set-buffer-property (buffer &rest proplist)
  "Pops to BUFFER and sets properties in parallel, similar to `pset'."
  (let ((result
         (with-current-buffer buffer
           (loop for (property value) on proplist by #'cddr
                 do (set property value)
                 finally (return value)))))
    result))

(defmacro haxe-buffer-pset-property (buffer &rest proplist)
  `(haxe-set-buffer-property
    ,buffer
    ,@(loop for (key value) on proplist by #'cddr
            nconc (list (list 'quote key) value))))
(defalias 'haxe-pbset #'haxe-buffer-pset-property)

(defmacro haxe-buffer-setf-property (buffer &rest proplist)
  `(with-current-buffer ,buffer
     ,@(list (append '(setf) proplist))))
(defalias 'haxe-pbsetf #'haxe-buffer-setf-property)

1 个答案:

答案 0 :(得分:2)

emacs -q会话中,我执行了以下操作:

;; set up a hook to create buffer local variable
(add-hook 'html-mode-hook
      (lambda ()
        (set (make-local-variable 'my-var) "abcd")))

;; find two files, one html and another java    
(find-file-noselect "X.java")
(find-file-noselect "X.html")

;; see the buffer value in html buffer
(progn
  (set-buffer "X.html")
  (message "html:%s" my-var))

;; in java buffer I get error for a void variable
(progn
  (set-buffer "X.java")
  (message "java:%s" my-var))