Jul 23

一个在 Wordpress 中使代码加亮的办法,需要激活 MyCSS 插件,并且保证 WP Lightbox 2 插件是禁用的。

写一段程序代码(我是写的 C++),尽量包含最多的语法元素,然后把代码给 htmlize 了(可以用 Emacs 的 htmlize.el 或者 Vim),这里有一个要求,就是生成的 html 必须是 CSS 与 <pre> 分开的,CSS 在 <head> 里, <pre><body> 中,然后把 CSS copy 到 MyCSS 里。

以后贴代码的时候,只要用同样的方法生成 html,然后把那个 <pre> 贴到 post 里就行了。还有一点需要注意,MyCSS 会生成一个 my.css 放在 blog 的 <head> 里,位置是比较靠前的,在我这里比 Coolcode 和 SyntaxHighlighter 都要考前,所以 my.css 里的内容可能会被覆盖掉。

测试:

;; =============== Unfill-buffer ===============>
(defun unfill-buffer ()
  “Unfill current buffer.”
  (interactive “”)
  (setq m (point-marker))
  (beginning-of-buffer)
  (while (re-search-forward \\\\([^ ]+\\\\) *
 *\\\\([^ ]\\\\) nil t)
    (replace-match “\\\\1\\\\2″))
  (set-marker m 0 (current-buffer)))

Continue reading »

Jul 20

才知道原来在多文件编译时 c++ 类模板的声明和定义一定包含在同一个文件里。

以前写 Game of Life 的时候写了一个动态二维数组的模板,当时的文件是这样包含的:

// matrix.hpp

template<class T> class TMatrix
{
// Declarations…
};

#include “matrix.cpp” // Definitions

今天又用到了这个类,看这个文件包含不爽,改成了常规的

// matrix.hpp

template<class T> class TMatrix
{
// Declarations...
};
// matrix.cpp

#include "matrix.cpp"

... // Definitions

然后在主程序里引用了一个成员函数

// main.cpp
#include "matrix.hpp"

int main()
{
    TMatrix<int> Matrix;
    Matrix.row();
    return 0;
}

编译:g++ -g main.cpp matrix.cpp

呜呼!出错了!说 TMatrix<int>::row() 只是声明了,没有定义。把 matrix 里的文件包含改成原来那个样子,然后只编译 main.cpp 就没问题了。百思不得其解,后来在 CSDN 上找到了这个。理论上,只要在模板声明前加上 export 关键字,然后用常规的多文件编译方法就可以,但是目前大部分编译器都不支持这个关键字。g++ 说“目前尚未实现,忽略”,真够直接的…

Jul 20

写了个简单的 elisp 函数 unfill-buffer,用来去掉当前 buffer 的所有单独的换行。

[coolcode lang=”lisp” download=”unfill-buffer.el”]
(defun unfill-buffer ()
“Unfill current buffer.”
(interactive “”)
(setq m (point-marker))
(beginning-of-buffer)
(while (re-search-forward “\\([^ ]+\\) *
*\\([^ ]\\)” nil t)
(replace-match “\\1\\2″))
(set-marker m 0 (current-buffer)))
[/coolcode]

Close
E-mail It