-
Notifications
You must be signed in to change notification settings - Fork 0
/
openeditor
105 lines (92 loc) · 2.24 KB
/
openeditor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# opens a file in the editor of your choice
# good thing: it checks which editor is open and opens the file in that editor
# if no editor is found it defaults to a lightweight editor (like kwrite)
# the order in which to execute the commands
# breaks on success
# available are the following commands:
#
# kwrite_new_win
# quanta_new_win
# kate_new_win
#
# quanta_new_tab
# kate_new_tab
#
# for Protocol in Firefox go to about config
# Right-click -> New -> Boolean -> Name:
# network.protocol-handler.expose.foo -> Value -> false
# (Replace foo with the protocol you're specifying)
# Then click a link and choose your editor \o/
#
# Url accepted :
# ./editor file://brol
# ./editor file://brol 12
# ./editor file://brol:12
# ./editor file://brol
# ./editor /var/brol:12
# ./editor editor://open/?url=file:///brol/file:12
order=(
# debug
kate_new_tab
kate_new_win
kwrite_new_win
)
# nothing should be changed below
function debug {
echo "file: '$1' line: '$2'"
}
# new kate tab
function kate_new_tab {
isrunning kate || return 1
echo "open file in a new kate tab:"
kate -u "$1" --line "$2"
}
# new kate window
function kate_new_win {
echo "open file in kate:"
kate "$1" --line "$2"
}
# new kwrite window
function kwrite_new_win {
echo "open file in kwrite:"
kwrite "$1"
}
# new kdevelop window
function kdevelop_new_win_tab {
echo "open file in kdevelop:"
id=$(qdbus | grep kdevelop)
file=$(readlink -f $1)
if [[ ${id} ]]; then
qdbus $id /org/kdevelop/DocumentController org.kdevelop.DocumentController.openDocumentSimple $file
else
kdevelop $1
fi
}
# check if a process is running
function isrunning {
pid=(`pidof $1`)
return `[[ ${pid[0]} ]]`
}
# If we are comming from editor://open?url=file://brol:line
url=$1
if [[ "$1" =~ ^editor:.* ]]; then
url=$(echo $1 | sed -e 's/.*url=//')
fi
oldname=$url
#remove protocol for sed
fname=$(echo $url | sed -e 's/^file:\///')
file=$(echo $fname | sed -e 's/:.*//g')
line=$(echo $fname | cut -d ':' -f2)
#re set protocol if removed
if [[ "$oldname" != "$fname" ]]; then
file="file:/$file"
fi
if [[ "$line" = "$fname" ]]; then
line=$2
fi
# call functions based on order defined above
for i in `seq 1 ${#order[@]}`;
do
${order[$i-1]} "$file" "$line" $&& exit 0
done