Emacs

Surviving with Emacs/VM mail client where everybody else uses MS Outlook

Using Emacs and VM for e-mail in a company that has a lot of MS Outlook employees can be a difficult task. Microsoft Outlook handles e-mails in a non-standard way in many cases. Reading e-mails properly and making your e-mails properly read by others can be tough tasks. This post addresses some issues.

Removing HTML part from text messages

Outlook always includes an HTML portion of the message as well, even if the message was composed as “Plain text”. This function removes the HTML part, preserving only the text portion.

(defun upn-mail-remove-html ()
  "Removes HTML portions from reply e-mails."
  (interactive)
  (let (old-case-fold-val)
	 (setq old-case-fold-val case-fold-search)
	 (setq case-fold-search t)
	 (when (search-forward "")
			 (delete-region beg (point)))))
	 (setq case-fold-search old-case-fold-val)))

The above function can be conveniently added in the supercite’s post-hook so that it will be done after supercite prepares the reply buffer.

(add-hook 'sc-post-hook 'upn-mail-remove-html)

While all other mail clients show a name as “<FirstName> <LastName>”, Outlook shows it as “<LastName>, <FirstName>”, thereby breaking VM’s handling of the names and generating the right supercite prefixes. The following function extracts the first name and last name from both mail formats.

;; The following one matches the name from MS Outlook.
(defconst upn-ms-outlook-name-regexp "\\([^,\"]+\\),[ \t]+\\([^,\"]+\\)")

;; The following one matches the name from all "sensible" mailers.
(defconst upn-normal-name-regexp "\\([^\"<>]+\\)[ \t]+\\([^\"<>]+\\)")

(defun upn-get-ms-outlook-name (fromline)
  "Extracts First name and last name from a name in MS Outlook format.
   Returns nil if it doesn't match."
  (let (matched-retval)
    (setq matched-retval (string-match upn-ms-outlook-name-regexp fromline) )

    (if (not matched-retval)
        nil
      (let (
            (firstword (substring fromline (match-beginning 1) (match-end 1)))
            (secondword  (substring fromline (match-beginning 2) (match-end 2)))
            )
        ;; return the firstname which will be the second word:
        (list secondword firstword)))))

This function extract the same information from a normal name.

(defun upn-get-normal-name (fromline)
"Extracts First name and last name from a name in a normal format.
  Returns nil if it doesn't match."
  (let (matched-retval)
    (setq matched-retval (string-match upn-normal-name-regexp fromline) )

    (if (not matched-retval)
        nil
      (let (
            (firstword (substring fromline (match-beginning 1) (match-end 1)))
            (secondword  (substring fromline (match-beginning 2) (match-end 2)))
            )
        ;; return the firstname which will be the second word:
        (list firstword secondword)))))

The following function can be used to extract the name info from either format.

(defun upn-get-first-last-name (fromline)
  "Gets the first name and last name from the From: line of a mail header.
Handles normal names and names from MS outlook."
  (let (result)
    ;; Most people are rotten by Exchange sever, so check it first
    (setq result (upn-get-ms-outlook-name fromline))
    (unless result

      ;; Now check the normal one
      (setq result (upn-get-normal-name fromline))
      (unless result

        ;; Set the name as nil, so the calling function can take action
        (setq result nil)))

    ;; Return result
    (or result)))

The following function generates a word from the first name and the initial showing the last name from a name of either format, to be used as the supercite prefix.

(defun upn-get-FirstL-name (namestring)
  "Gets a string formed by a capitalized first name appended with the
first letter of the last name capitalized, from the From: header of an
e-mail.  This is used as the attribution if one has not been
provided."
  (let (result result-list)
    (setq result-list (upn-get-first-last-name namestring))
    (if (not result-list)
        (setq result namestring)
      (setq result
            (concat (capitalize (car result-list))
                    (upcase (substring (cadr result-list) 0 1)))))
    (or result)))

Emacs

Comments (0)

Permalink

Emacs: Comment and duplicate

The following Emacs Lisp function duplicates the current line and comments the first line (using the prefix for the current mode) .

(defun upn-comment-and-duplicate ()
  "Comments current line after duplicating."
  (interactive)
  (let (
        (beg (line-beginning-position))
        (end (+ 1 (line-end-position))))
    (copy-region-as-kill beg end)
    (beginning-of-line)
    (yank)
    (comment-region beg end)))

Emacs

Comments (0)

Permalink

Emacs: Extended point-to-register and jump-to-register

When working in Emacs, we may need to temporarily “bookmark” some places so that we can later jump to that line in the same session. For example, you want to search for something, but after done with that, you want to come back to the place you were initially. These routines allow to store upto 10 such points and later jump to those places.

The function upn-point-to-register stores the point to register ‘0′. It can be invoked with a numeric argument (0-9), and the point is saved in that register.

The function upn-jump-to-register jumps to register ‘0′. With a numeric argument, the cursor jumps to that register.

;; Quick point-to-register and jump-to-register
(defun upn-point-to-register (arg)
  "Puts point to register '0'.  With a numeric prefix argument, puts
point to the register given by the first digit of the argument."
  (interactive "P")
  (let ((register-name ?0))
    (if arg
        ;; There is some bug here
        (setq register-name (string-to-char (number-to-string arg))))
    (point-to-register register-name)))

(defun upn-jump-to-register (arg)
  "Jumps to register '0'.  With a numeric prefix argument, jumps to
the register given by the first digit of the argument."
  (interactive "P")
  (let ((register-name ?0))
    (if arg
        ;; There is some bug here
        (setq register-name (string-to-char (number-to-string arg))))
    (jump-to-register register-name)))

;; The following key-bindings are in upn-key-bindings.el
;; (global-set-key [C-f7]           'upn-point-to-register)
;; (global-set-key [M-f7]           'upn-jump-to-register)

I bound these two defuns to C-f7 and M-f7, so that I can easily provide the numeric argument. For example, C-4 C-f7 will put the current point into register ‘4′, while M-4 M-f7 will jump to register ‘4′.

Emacs

Comments (0)

Permalink

Emulate vi’s dot (.) command in Emacs

One of the most powerful commands vi has that emacs doesn’t have is the dot (.) command that repeats the previous command. If you use emacs what will you do if you are addicted to vi’s dot command?

There is no adequate answer. Some answers are

  1. Use the key sequence C-x z if you have version 20.3. or later. Equivalent to the command repeat.
  2. Use the key sequence C-x <ESC> <ESC>, for repeat-complex-command. You can even navigate to the previous complex command using M-p and M-n.
  3. Get vi-dot.el by Will Mengarini. Check Will Mengarini’s homepage. If not found, try a google search on vi-dot.el.
  4. Use macros for repeating complex commands.
  5. Use any vi-emulating modes in emacs, like VIPER.
  6. Use vi :-)

Emacs
vi/vim

Comments (0)

Permalink

Emulating vi’s % command in Emacs

In vi/vim, The % command will match any parenthesis/bracket for its counterpart. The same thing can be achieved in emacs by one of the following ways:

  1. In all language modes, the following keys work:
       M-C-b          : Goto starting bracket ({, (, < etc.)
       M-C-f          : Goto ending bracket (}, ), > etc.)
       M-C-a          : Goto beginning of function
       M-C-e          : Goto end of function
       
  2. If you want the exact behavior of vi “%” command, matching all parens, use the following elisp code:
    (defun match-parenthesis ()
      "Go to the matching parenthesis if on parenthesis else insert %."
      (interactive)
      (cond ((looking-at "[([{]") (forward-sexp 1) (backward-char))
    		  ((looking-at "[])}]") (forward-char) (backward-sexp 1))
    		  (t (self-insert-command 1))))
    (global-set-key "%" 'match-parenthesis)
        

    It matches the parenthesis if the point is on a parenthesis, otherwise inserts the character ‘%’.

Emacs
vi/vim

Comments (0)

Permalink