Category Archives:Programming Tools
Quickly Switch Between HTML and Plain Text inmu4e
Last updated on December 29, 2024
Mu4e
defaults loading messages in HTML with shr. While this works well most of the time, sometimes a plain text version is more readable. Therefore, it is desirable to create a key binding to switch between these two versions.
I have the following in my Emacs configuration:
;; Quickly switching between plain text and HTML mime type.(keymap-set mu4e-view-mode-map (kbd "K") (lambda () (interactive) (gnus-article-jump-to-part 1) (gnus-article-press-button) (gnus-article-press-button)))
This binds keyShift+K to switch between plain text and HTML versions of a message, if the message has both versions available.
Attachment Reminder in Emacsmessage-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 modemessage-mode
, which is used in email clients such asgnus andmu4e. Here is an attachment reminder based onthis 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 functionbrowse-url
in Emacs can be used to start a browser process to visit a given URL. It is used by many packages such asorg-mode
,mu4e
, etc. However, on GNU/Linux, by default a new browser process started bybrowse-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 thesetsid
andgnome-open
commands available. Note that replacinggnome-open
withxdg-open
is not guaranteed to work.
Installing Emacs from Source: Avoid the Conflict ofctags
Last updated on December 10, 2016
If installing Emacs from source, an executable namedctags
will be installed by default. However, many systems such as GNU/Linux, FreeBSD and macOS often already have an executable namedctags
installed, such asExuberant Ctags orUniversal Ctags, which then causes conflict. To avoid this conflict, we can use the switch--program-transform-name='s/^ctags$/ctags.emacs/'
when runningconfigure
to renamectags
toctags.emacs
. For example:
cd /path/to/emacs-sourcemkdir build && cd build../configure --program-transform-name='s/^ctags$/ctags.emacs/'
After that, runningmake install
will install the Emacsctags
executable asctags.emacs
.
Emacs ElDoc: Display Function or Variable Information Near Point (Cursor)
Last updated on December 10, 2016
ElDoc mode is a minor mode that displays information about a function or variable in the text at point (cursor). However, by default, it displays the information in the echo area, which isn't quite ideal since our eyes often need to traverse half of the screen to view that information:
Emacs: Disable Certain Pairs forelectric-pair-mode
Last updated on December 10, 2016
InGNU 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 ofelectric-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{}
inweb-mode
(to allowweb-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 variableelectric-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 variabledired-listing-switches
, it is still not convenient to switch between different sorting criteria quickly. For this reason, I created the Emacs extensiondired-quick-sort to make sorting Dired an easy story.

Live Preview of LaTeX in Vim
Last updated on October 1, 2016
Vim, as a highly configurable and customizable editor, already has a powerfulVim-LaTeX plugin. However, I don’t like the lack of live preview feature. For this reason, I developed a plugin calledvim-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 toAsis Hallab).
Note: This plugin currently doesn’t work on Windows.
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 useExuberant Ctags. If you are usingother versions of ctags, this post may not be useful.
When usingctags 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 asVim, 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.
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 consistentcoding style. Most code editors and IDEs, such asVim,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 forVim,.emacs forEmacs. In order to solve this,EditorConfig was born. By defining coding style in files named.editorconfig
, theEditorConfig plugins for different editors and IDEs will automatically adjust your coding style.