Category Archives: Programming Tools

Attachment Reminder in Emacs message-mode

Last updated on December 13, 2016

When sending out an email, sometimes we just forgot to attach the attachments. An attachment reminder can largely prevent this: You are asked to confirm whether you have forgotten the attachments if your message body shows that you may need one. However, Emacs by default does not provide such a feature in its mail composing mode message-mode, which is used in email clients such as gnus and mu4e. Here is an attachment reminder based on this comment:

(defun my-message-current-line-cited-p ()
  "Indicate whether the line at point is a cited line."
  (save-match-data
    (string-match (concat "^" message-cite-prefix-regexp)
                  (buffer-substring (line-beginning-position) (line-end-position)))))

(defun my-message-says-attachment-p ()
  "Return t if the message suggests there can be an attachment."
  (save-excursion
    (goto-char (point-min))
    (save-match-data
      (let (search-result)
        (while
            (and (setq search-result (re-search-forward "\\(attach\\|pdf\\|file\\)" nil t))
                 (my-message-current-line-cited-p)))
        search-result))))

(defun my-message-has-attachment-p ()
  "Return t if the message has an attachment."
  (save-excursion
    (goto-char (point-min))
    (save-match-data
      (re-search-forward "<#part" nil t))))

(defun my-message-pre-send-check-attachment ()
  (when (and (my-message-says-attachment-p)
             (not (my-message-has-attachment-p)))
    (unless
        (y-or-n-p "The message suggests that you may want to attach something, but no attachment is found. Send anyway?")
      (error "It seems that an attachment is needed, but none was found. Aborting sending."))))
(add-hook 'message-send-hook 'my-message-pre-send-check-attachment)

If “attach”, “pdf” or “file” are in the message you are sending, a confirmation would be required to confirm the attachments if they are not attached.

Prevent Browser Processes Started by Emacs from Being Killed When Emacs Exits on GNU/Linux

Last updated on November 16, 2016

The function browse-url in Emacs can be used to start a browser process to visit a given URL. It is used by many packages such as org-mode, mu4e, etc. However, on GNU/Linux, by default a new browser process started by browse-url will be killed if the Emacs process exits. To prevent the browser process from being killed, add the following code to your Emacs init file:

(when (and (executable-find "setsid") (executable-find "gnome-open"))
  (setq browse-url-browser-function
        (lambda (url &optional ignored)
          (start-process "" nil "setsid" "gnome-open" url))))

You will need both the setsid and gnome-open commands available. Note that replacing gnome-open with xdg-open is not guaranteed to work.

Installing Emacs from Source: Avoid the Conflict of ctags

Last updated on December 10, 2016

If installing Emacs from source, an executable named ctags will be installed by default. However, many systems such as GNU/Linux, FreeBSD and macOS often already have an executable named ctags installed, such as Exuberant Ctags or Universal Ctags, which then causes conflict. To avoid this conflict, we can use the switch --program-transform-name='s/^ctags$/ctags.emacs/' when running configure to rename ctags to ctags.emacs. For example:

cd /path/to/emacs-source
mkdir build && cd build
../configure --program-transform-name='s/^ctags$/ctags.emacs/'

After that, running make install will install the Emacs ctags executable as ctags.emacs.

Emacs: Disable Certain Pairs for electric-pair-mode

Last updated on December 10, 2016

In GNU Emacs, electric-pair-mode is a minor mode for auto closing pairs of (curly) braces/brackets/quotes, which was first introduced in Emacs 24. However, up till now, it still has not provided an easy option to disable a certain pair—we need to make use of electric-pair-inhibit-predicate. To disable a certain pair, such as double quotes, we can add the following to ~/.emacs or ~/.emacs.d/init.el:

(setq electric-pair-inhibit-predicate
      `(lambda (c)
         (if (char-equal c ?\") t (,electric-pair-inhibit-predicate c))))

To disable a certain pair for a specific mode, for example to disable the pairing of {} in web-mode (to allow web-mode to better handle the auto pairing of the template tags {% %}), we can add the following to ~/.emacs or ~/.emacs.d/init.el:

;; disable {} auto pairing in electric-pair-mode for web-mode
(add-hook
 'web-mode-hook
 (lambda ()
   (setq-local electric-pair-inhibit-predicate
               `(lambda (c)
                  (if (char-equal c ?{) t (,electric-pair-inhibit-predicate c))))))

For more information, please move onto the documentation of the variable electric-pair-inhibit-predicate.

dired-quick-sort: Sort Dired Buffers Quickly in Emacs

Last updated on December 10, 2016

While Dired is great for browsing the file system, it is often annoying that the buffer is not sorted in the way we want to see. While Dired provides the flexible customizing variable dired-listing-switches, it is still not convenient to switch between different sorting criteria quickly. For this reason, I created the Emacs extension dired-quick-sort to make sorting Dired an easy story.

Project Homepage

Screenshot

Live Preview of LaTeX in Vim

Last updated on October 1, 2016

Vim, as a highly configurable and customizable editor, already has a powerful Vim-LaTeX plugin. However, I don’t like the lack of live preview feature. For this reason, I developed a plugin called vim-latex-live-preview last year. Although it currently can’t handle complex situations such as multiple tex files, it could basically handle single tex file project well (with or without BibTeX, thanks to Asis Hallab).

Note: This plugin currently doesn’t work on Windows.

Continue reading

Generate Ctags Files for C/C++ Source Files and All of Their Included Header Files

Last updated on October 10, 2016

This post is for those people who use Exuberant Ctags. If you are using other versions of ctags, this post may not be useful.

When using ctags to generate the tags file for C/C++ projects, usually we use the following command:

ctags -R .

For some users that need more info of the symbols, they may use this command instead:

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

No matter which one you use, the generated tags file only contains the symbols in the files in your project source tree, but not any external file, such as standard header files (e.g. stdio.h, stdlib.h), etc. thus editors or IDEs that use tags files, such as Vim, are not able to locate symbols in external header files. There was a solution: generate a tags file for any external header files first, and let the editor or IDE read both the generated tags file and the tags file for the project source tree. For example, the following command will generate a tags file for all your system header files on UNIX/Linux:

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q /usr/include

This command usually takes a very long time to finish, and finally it gives a quite large tags file, which causes the editor or IDE a long time to search this tags file for symbols. To solve this problem, I came up with another idea.

Why must we generate a tags file containing all the symbols in the system header? If we only generate the tags file only for the header files that are related to our projects, would it be faster? That’s the point of this idea. We could first search for the header files that are included in our projects, and then we use ctags to generate a tags file for these files and our source files, in this way, a much smaller tags file that containing all the symbols that maybe useful for the project is generated.

Continue reading

Use EditorConfig to Maintain Consistent Coding Styles between Different Editors and IDEs

Last updated on October 1, 2016

Usually for a project with more than one developer involved, it is essentially important for the project to define and maintain a consistent coding style. Most code editors and IDEs, such as Vim, Emacs, Code::Blocks, provide settings related to coding styles, such as the width of tab, the size of indentation, end of line, etc. However, it is hard to provide the same settings for different Editors and IDEs: we have to maintain many config files for different editors and IDEs, such as .vimrc for Vim, .emacs for Emacs. In order to solve this, EditorConfig was born. By defining coding style in files named .editorconfig, the EditorConfig plugins for different editors and IDEs will automatically adjust your coding style.

Continue reading

Use SingleCompile to Compile and Run a Single Source File Easily in Vim

Last updated on October 1, 2016

Although Vim itself has already been a very powerful text editor, its plugins make it even better. SingleCompile is a plugin aimed at making it more convenient to compile or run a single source file without leaving Vim.

Consider this situation: you’ve just written a small C file (or small python script) with Vim for some tiny test, then you need to use :!gcc %:p to compile the C source file and run the executable with :!./a.out command (Or use :!python %:p to run the python script). Although a key mapping could make this process a bit convenient, but many of Vim’s advanced features will become unavailable, such as quickfix, compiler feature. SingleCompile was born to solve this problem, making this process more convenient and powerful than simply defining a few key mappings:

  • Compile or run the source file quickly using quickfix feature and compiler feature of Vim;
  • Auto detecting compilers and interpreters;
  • Fast switch between several installed compilers or interpreters;
  • Multi-language support;
  • Custom your own compiler/interpreter template;
  • View the result of last run command at any time(requires tee command);
  • Run the compiled program asynchronously and view the result at any time (see :SCCompileRunAsync in the help file).

Let’s see more about SingleCompile.

Continue reading