Xah Emacs Blog Archive 2012-08

Emacs Lisp Real-World Case Example: Command to Change Image Link

I work in HTML a lot. Often, i have image links like this:

<img src="i/92834284.jpg" …>

The image name “92834284.jpg” is not descriptive. I want to rename it properly to “my_cat.jpg”.

So, i put cursor there, press a key to select between quotes (in ErgoEmacs, that's select-text-in-quote), copy, then Alt+x shell-command, then type paste mv [paste] ‹new name› to rename the file, and then edit the link to the new name. The multiple steps gets annoying.

Here's a command that does it in one shot:

(defun rename-html-inline-image ($newFilePath)
  "Replace current HTML inline image's file name.

When cursor is in HTML link file path, for example,  <img src=\"gki/macosxlogo.png\" > and this command is called, it'll prompt user for a new name. The link path will be changed to the new name, the corresponding file will also be renamed. The operation is aborted if a name exists."

   (let (
         (defaultInput (expand-file-name
                        (elt (get-selection-or-unit 'filepath) 0)
                        (file-name-directory (or (buffer-file-name) default-directory )) )) )
     (list (read-string "New name: " defaultInput nil defaultInput )) ) )
  (let* (
         (bds (get-selection-or-unit 'filepath))
         ($inputPath (elt bds 0) )
         (p1 (aref bds 1) )
         (p2 (aref bds 2) )
         ($ffp (local-url-to-file-path (expand-file-name $inputPath (file-name-directory (or (buffer-file-name) default-directory )) ))) ;full path
         ;; (setq ξffp (windows-style-path-to-unix (local-url-to-file-path ξffp)))

    (if (file-exists-p $newFilePath)
        (progn (error "file 「%s」 exist." $newFilePath ))
        (rename-file $ffp $newFilePath )
        (message "rename to %s" $newFilePath)
        (delete-region p1 p2)
        (insert (xahsite-filepath-to-href-value $newFilePath (or (buffer-file-name) default-directory)))

Note: this command uses {get-selection-or-unit, local-url-to-file-path, xahsite-filepath-to-href-value}. They are commands i wrote for managing my site. If you have a simple site or blog, you don't need them. You can use thing-at-point in place of get-selection-or-unit. For getting the link path, you can use a combination of {buffer-file-name, expand-file-name, file-relative-name, file-name-directory}.

emacs: navigating brackets

emacs by default have commands to navigate source code bracket tree.

very useful in lisp code. Try it!

but in C-syntax code, i find it more useful to freely jump to any prev/next brackets.

here's the code Emacs: Move Cursor to Bracket 🚀

i have 【Alt+arrow】 keys for them. (if you are using ErgoEmacs keybinding, it's already there)

i use these together with extend-selection command. For example, when cursor is on a left bracket (any paren, curly, square, or asian brackets), then you press a key, then the whole braketed text will be selected, then you can cut or delete.

You can find the code for extend-selection here: Emacs Select Word Command Problem (or, if you have expand region package installed, that works too.)

there's also select-current-line and select-current-block. Very useful.

Emacs Lisp: using let*

didn't realize that let* is in elisp (i thought it was in cl package).

let* differs from let in that it allows subsequent variables to use values of previous variables. Example:

(let* (
       (a 1)
       (b (+ a 1))
  ) ; returns 2

without let*, you have to do like this:

(let (
      (a 1)
  (setq b (+ a 1) )

updated: Emacs Init: Set Default Major Mode.

Ask Emacs Tuesday.

emacs: define a key today!

in emacs, everything is a command. Then, the most important thing are keys. Any command can have a key.

If you find yourself calling a command often that doesn't have a key, or default key too long, it's very convenient to give it a key. Single keys such as F7, or Ctrl+8, etc are all good.

i've been adding/changing my emacs keys every week for the past few years. I even have a key to open my key config file. Like this:

(global-set-key (kbd "<kp-7> <kp-5>")
  (lambda () "" (interactive)
   (find-file "~/.emacs.d/xah_emacs_init/xah_emacs_keybinding.el")))

That means, i press the sequence 7 5 on the number pad to open my key config file.

Typically, when i find myself using a command often (you'll just notice it), i give it a key. Defining a key to a command takes about 5 seconds. However, the time drain is thinking about which key to use. I'm still stuck on that sometimes. But most of the time, i try to not to think about it much and simply pick whatever unused key comes to mind. It's like guerrilla-warfare tactics. This way, i can immediately start to benefit from the convenience of the new key. Every ~6 months, i try to organize the keys. That's usually very painful, because of muscle memory and habit. However, the whole thing is still better than not to define keys.

Emacs Keys: Define Key

emacs is a workstation. From which, you launch nuclear rockets, among other things.

my emacs elisp tutorial is now $5 bucks. BUY it, you beautiful beings.

the PASSION cannot be stopped!

Jon Snader's Lisp Puzzles Blog

I want to thank Jon Snader (jcs) who kept saying good things about my elisp blog. Jon writes many very interesting programing puzzles, and shows elegant solution in emacs lisp or Common Lisp. For examples, here's some recent ones that are my favorite:

Emacs 24 New Command: rectangle-number-lines

Old news, but worth mentioning again. rectangle-number-linesCtrl+x r N】 will insert a vertical column of numbers into a block of text, like this:

1 rectangle-number-lines
2 rectangle-number-lines
3 rectangle-number-lines
4 rectangle-number-lines

You can first make a text selection and it'll insert a sequence of numbers vertically at cursor start position.

To start with a different number, press Ctrl+u first.

For emacs 23 users, you can define your own, like this:

(defun insert-column-counter (n)
  "Insert a sequence of numbers vertically.
For example, if your text is:

a b
c d
e f

and your cursor is after “a”, then calling this function with argument
3 will change it to become:

a1 b
c2 d
e3 f

If there are not enough existing lines after the cursor
when this function is called, it aborts at the last line.

This command is conveniently used together with `kill-rectangle' and `string-rectangle'."
  (interactive "nEnter the max integer: ")
  (let ((i 1) colpos )
    (setq colpos (- (point) (line-beginning-position)))
    (while (<= i n)
      (insert (number-to-string i))
      (forward-line) (beginning-of-line) (forward-char colpos)
      (setq i (1+ i))

See also: Emacs: Edit Column Text, Rectangle.

Archived at: Emacs 24 (Released 2012-06).

Other emacs bloggers have also blogged about this: For example: http://emacsworld.blogspot.fi/2012/06/numbering-lines-and-lists-in-emacs.html and Dirk-Jan C Binnema (djcb) shows other ways to do the same, at: http://emacs-fu.blogspot.com/2012/07/replace-regexp-and-numbering-lines.html

ErgoEmacs repository is now in git. Get your clone at https://code.google.com/p/ergoemacs/source/checkout

If you have scripts that access it using svn, it will still work but only on the version as of . You'll need to switch to git for latest source code.

Much thanks to Jorge Dias [http://mrdias.com/ ], David Capello [https://davidcapello.com/], Thomas Koch 2012-08-15[ http://www.koch.ro/ ].

Let me know if you have any questions. https://plus.google.com/b/113859563190964307534/113859563190964307534/posts or [ https://twitter.com/ErgoEmacs ] or [ https://www.facebook.com/xahlee ] or [ https://groups.google.com/group/ergoemacs ]

Ask Emacs Tuesday.

Updated: Emacs: Regular Expression

TextMate Gone Open Source

Xah Emacs/Elisp Tutorial Update 2012-08-09

New version of my emacs tutorial ready.

• If you've bought it before, please do email to Xah@XahLee.org with subject “emacs tutorial upgrade”.

• If not, why not? Let me know and i'll do what i can to get your money.

Get it for $10. At: Buy Xah Emacs Tutorial. Currently about 350 HTML pages, about 1k pages printed.

Ask Emacs Tuesday.

can emacs have anything to do with Olympics?

updated Emacs: Organize Init File

This tip seems very popular. Emacs: Flow Buffer from Left to Right Window Like Newspaper Columns. Thanks to [Gert Meulyzer https://plus.google.com/106034493580391849763/about] for bringing this up yesterday.