Insert a picture in code comment via Emacs

2019-04-26 02:05发布

问题:

An idea has just came to my mind. Is there any methods to insert a picture in code comment to help explain our code? After all, a picture is worth a thousand words.

Since I use Emacs, is there any implementations for Emacs so far?

回答1:

Take a look at iimage-mode: it is a standard solution to display an image in a text buffer.

For example, given the following file:

/* an image is worth a thousand words:
 * <path/to/image.png>
 */
int main (int argc, char **argv) {
  return 0;
}

then running M-xturn-on-iimage-mode should display image.png in place of its path.


However, I agree with @user4815162342's comment that you need to distribute images along with source files and to remember updating them when needed (it is sometimes hard to update code comments when they are text only; it will IMO be harder to update image comments)



回答2:

Are you talking about ASCII graphics or actual images?

For ASCII art, there's the M-x artist-mode that allows you to draw simple (or not very simple) images. Also, if you need to draw ASCII-based UML diagrams, CEDET can do it for you.

For inserting actual images, there's a bunch of functions like create-image and insert-image, so writing a minor mode for inserting images to comments should be straightforward. I'm not aware of any ready-made solutions, though.



回答3:

If you want to paste online images also, cacoo.el will be very useful. It caches online images and resize it wisely. So, showing the image from the second time is much fast. It also works with local images.

It was first developed for web service called cacco, but it work for any online images.