学习再学习,多少大秘密

14 11

emacs po-mode 断行的问题

emacs 的 po-mode 非常好用,而且几乎不用任何配置,快捷键很多而且很简单。
比如 n 跳到下一条,t 跳到下一条翻译的,u 跳到下一条没有翻译的,f 跳到下
一条标记 fuzzy 的, backspace 增加 fuzzy 标记, TAB 去除,等等…

不过也有个比较郁闷的地方,就是多行的文本,在 emacs 中配合 autofill-mode
自动换行之后,会在每一行后面多出一个回车符“\n”,这个是不需要的,并且以后
修改的时候比较麻烦。因为在 po-mode 中是不能直接随心所欲的编辑文本正文的。
所以只有文本模式下面编辑。再处理一次简直是不可原谅的。所以就自己想修改一
下,查了一下,该死的竟然没有文档,于是就只有修改源码了。。。


找了一下,改了一个小小的部分。在 po-mode.el 的 1722 行的一个函数定义之中

  1. (defun po-eval-requoted (form prefix obsolete)
  2.   "Eval FORM, which inserts a string, and return the string fully requoted.
  3. If PREFIX, precede the result with its contents.  If OBSOLETE, comment all
  4. generated lines in the returned string.  Evaluating FORM should insert the
  5. wanted string in the buffer which is current at the time of evaluation.
  6. If FORM is itself a string, then this string is used for insertion."
  7.   (po-with-temp-buffer
  8.     (if (stringp form)
  9.     (insert form)
  10.       (push-mark)
  11.       (eval form))
  12.     (goto-char (point-min))
  13.     (let ((multi-line (re-search-forward "[^\n]\n+[^\n]" nil t)))
  14.       (goto-char (point-min))
  15.       (while (re-search-forward "[.\"\a\b\f\n\r\t\\]" nil t) ;第一
  16.     (cond ((eq (preceding-char) ?\") (replace-match "\\\"" t t))
  17.           ((eq (preceding-char) ?\a) (replace-match "\\a" t t))
  18.           ((eq (preceding-char) ?\b) (replace-match "\\b" t t))
  19.           ((eq (preceding-char) ?\f) (replace-match "\\f" t t))
  20.   ((eq (preceding-char) ?.) (replace-match "\\n" t t)) ;第二
  21.           ((eq (preceding-char) ?\n) ;(replace-match "\n" t t))
  22.            (replace-match (if (or (not multi-line) (eobp))
  23.                   "\\n"
  24.                 "\"\n\"") ;第三
  25.                   t t))
  26.           ((eq (preceding-char) ?\r) (replace-match "\\r" t t))
  27.           ((eq (preceding-char) ?\t) (replace-match "\\t" t t))
  28.           ((eq (preceding-char) ?\\) (replace-match "\\\\" t t))))
  29.       (goto-char (point-min))
  30.       (if prefix (insert prefix " "))
  31.       (insert (if multi-line "\"\"\n\"" "\""))
  32.       (goto-char (point-max))
  33.       (insert "\"")
  34.       (if prefix (insert "\n"))
  35.       (if obsolete
  36.       (progn
  37.         (goto-char (point-min))
  38.         (while (not (eobp))
  39.           (or (eq (following-char) ?\n) (insert "#~ "))
  40.           (search-forward "\n"))))
  41.  
  42.       (buffer-string))))

修改的就是上面标记了 第一,第二,第三 的三行,下面是相关的说明。
先说第三,原先这里是 “\\n\”\n\”") 实质是在每一行结尾加上 \n”然后回车,再
在下面一行加上”, 当然我简单的将 \\n 给去掉了
再说第二,经过下面的修改之后,每一行的最后的 \n 都被去掉了,不过最后一行
如果是 \n 就还在,这个是我们期望的,但是也有多行的文本,本来有 \n 的,这
时也被去掉了,很是郁闷,但是在正常状况之下,emacs 无法区分哪里的回车应该
加上,哪里不应该加上。于是我就想到使用不同的标识符,想了半天,最后相中了
这个 . 因此就多了这么一个规则,将 . 替换为 \n。自然,在第一处的中括号中
就多出了一个 . 。于是遇到应该有 \n 符号的,就在行尾加上一个 . 。再断行,
否则就直接断行,最后一行不用理会。 Emacs 会自动处理好的。

虽然比较 ugly,不过初步目的是达到了,简单测试了一下,还好,能够正常工作。哈哈~

附:我是直接在 scratch 中 eval 这段代码测试的,这样这个函数覆盖了原来的
定义,我想应该直接可以写在配置文件中的,只要保证这段代码在 po-mode 之后
加载就能够正常工作了。具体的明天有空再补充了。

4 Responses to “emacs po-mode 断行的问题”

  1. 1
    millenniumdark Says:

    vi下貌似也有po.vim。不過我還是用poedit。

  2. 2
    millenniumdark Says:

    暈,trackback也trackback錯地方了,zhan把它刪掉吧。

  3. 3
    zhan Says:

    trackback 是什么玩意?

  4. 4
    学习再学习,多少大秘密 » Blog Archive » 小修改了一下 po-mode 的配置 Says:

    [...] 之前 小修改了一下 po-mode, 使用了 . 来识别是否需要真正的使用 n 来断行, 但是今天被郁闷了,翻译 kile.po 时遇到了几个 abc.def 的文件名,照样翻译了 之后,点被替换成 n 了。于是发现用其他符号代替回车不是一个好办法,最好的 还是使用 n,不过一个问题是,emacs 替换时会将 n 替换成 \n,这是基于 的转义,平时在一行的中间也是可以的,但是在一行的行尾的话就会有问题。 [...]

Leave a Reply



© 2008 学习再学习,多少大秘密 | Entries (RSS) and Comments (RSS)

Your Index Web Directorywordpress logo