Monday, May 23, 2011

Emacs flush duplicated lines function

(defun flush-duplicated-lines (&optional arg)
  "Flush duplicated lines down on buffer. When region is active,
operate on region instead of rest of buffer.

By default it will flush only exatly matched lines only dropping
spaces on begin and end, prefix argument allows specify more
options."
  (interactive "P")

  (flet ((option (name default-value)
                 (if arg
                     (read-from-minibuffer (concat name " (default " (format "%S" default-value) "): "))
                   default-value)))
    
    (let ((start (if (region-active-p)
                     (region-beginning)
                   (point)))
          (end (if (region-active-p)
                   (region-end)
                 (buffer-end 1)))

          (line-side-ignore-space (not (string-equal
                                        (option "Ignore spaces on sides of line?" t)
                                        ""))))

      (labels ((shift-to-next-line ()
                                   (and (zerop (forward-line 1))
                                        (<= (line-end-position)
                                            end)))
                (chomp (str)
                      (let ((s (if (symbolp str) (symbol-name str) str)))
                        (replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" "" s)))
               
                (line-matches (line line-matcher)
                
                              (and (<= (line-end-position)
                                       end)
                                   (string-equal
                                    (if line-side-ignore-space
                                        (chomp line)
                                      line)
                                    (if line-side-ignore-space
                                        (chomp line-matcher)
                                      line-matcher))))
               
               (current-line ()
                             (buffer-substring-no-properties (line-beginning-position)
                                                             (line-end-position)))

               (delete-current-line ()

                                    (setf end
                                          (- end
                                             (- (line-end-position)
                                                (line-beginning-position))
                                             1))
                                    
                                    ;; delete line region
                                    (delete-region (line-beginning-position)
                                                   (line-end-position))
                                    ;; delete newline character
                                    (delete-char 1))

               (delete-lines-matching (line-matcher)
                                      (save-excursion
                                        (when (shift-to-next-line)
                                          (loop if (line-matches (current-line)
                                                                 line-matcher)

                                                do (delete-current-line)
                                                
                                                else if (not (shift-to-next-line))
                                                return nil)))))
        (save-excursion
          (goto-char start)
          (loop do (delete-lines-matching (current-line))
                while (shift-to-next-line)))))))

Thursday, May 19, 2011

Ubuntu Linux and Hewlett-Packard LaserJet P1566

So.I have bought this printer as i need laser printer for the PCB and documentation printing.

And directly after connecting it to the Linux box, the problem arised.

The HPLIP drivers package not saw printer, saying "error: No devices found on bus: usb" in console when i tried to do "hp-setup -i".
I tried to pass printer's usb device id as argument to the setup utility to make it see printer and it was detected, but failed to work: any try to access the printer lead to the something like:

error: Unable to communicate with device (code=12): hp:/usb/HP_LaserJet_Professional_P1566?serial=000000000QB03350SI1chp-toolbox(UI)
[3445]: debug: Exception: 2 (Device not found)
error: Unable to open device hp:/usb/HP_LaserJet_Professional_P1566?serial=000000000QB03350SI1c.


Googling not helped, it looks like I first who have this problem with this printer.
Two days i was in searching of solution, even asked a question on Launchpad: Launchpad Question #158013, but in vane.

So today i realized: Smart Install.Smart Install mode is the mode when printer shows self as media drive to the system with drivers for Windows. Unfortunately, by some strange occasion, when printer in this mode, it is unusable with the HPLIP drivers package.

So what you can do with this?
As far as I know for now - you'll need a windows machine to disable Smart Install mode with utility provided on the printer CD, which located in UTIL directory.

I have tried to pass USB with printer to Qemu with windows but had no luck - system hanged.
I have tried to run utility in the Wine, but it will not see the printer.

So, i asked about disabling this feature on launchpad, so it possble will have and answer after some time.

But now it seems you will need a windows machine at least once to use this printer.
Sadly.

Thursday, March 4, 2010

Screen + Midnight Commander + Shift + F6!

I have expiriencing this problem for rather long time: Shift + F6 in midnight commander under screen behaves like F8 (delete file). Google have rather small amount information about this, so I have to use it as it is.

But fortunately i found a solution: when using screen, environment variable TERM is set to "screen". So, we just need to set it back to "xterm" or whatever worked on your host machine. And viola, shift + f6 works as needed.

Friday, December 11, 2009

Hard Android vs Soft Keyboard

I'm now porting BlackBerry application to Android platform and had to use dialogs at this platform. You know, this is some of Dialog objects. Rather good popup windows, capable to carry some useful view payload. But...

I have encountered curious problem (not first, to be honest, during this project): if dialog shown, hidden and after that soft keyboard is shown - view will not be resized to make space for keyboard, instead keyboard will overlay on view.
So, there is possible situation, when editbox, which is located on bottom of view, will be obscured with keyboard. When it get focus, it will request self to be visible and view will be paned to make it visible. But with no way back, as view paned entirely!

I google'd and found this email (thanks to it's author, Oceanedge, which gived right digging direction). In this email written, that
android:windowSoftInputMode=adjustResize
will not help, but this is not correct.

It seems that main reason of my problem is that dialog sets "adjust pan" mode on window, while nobody setted "adjust resize" back again. So, problem was solved by adding code, which resets "adjust rezize" on activity window when dialog is dismissed:

_activity.getWindow()
.
setSoftInputMode(WindowManager.LayoutParams
.
SOFT_INPUT_ADJUST_RESIZE);


So, this is solution for problem with resize view/window for keyboard after dialog pops up. Hope this will help anyone.

Thursday, June 4, 2009

A bit about music sorting.

As man, who likes to listen music as background of my working, I encountered a problem with getting/sorting new music. Old good music is still good, but it still old....

I can't just listen every track before adding it to my playlist as this can really take forever. (:

As background of this story, imagine i have server with a lot of music of different styles/groups etc, but i don't know which group will be good for me or which music is totally crap from my POV.

Some time ago i encountered information about "union" filesystems, particulary funionfs, and this gave the direction for my subconsciousness to work on problem of sorting music. And this is solution i finally came to.

As i'm using the mocp player, there is no problem to obtain currently playing track information from script. This was the base.

Setup step 1, creating music sorting playground:

Using funionfs establishing "out" directory (entry from /etc/fstab):

funionfs#/home/elk/.music_sort/tmp   \
/home/elk/.music_sort/out \
fuse \
rw,allow_other,dirs=/home/elk/music=ro 0 0


So, after this, we will have "out" in /home/elk/.music_sort/out, where player should search for music and play music from. /home/elk/music is directory, where music is actually located (and where is symlink to server music).

So, now, if someone will delete some file in /home/elk/.music_sort/out, file will not be actually deleted, but will be marked as deleted in /home/elk/.music_sort/tmp.

But with deleting not all so easy, as i encountered some problems with "deleting" files on server. Server is cifs share mounted and in /home/elk/music is symbolic link to place, where this music is located, and any unification fs i have tried expirienced different troubles with
deleting in such circumstances.

So, instead of deleting files via funionfs, script creates deleted-markers in /home/elk/.music_sort/tmp.

Setup step 2, script to do "deleting":


#!/bin/bash

# this part obtains music file information
FILENAME=`mocp -i | grep -o "File:.*" | sed 's/File: //'`
TRACKNAME=`mocp -i | grep -o "SongTitle:.*" | sed 's/SongTitle: //'`
AUTHOR=`mocp -i | grep -o "Artist:.*" | sed 's/Artist: //'`

SORTDIR='/home/elk/.music_sort/out'

# this part produces dialog
POSITION="-G +950+28"

DIRNAME=`dirname "$FILENAME"`

# if file not in music directory, we should not be able to delete it, as
# this can lead to some unexpected behaviour

if [ "$SORTDIR" != "${FILENAME:0:25}" ] ; then
export MAIN_DIALOG="
<vbox>
<frame Track>
<text>
<label>\"$AUTHOR - $TRACKNAME\"</label>
</text>
</frame>
<frame File>
<text>
<label>\"$FILENAME\"</label>
</text>
</frame>
<frame Directory>
<text>
<label>\"$DIRNAME\"</label>
</text>
</frame>

<frame Can not be deleted>
<text>
<label>Not in music sorting dir, so can not be deleted.</label>
</text>
</frame>
<button ok></button>
</vbox>"


gtkdialog $POSITION --program MAIN_DIALOG
exit 1
fi

export MAIN_DIALOG="
<vbox>
<frame Track>
<text>
<label>\"$AUTHOR - $TRACKNAME\"</label>
</text>
</frame>
<frame File>
<text>
<label>\"$FILENAME\"</label>
</text>
</frame>
<frame Directory>
<text>
<label>\"$DIRNAME\"</label>
</text>
</frame>

<frame Actions:>
<hbox>
<button>
<label> Delete track </label>
<action type=\"exit\">track</action>
</button>
<button>
<label> Delete dir </label>
<action type=\"exit\">dir</action>
</button>
<button cancel></button>
</hbox>
</frame>
</vbox>
"


eval `gtkdialog $POSITION --program MAIN_DIALOG`
echo exit: $EXIT

# as format for deleting marker for funionfs is filename_DELETED~
# we should create this file instead of dir or file

if [ "$EXIT" == "track" ] ; then
DELETED_FN='/home/elk/.music_sort/tmp'/"${FILENAME:26}"_DELETED~
mkdir -p "`dirname "$DELETED_FN"`"
touch "$DELETED_FN"
mocp -f
elif [ "$EXIT" == "dir" ] ; then
DELETED_FN=`dirname '/home/elk/.music_sort/tmp'/"${FILENAME:26}"`_DELETED~
mkdir -p "`dirname "$DELETED_FN"`"
touch "$DELETED_FN"
mocp -f
fi


There is some comments in script, so there should be no problems to understand it.

So, finally, i added a button on toolbar, which calls this script.

So, looks like this:



So now every time as I want to never hear again track, which is played by now, i press button on toolbar, this dialog appears and i click to "delete dir" or "delete track".

Music is not actually deleted, but my player will not see it anymore.