At times it’s necessary to manually force a non-cache reload/refresh of a page, overriding what browsers (wrongly) interpret as when the last change of a site happened. (The cache is a temporary storage area where frequently accessed data can be stored for rapid access in our hard drive.)
To do this in FireFox, hold down SHIFT and click the RELOAD button. In Internet Explorer, hold down CTRL and click the REFRESH button. Other browsers can allow this quick bypass which I was glad to discover, very handy and much simpler than erasing the whole cache. Check these instructions containing others like Safari, Chrome, Konkeror and Flock.

To remove empty lines in a region simply do:

M-x flush-lines RET ^$ RET

or, if blank lines contain some white spaces characters:

M-x flush-lines RET ^\W*$ RET

Whereas with sed (which I use inside emacs via M-| shell-command-on-region ) it’s simply:

sed -e '/^[	 ]*$/d'
(this is either a tab or a space, press the TAB key since most versions
of sed don't recognize the \t character)

How easy is to forget this type of things!

Didn’t know before that “M-x dirs” queries the shell and resyncs Emacs’ idea of what the current directory stack is. Tiny but useful piece of knowledge.

This is more of quick reminder for myself on the difficult task of escaping quotes and running an awk or sed command inside the bash command line. I solved the issue in my case using the octal representation of a single quote (\47), taken from here, where more options are shown.

Having previously accommodated the list of stale links with their replacements in a two columns format that looked like:

http://addressOld.com http://www.goodWorkingOne.com
http://addressOld2.com http://www.goodWorkingOne2.com
etc…

I used the following:

awk '{print "UPDATE library SET url=\47"$2"\47 WHERE url=\47"$1"\47;"}' 

to correctly generated what I wanted sql-postgres statements to update the links table:

UPDATE links SET url='http://goodWorkingOne.com' WHERE url='http://addressOld.com';

Traveling with my kid I had some medication to give him the other night. I didn’t have an alarm clock with me, only my laptop computer. Into the Windows system I found nothing suited to help me gain some rest while waking up again at 3:00 am. I wished Emacs could help, and it certainly didn’t disappoint me once more. I found a simple alarm clock coded by Mathias Dahl here . Great!, I only had to tweak it a little by adding a sound load enough to ring me at the desired time. I picked a rooster sing, which I had collected a while ago when my child was getting to know farm animals. I put a loop around to repeat it a few times so to guarantee the awakening.
Anyways, for what is worth, here goes the cool little snippet:

(defvar alarm-clock-timer nil
  "Keep timer so that the user can cancel the alarm")

(defun alarm-clock-message (text)
  "The actual alarm action"
  (progn
    (let((i 0))
      (while (< i 3)
        (play-sound-file "c:/web/sounds/rooster.wav")
        (setq i (1+ i)))
      (message-box text))))

(defun alarm-clock ()
  "Set an alarm.
The time format is the same accepted by `run-at-time'.  For
example \"11:30am\"."
  (interactive)
  (let ((time (read-string "Time: "))
        (text (read-string "Alarm message: ")))
    (progn
      (setq alarm-clock-timer
          (run-at-time time nil 'alarm-clock-message text)))))

(defun alarm-clock-cancel ()
  "Cancel the alarm clock"
  (interactive)
  (cancel-timer alarm-clock-timer))

Using an interactive shell in Emacs for Windows you might enconter a bug that causes the shell process to terminate when you signal and eof (See point 7.6 here)
If, for example, you try:

/Program Files/Emacs/ cat >newfile
this is a test file
being created with the cat command
directly from the user input 
in shell.

Now, by pressing C-d (or C-c C-d) the input should finish here, and the prompt return, but what happens instead, is that the shell process terminates. Same thing occurs if you try to M-x comint-send-eof or if you pick an eof signal from the menu.

I had noticed that before (in my Windows emac, only not under Unix), but last night I found that an eof sign could be generated by typing C-q C-z.

I choose to set an alias and bind it to some keys in my .emacs to overcome the issue:

(defalias 'eof
  (read-kbd-macro "C-q C-z RET"))
(global-set-key [M-S-f8] 'eof)

Of course a better solution would be to advice the comint-send-eof to procede accordingly when in emacs for Windows. Anybody knows how to do it, or mind to share a stronger fix than this hack? Any feedback will be appreciated.

Update:
Actually there’s no need to set an alias, as I learned that this is also possible and more straight-forward:

(global-set-key [(meta shift f8)]
 '(lambda () (interactive) (execute-kbd-macro((read-kbd-macro "C-q C-z RET")))))

Knowing how to do loops in awk is helpful to address a range of fields. If for example, we want to output only from second, to prior to last field. Say we’ve got :

one:two:three:four:five:six:seven:eight:nine

Fields are here defined by “:” separator, so using:

awk -F ':' '{for(i=2;i<NF;i++) printf "%s:",$i}'




We will get:

two:three:four:five:six:seven:eight:

On how to delete a chunk of text contained in multiple lines: use sed to catch the range (/a/,/b/)

There were some javascript google ads calls from a page I downloaded via wget, doing:

 wget www.someSite.com/somePage.html > toCleanItUp.htlm

After highlighting all the page C-x h I did a M-x shell-command-on-region and used the following:

sed "/<script .*>/,/<\/script>/d" > nowItIsClean.html

If feeling lazy, (or if I don’t have the file opened already in an emacs buffer), there’s the straightforward way of using cat:

cat toCleanItUp.html | sed "/<script .*>/,/<\/script>/d" > nowItIsClean.html

Ah, the beauty of unix tools!

This was once the most important portion of elisp code I had put into my dot emacs.  Thanks to it I began to conciliate with the editor which I sort of resisted at first, by finding it foreign to my habits and liking. I was accustomed to the way Dreamveaver handled the indenting of selected text in blocks, so getting emacs to behave in such familiar and practical style was critical to ease the experience of using it in a daily basis -as I needed  a couple of years ago when entering a new position.

Pretty soon I saw why emacs was so superior in regard of its extensibility: with basic elisp knowledge and a few lines of code I was able to get indent/unindent function the way I wanted.  Now to select text as a block, I not longer needed to set the mark and point exactly from the beginning to the end of a line or group of lines. The code conveniently extended the highlighted area via the tab key, turning the sub-selection into a block containing all the text of the line(s) involved.

Besides, the selection was configured so to not disappear unless you click outside it, that is, while keeping  pressed the tab (or Shift-tab) key(s) you could get the text to shift continuously right (or left).

Having bound the tab key with the indentation functions required a few adjustment to make it context-sensitive. Here is how the tab key behaves after the changes:

  • First, to insert a real tab space, (when the cursor is before a word), you need to use Ctrl -q-tab.
  • If the text point is, at least, a space away from the end of a word, a standard tab-space is inserted.
  • If the text point is exactly at the end of a word, the tab uses the hippie-expand function to auto-complete the word. If Shift+tab are used instead, words are deleted backward one at a time up to the beginning of the line.
  • When editing in org-mode an exception was set to respect its visibility cycling function (org-mode cleverly uses the tab key to rotate the current state of tree and sub-trees).

Finally, Mac and PC users would probably appreciate the added function that makes Shift + click to end a selection of text, which is the default way on both platforms (I grabbed it here, from Dave Peck).

So, as you can see, the whole point of the code below is to ease the manual indentation of code. Not being comfortable with the automatic indentation in emacs, I like to avoid it at times, or for certain modes. Check the associated hack posted on how to deactivate it.

Please, have a look at the elisp lines. Remember that you could copy and paste all into the scratch buffer and do “M-x evaluate-buffer” to get them immediately working.

;; turn on transient mark mode
;;(that is, we highlight the selected text)
(transient-mark-mode t)

(setq my-tab-width 2)

(defun indent-block()
  (shift-region my-tab-width)
  (setq deactivate-mark nil))

(defun unindent-block()
  (shift-region (- my-tab-width))
  (setq deactivate-mark nil))

(defun shift-region(numcols)
" my trick to expand the region to the beginning and end of the area selected
 much in the handy way I liked in the Dreamweaver editor."
  (if (< (point)(mark))
    (if (not(bolp))    (progn (beginning-of-line)(exchange-point-and-mark) (end-of-line)))
    (progn (end-of-line)(exchange-point-and-mark)(beginning-of-line)))
  (setq region-start (region-beginning))
  (setq region-finish (region-end))
  (save-excursion
    (if (< (point) (mark)) (exchange-point-and-mark))
    (let ((save-mark (mark)))
      (indent-rigidly region-start region-finish numcols))))

(defun indent-or-complete ()
  "Indent region selected as a block; if no selection present either indent according to mode,
or expand the word preceding point. "
  (interactive)
  (if  mark-active
      (indent-block)
    (if (looking-at "\\>")
  (hippie-expand nil)
      (insert "\t"))))

(defun my-unindent()
  "Unindent line, or block if it's a region selected.
When pressing Shift+tab, erase words backward (one at a time) up to the beginning of line.
Now it correctly stops at the beginning of the line when the pointer is at the first char of an indented line. Before the command would (unconveniently)  kill all the white spaces, as well as the last word of the previous line."

  (interactive)
  (if mark-active
      (unindent-block)
    (progn
      (unless(bolp)
        (if (looking-back "^[ \t]*")
            (progn
              ;;"a" holds how many spaces are there to the beginning of the line
              (let ((a (length(buffer-substring-no-properties (point-at-bol) (point)))))
                (progn
                  ;; delete backwards progressively in my-tab-width steps, but without going further of the beginning of line.
                  (if (> a my-tab-width)
                      (delete-backward-char my-tab-width)
                    (backward-delete-char a)))))
          ;; delete tab and spaces first, if at least 2 exist, before removing words
          (progn
            (if(looking-back "[ \t]\\{2,\\}")
                (delete-horizontal-space)
              (backward-kill-word 1))))))))

(add-hook 'find-file-hooks (function (lambda ()
 (unless (eq major-mode 'org-mode)
(local-set-key (kbd "<tab>") 'indent-or-complete)))))

(if (not (eq  major-mode 'org-mode))
    (progn
      (define-key global-map "\t" 'indent-or-complete) ;; with this you have to force tab (C-q-tab) to insert a tab after a word
      (define-key global-map [S-tab] 'my-unindent)
      (define-key global-map [C-S-tab] 'my-unindent)))

;; mac and pc users would like selecting text this way
(defun dave-shift-mouse-select (event)
 “Set the mark and then move point to the position clicked on with
 the mouse. This should be bound to a mouse click event type.”
 (interactive “e”)
 (mouse-minibuffer-check event)
 (if mark-active (exchange-point-and-mark))
 (set-mark-command nil)
 ;; Use event-end in case called from mouse-drag-region.
 ;; If EVENT is a click, event-end and event-start give same value.
 (posn-set-point (event-end event)))

;; be aware that this overrides the function for picking a font. you can still call the command
;; directly from the minibufer doing: "M-x mouse-set-font"
(define-key global-map [S-down-mouse-1] 'dave-shift-mouse-select)

;; to use in into emacs for  unix I  needed this instead
; define-key global-map [S-mouse-1] 'dave-shift-mouse-select)

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; this final line is only necessary to escape the *scratch* fundamental-mode
;; and let this demonstration work
(text-mode)

NOTE:
I revised the code and modified some things. Especially the function ‘my-unindent” in order to add some sophistication on how the shifting backwards now occurs. The main improvement is that deleting backwards will never lead to jumping to the previous line (and removing words from there) . Here’s how the movement to left is done when pressing Shift + tab:

  • killing word by word, if the insertion point is at the right of any text.
  • stepping back, in increments (set by the variable my-tab-width), BUT ALWAYS stopping at the beginning of the line when the cursor is at the left of any indented text.

You can see how much simpler the function looked before I decided to better it:

(defun my-unindent()
  "Unindent line, or block if it's a region selected"
  (interactive)
  (if mark-active
      (unindent-block)
    (if(not(bolp))(delete-backward-char 2 ))))

I know this is subjective, but it didn’t feel right being overridden by the editor rules on how the code should look. For quite a while until recently could only partially deactivate the mechanism that automatically handles indentation (there are dozens of functions and variables involved). Luckily I came across a hack that allows to turn off all syntactic indentation that happens within a mode.


; I don't want emacs making changes I can't control in regards of the indentation.
; This is a hairy issue but I want php to be just a major mode with syntax coloring and bracket balancing.

; to use PHP mode,
(autoload 'php-mode "php-mode" "PHP editing mode" t)
(add-to-list 'auto-mode-alist '("\\.php3\\'" . php-mode))
(add-to-list 'auto-mode-alist '("\\.php\\'" . php-mode))

(add-hook 'php-mode-hook
          '(lambda ()
             (define-key php-mode-map (kbd "C-c C-c") 'comment-or-uncomment-region)
             (setq tab-width 4)
             (highlight-regexp "&lt;\\?php" 'hi-blue)
             (highlight-regexp "\\?&gt;" 'hi-blue)))

(defun my-php-setup ()
  (local-set-key (kbd "TAB") 'tab-to-tab-stop)
  (local-set-key (kbd "{") 'self-insert-command) ; support for "{" needed
  (local-set-key (kbd "}") 'self-insert-command)
  (local-set-key (kbd ":") 'self-insert-command)
  (local-set-key (kbd ";") 'self-insert-command))

(add-hook 'php-mode-hook 'my-php-setup)