CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by johnraff on Sat 20th Nov 16:38 (modification of post by johnraff view diff)
View followups from johnraff | diff | download | new post

  1. #!/bin/sh
  2. # shell (hopefully dash!) places menu for openbox
  3. # Usage: add
  4. # <menu id="places" label="Places" execute="/path/to/dash_places_menu.sh ~" />
  5. # to your .config/openbox/menu.xml (you don't need a final slash after the ~)
  6. # or, if you want the "recent files" menu incorporated at the top, use:
  7. # <menu id="places" label="Places" execute="/path/to/dash_places_menu.sh --recent ~" />
  8. # make sure you have recently_opened_menu.sh somewhere, and enter its path below.
  9.  
  10. # path to your "recent files" script, if you want to incorporate it:
  11. recent_script="$HOME"/scripts/recently_opened_menu.sh
  12.  
  13. # Command to open folders at "Browse here..." - any file manager
  14. open_folder_cmd=thunar
  15. # Default command to open files with - others might be xdg-open, gnome-open, pcmanfm...
  16. default_open_cmd=exo-open  # exo-open comes with thunar
  17. # Text editor of choice
  18. text_editor=gedit
  19.  
  20. # function to open files with default open command, or alternative command for certain files
  21. # - add other conditions to choice
  22. open_file() {
  23.         [ -x "$1" ] && exec "$text_editor" "$1"     # comment out this line if you don't want to edit executables instead of executing
  24.         #[ -x "$1" ] && exec "terminator -e" "$1"     # uncomment this and comment out previous line to run executables in terminal instead of editing
  25.         [ "${1##*.}" = desktop ] && exec "$text_editor" "$1"     # comment out this line if you don't want to edit .desktop files instead of executing
  26.         exec "$default_open_cmd" "$1"     # use default open command if above conditions not satisfied
  27. }
  28.  
  29. # extra dotfiles to display in HOME folder (dotfiles are hidden by default)
  30. # edit the list (space separated, surrounded by single quotes) or comment this line out, to taste:
  31. shown_dotfiles='.config .local .Xdefaults .bash_aliases .bashrc .fonts.conf .gtkrc-2.0.mine .profile .xsession-errors'
  32.  
  33. # By default, this script will display directories separately, before files.
  34. # To change this behaviour, see NOTE1, NOTE2 and NOTE3 below.
  35.  
  36. #######################################################################
  37.  
  38. # if "--open" option is sent as $1, open file ($2) instead of generating menu
  39. [ "$1" = '--open' ] && {
  40. open_file "$2"
  41. echo "$0 : failed to open $2" >&2
  42. exit    # in case exec command fails
  43. }
  44.  
  45. output='<openbox_pipe_menu>
  46. '
  47. # if "--recent" option is sent, incorporate "recent files" menu
  48. [ "$1" = '--recent' ] && {
  49. shift
  50. if [ -x "$recent_script" ]
  51. then
  52.     output="$output"'<separator label="Recent Files" />
  53. <menu execute="'"$recent_script"'" id="recent" label="Recent Files" />
  54. '
  55. else
  56.     echo "$0 : cannot find executable script $recent_script" >&2
  57. fi
  58. }
  59.  
  60. path="${1:-$HOME}"  # default starting place is ~, otherwise $1
  61. path="$( echo "${path}"/ | tr -s '/' )"    # ensure one final slash
  62. [ -d "$path" ] || { echo "$0 : $path is not a directory" >&2; exit 1; }
  63.  
  64. # escape xml special characters
  65. escape() {
  66. case "$1" in    # only escape if string needs it
  67. *\&*|*\<*|*\>*|*\"*|*\'*) sed "s/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/\"/\&quot;/g;s/'/\&apos;/g;" <<XXX
  68. $1
  69. XXX
  70. ;;
  71. *) printf '%s' "$1";;
  72. esac
  73. }
  74.  
  75. # escape &apos; (originally ') with surrounding '""' for command line
  76. escapos() {
  77. case "$1" in
  78. *\&apos\;*) sed 's/\&apos;/\&apos;\&quot;\&apos;\&quot;\&apos;/g;'<<XXX
  79. $1
  80. XXX
  81. ;;
  82. *) printf '%s' "$1";;
  83. esac
  84. }
  85.  
  86. pathe="$( escape "$path" )"
  87.  
  88. output="$output"'<separator label="'$pathe'" />
  89. <item label="Browse here...">
  90.         <action name="Execute">
  91.                 <execute>
  92.                  '$open_folder_cmd' "'$(escapos "$pathe" )'"
  93.                 </execute>
  94.         </action>
  95. </item>
  96. <separator />
  97. '
  98.  
  99. unset extra_entries directories_menu files_menu
  100. [ "$path" = "$HOME"/ ] && extra_entries="$shown_dotfiles"
  101. for i in "$path"* $extra_entries
  102. do
  103.     [ -e "$i" ] || continue    # only output code if file exists
  104.         shortname="$(escape "${i##*/}")"
  105.         ie="${pathe}${shortname}"
  106.         [ -d "$i" ] && {
  107. # NOTE1 If you want directories and files listed together       
  108. # change next line (directories_menu=$directories_menu') to read:       files_menu=$files_menu' (note the one single quote at the end)
  109.         directories_menu="$directories_menu"'
  110. <menu id="'"$ie"'" label="'"$shortname"'" execute="&apos;'"$0"'&apos; &apos;'"$(escapos "$ie")"'&apos;" />'; continue; }
  111.         files_menu="$files_menu"'
  112. <item label="'"$shortname"'">
  113.     <action name="Execute">
  114.         <execute>
  115.         &apos;'"$0"'&apos; --open &apos;'"$(escapos "$ie")"'&apos;
  116.         </execute>
  117.     </action>
  118. </item>'
  119. done
  120.  
  121. [ -n "$directories_menu" ] && {
  122. # NOTE2 comment out next 2 lines if you don't want "Directories" label
  123. output="${output}"'<separator label="Directories" />
  124. '
  125. output="${output}${directories_menu}"'
  126. '; }
  127. [ -n "$files_menu" ] && {
  128. # NOTE3 comment out next 2 lines if you don't want "Files" label
  129. output="${output}"'<separator label="Files" />
  130. '
  131. output="${output}${files_menu}"'
  132. '; }
  133. output="${output}"'</openbox_pipe_menu>
  134. '
  135. printf '%s' "$output"
  136. exit

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me