Getting Emacs fill-paragraph to play nice with javadoc-like comments

自古美人都是妖i 提交于 2019-11-30 23:05:16

问题


I'm writing an Emacs major mode for an APL dialect I use at work. I've gotten basic font locking to work, and after setting comment-start and comment-start-skip, comment/uncomment region and fill paragraph also work.

However, comment blocks often contain javadoc style comments and i would like fill-paragraph to avoid glueing together lines starting with such commands.

If I have this (\ instead of javadoc @):

# This is a comment that is long and should be wrapped.
# \arg Description of argument
# \ret Description of return value

M-q gives me:

# This is a comment that is long and
# should be wrapped. \arg Description
# of argument \ret Description of
# return value

But I want:

# This is a comment that is long and
# should be wrapped.
# \arg Description of argument
# \ret Description of return value

I've tried setting up paragraph-start and paragraph-separate to appropriate values, but fill-paragraph still doesn't work inside a comment block. If I remove the comment markers, M-q works as I want to, so the regexp I use for paragraph-start seems to work.

Do I have to write a custom fill-paragraph for my major mode? cc-mode has one that handles cases like this, but it's really complex, I'd like to avoid it if possible.


回答1:


The problem was that the paragraph-start regexp has to match the entire line to work, including the actual comment character. The following elisp works for the example I gave:

(setq paragraph-start "^\\s-*\\#\\s-*\\\\\\(arg\\|ret\\).*$")

Here a page that has an example regexp for php-mode that does this: http://barelyenough.org/blog/2006/10/nicer-phpdoc-comments/




回答2:


There's other modes that have less complex functions used for fill-paragraph-function. Browsing through my install, it looks like the ones in ada-mode and make-mode are good examples.




回答3:


What I do in these cases is open a blank line between the paragraph lines and the argument lines, then use M-q to wrap the paragraph lines, then kill the blank line between them. Not ideal, but it works and is easy enough to record in a macro if you need to repeat it.



来源:https://stackoverflow.com/questions/71788/getting-emacs-fill-paragraph-to-play-nice-with-javadoc-like-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!