Category Archives: Emacs

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