help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why emacsclient -e "(current-word nil t)" does not print ?


From: Wang Lei
Subject: Re: Why emacsclient -e "(current-word nil t)" does not print ?
Date: Wed, 22 Jul 2009 21:27:01 +0800

Thanks! It works. I have finished it. Here is the code.
-------------------->8-------------------->8--------------------
(defvar sdcv-replace-template ",$|^`|\\.$|^'|'$|\\?$")
(defvar emacs-window-name "@EMACS@")
(defun display-message-seconds (message seconds #!optional attributes)
  "Display message for N seconds."
  (interactive)
  (display-message message attributes)
  (make-timer (lambda () (display-message))
              seconds))
(defun sdcv-get-word ()
  "Get the word from current-word in emacs or selection or user input."
  (let* ((stream (make-string-output-stream))
         (process (make-process stream))
         (title "Enter String ")
         (word "")
         (result ""))
    (if (string= (window-name (input-focus)) emacs-window-name)
        (progn
          (call-process process nil "emacsclient" "-e"
                        "(with-current-buffer (window-buffer 
(frame-first-window))
(current-word nil t))")
          (setq word (get-output-stream-string stream))
          (setq word (string-replace "^\"(.+)\"\n" "\\1" word)))
      (setq word (string-replace sdcv-replace-template ""
(x-get-selection 'PRIMARY))))
    (setq word (string-replace sdcv-replace-template "" word))
    (setq title (concat title "(" word "):" ))
    (setq result (prompt-for-string title))
    (if (string= result "")
        word
      result)))
(defun sdcv-search ()
  "Search word with sdcv."
  (interactive)
  (let* ((word (sdcv-get-word))
         (output (make-string-output-stream))
         (proc (make-process output))
         (errmsg "NO definite result. Try again.")
         (msg-attr '((position . (0 . 0)) (background . "LightGoldenrod1")
(foreground . "black"))))
    (call-process proc nil "sdcv" "-n" "--data-dir"
"~/.stardict/dic/stardict-langdao-ec-gb-2.4.2/" word)
    (setq output (get-output-stream-string output))
    (if (> (when (string-match "Found \(..?\) items" output)
             (string->number (expand-last-match "\\1")))
           1)
        (display-message-seconds errmsg 2 msg-attr)
      (display-message-seconds output 5 msg-attr))))
-------------------->8-------------------->8--------------------
This is librep, it's like elisp, used in sawfish wm. Maybe it's
useless for you at all. Just have a look. ;)

On 7/22/09, Anselm Helbig <address@hidden> wrote:
>> NOW. Maybe having two choices is ok. Because if I really want to
>> integrate everything, "get word from screen" should be my choice, not
>> selection ang prompt. Thanks for you help!
>
> So I understand that you really require to call your code from outside
> of emacs. When you're not in emacs, there's AFAIK no way to tell which
> buffer is "current". You may have many buffers, and also any number of
> frames and windows. This code takes the buffer that the first window
> of the currently selected frame is pointing to, which will probably do
> what you want:
>
>   (with-current-buffer (window-buffer (frame-first-window)) (current-word
> nil t))
>
> HTH,
>
> Anselm
>
>
> --
> Anselm Helbig
> mailto:address@hidden
>


-- 
Regards
Lei




reply via email to

[Prev in Thread] Current Thread [Next in Thread]