【问题标题】:Elisp: How to search a wordlist and copy the results to another buffer?Elisp:如何搜索单词表并将结果复制到另一个缓冲区?
【发布时间】:2016-12-21 13:56:51
【问题描述】:

我有一个单词表

dempron {hic, haec, hoc, huius, huic, hunc, hanc, hac, hi, hae, horum, harum, his, hos, has}

我有一个 xml 类型的文本

<p>Hoc templum magnum est.</p>
<p>Templa Romanorum magna sunt.</p>
<p>Claudia haec templa in foro videt.</p>

我想搜索单词列表“dempron”并将包含单词的句子从单词列表复制到名为 results 的缓冲区。

【问题讨论】:

  • 您自己对这个问题有什么尝试吗? Stackoverflow 是针对特定编程问题的,而不是人们免费编写代码的网站......
  • 我无意滥用 Stackoverflow。我希望其他人能从 charliegreen 的代码中受益。

标签: search copy buffer elisp


【解决方案1】:

我同意 Simon Fromme 的观点,但希望这能让你开始。如果您有任何问题,请告诉我!

(defconst dempron
  '("hic" "haec" "hoc" "huius" "huic" "hunc" "hanc" "hac" "hi" "hae" "horum"
    "harum" "his" "hos" "has"))

(defun dempron-search ()
  "A function to naively search for sentences in XML <p> tags
containing words from `dempron'. Run this in the buffer you want
to search, and it will search from POINT onwards, writing results
to a buffer called 'results'."
  (interactive)
  (beginning-of-line)

  (while (not (eobp)) ;; while we're not at the end of the buffer
    (let ((cur-line   ;; get the current line as a string
           (buffer-substring-no-properties
            (progn (beginning-of-line) (point))
            (progn (end-of-line) (point)))))

      ;; See if our current line is in a <p> tag (and run `string-match' so we
      ;; can extract the sentence with `match-string')
      (if (string-match "^<p>\\(.*\\)</p>$" cur-line)
      (progn
        ;; then extract the actual sentence with `match-string'
        (setq cur-line (match-string 1 cur-line))

        ;; For each word in our sentence... (split on whitespace and
        ;; anything the sentence is likely to end with)
        (dolist (word (split-string cur-line "[[:space:].?!\"]+"))
          ;; `downcase' to make our search case-insensitive
          (if (member (downcase word) dempron)
              ;; We have a match! Temporarily switch to the
              ;; results buffer and write the sentence
              (with-current-buffer (get-buffer-create "results")
                (insert cur-line "\n")))))))
    (forward-line 1))) ;; Move to the next line

【讨论】:

  • 非常感谢您提供的代码宝库。效果很好,我一定会开始的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
相关资源
最近更新 更多