go: format the souce code using gofmt

Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
This commit is contained in:
Justus Winter 2012-05-09 13:15:20 +02:00 committed by David Bremner
parent 3113731713
commit 1952889353
2 changed files with 85 additions and 82 deletions

View file

@ -22,18 +22,18 @@ type frequencies map[string]uint
/* Used to sort the email addresses from most to least used */ /* Used to sort the email addresses from most to least used */
func sort_by_freq(m1, m2 *mail_addr_freq) int { func sort_by_freq(m1, m2 *mail_addr_freq) int {
if (m1.count[0] == m2.count[0] && if m1.count[0] == m2.count[0] &&
m1.count[1] == m2.count[1] && m1.count[1] == m2.count[1] &&
m1.count[2] == m2.count[2]) { m1.count[2] == m2.count[2] {
return 0 return 0
} }
if (m1.count[0] > m2.count[0] || if m1.count[0] > m2.count[0] ||
m1.count[0] == m2.count[0] && m1.count[0] == m2.count[0] &&
m1.count[1] > m2.count[1] || m1.count[1] > m2.count[1] ||
m1.count[0] == m2.count[0] && m1.count[0] == m2.count[0] &&
m1.count[1] == m2.count[1] && m1.count[1] == m2.count[1] &&
m1.count[2] > m2.count[2]) { m1.count[2] > m2.count[2] {
return -1 return -1
} }
@ -46,17 +46,17 @@ func (self *maddresses) Len() int {
return len(*self) return len(*self)
} }
func (self *maddresses) Less(i,j int) bool { func (self *maddresses) Less(i, j int) bool {
m1 := (*self)[i] m1 := (*self)[i]
m2 := (*self)[j] m2 := (*self)[j]
v := sort_by_freq(m1, m2) v := sort_by_freq(m1, m2)
if v<=0 { if v <= 0 {
return true return true
} }
return false return false
} }
func (self *maddresses) Swap(i,j int) { func (self *maddresses) Swap(i, j int) {
(*self)[i], (*self)[j] = (*self)[j], (*self)[i] (*self)[i], (*self)[j] = (*self)[j], (*self)[i]
} }
@ -66,7 +66,7 @@ func frequent_fullname(freqs frequencies) string {
fullname := "" fullname := ""
freqs_sz := len(freqs) freqs_sz := len(freqs)
for mail,freq := range freqs { for mail, freq := range freqs {
if (freq > maxfreq && mail != "") || freqs_sz == 1 { if (freq > maxfreq && mail != "") || freqs_sz == 1 {
// only use the entry if it has a real name // only use the entry if it has a real name
// or if this is the only entry // or if this is the only entry
@ -87,7 +87,7 @@ func addresses_by_frequency(msgs *notmuch.Messages, name string, pass uint, addr
pattern = `.*` + strings.ToLower(name) + `.*` pattern = `.*` + strings.ToLower(name) + `.*`
var re *regexp.Regexp = nil var re *regexp.Regexp = nil
var err error = nil var err error = nil
if re,err = regexp.Compile(pattern); err != nil { if re, err = regexp.Compile(pattern); err != nil {
log.Printf("error: %v\n", err) log.Printf("error: %v\n", err)
return &freqs return &freqs
} }
@ -97,22 +97,22 @@ func addresses_by_frequency(msgs *notmuch.Messages, name string, pass uint, addr
headers = append(headers, "to", "cc", "bcc") headers = append(headers, "to", "cc", "bcc")
} }
for ;msgs.Valid();msgs.MoveToNext() { for ; msgs.Valid(); msgs.MoveToNext() {
msg := msgs.Get() msg := msgs.Get()
//println("==> msg [", msg.GetMessageId(), "]") //println("==> msg [", msg.GetMessageId(), "]")
for _,header := range headers { for _, header := range headers {
froms := strings.ToLower(msg.GetHeader(header)) froms := strings.ToLower(msg.GetHeader(header))
//println(" froms: ["+froms+"]") //println(" froms: ["+froms+"]")
for _,from := range strings.Split(froms, ",") { for _, from := range strings.Split(froms, ",") {
from = strings.Trim(from, " ") from = strings.Trim(from, " ")
match := re.FindString(from) match := re.FindString(from)
//println(" -> match: ["+match+"]") //println(" -> match: ["+match+"]")
occ,ok := freqs[match] occ, ok := freqs[match]
if !ok { if !ok {
freqs[match] = 0 freqs[match] = 0
occ = 0 occ = 0
} }
freqs[match] = occ+1 freqs[match] = occ + 1
} }
} }
} }
@ -125,7 +125,7 @@ func search_address_passes(queries [3]*notmuch.Query, name string) []string {
addr_to_realname := make(map[string]*frequencies) addr_to_realname := make(map[string]*frequencies)
var pass uint = 0 // 0-based var pass uint = 0 // 0-based
for _,query := range queries { for _, query := range queries {
if query == nil { if query == nil {
//println("**warning: idx [",idx,"] contains a nil query") //println("**warning: idx [",idx,"] contains a nil query")
continue continue
@ -133,9 +133,9 @@ func search_address_passes(queries [3]*notmuch.Query, name string) []string {
msgs := query.SearchMessages() msgs := query.SearchMessages()
ht := addresses_by_frequency(msgs, name, pass, &addr_to_realname) ht := addresses_by_frequency(msgs, name, pass, &addr_to_realname)
for addr, count := range *ht { for addr, count := range *ht {
freq,ok := addr_freq[addr] freq, ok := addr_freq[addr]
if !ok { if !ok {
freq = &mail_addr_freq{addr:addr, count:[3]uint{0,0,0}} freq = &mail_addr_freq{addr: addr, count: [3]uint{0, 0, 0}}
} }
freq.count[pass] = count freq.count[pass] = count
addr_freq[addr] = freq addr_freq[addr] = freq
@ -154,8 +154,8 @@ func search_address_passes(queries [3]*notmuch.Query, name string) []string {
} }
sort.Sort(&addrs) sort.Sort(&addrs)
for _,addr := range addrs { for _, addr := range addrs {
freqs,ok := addr_to_realname[addr.addr] freqs, ok := addr_to_realname[addr.addr]
if ok { if ok {
val = append(val, frequent_fullname(*freqs)) val = append(val, frequent_fullname(*freqs))
} else { } else {
@ -187,21 +187,21 @@ func new_address_matcher() *address_matcher {
home = os.Getenv("HOME") home = os.Getenv("HOME")
} }
if cfg,err = config.ReadDefault(path.Join(home, ".notmuch-config")); err != nil { if cfg, err = config.ReadDefault(path.Join(home, ".notmuch-config")); err != nil {
log.Fatalf("error loading config file:",err) log.Fatalf("error loading config file:", err)
} }
db_path,_ := cfg.String("database", "path") db_path, _ := cfg.String("database", "path")
primary_email,_ := cfg.String("user", "primary_email") primary_email, _ := cfg.String("user", "primary_email")
addrbook_tag,err := cfg.String("user", "addrbook_tag") addrbook_tag, err := cfg.String("user", "addrbook_tag")
if err != nil { if err != nil {
addrbook_tag = "addressbook" addrbook_tag = "addressbook"
} }
self := &address_matcher{db:nil, self := &address_matcher{db: nil,
user_db_path:db_path, user_db_path: db_path,
user_primary_email:primary_email, user_primary_email: primary_email,
user_addrbook_tag:addrbook_tag} user_addrbook_tag: addrbook_tag}
return self return self
} }
@ -226,7 +226,7 @@ func (self *address_matcher) run(name string) {
// pass 2: look at all to: addresses sent from our primary mail // pass 2: look at all to: addresses sent from our primary mail
query = "" query = ""
if name != "" { if name != "" {
query = "to:"+name+"*" query = "to:" + name + "*"
} }
if self.user_primary_email != "" { if self.user_primary_email != "" {
query = query + " from:" + self.user_primary_email query = query + " from:" + self.user_primary_email
@ -234,17 +234,17 @@ func (self *address_matcher) run(name string) {
queries[1] = self.db.CreateQuery(query) queries[1] = self.db.CreateQuery(query)
// if that leads only to a few hits, we check every from too // if that leads only to a few hits, we check every from too
if queries[0].CountMessages() + queries[1].CountMessages() < 10 { if queries[0].CountMessages()+queries[1].CountMessages() < 10 {
query = "" query = ""
if name != "" { if name != "" {
query = "from:"+name+"*" query = "from:" + name + "*"
} }
queries[2] = self.db.CreateQuery(query) queries[2] = self.db.CreateQuery(query)
} }
// actually retrieve and sort addresses // actually retrieve and sort addresses
results := search_address_passes(queries, name) results := search_address_passes(queries, name)
for _,v := range results { for _, v := range results {
if v != "" && v != "\n" { if v != "" && v != "\n" {
fmt.Println(v) fmt.Println(v)
} }

View file

@ -15,6 +15,7 @@ import "unsafe"
// Status codes used for the return values of most functions // Status codes used for the return values of most functions
type Status C.notmuch_status_t type Status C.notmuch_status_t
const ( const (
STATUS_SUCCESS Status = iota STATUS_SUCCESS Status = iota
STATUS_OUT_OF_MEMORY STATUS_OUT_OF_MEMORY
@ -83,6 +84,7 @@ type Filenames struct {
} }
type DatabaseMode C.notmuch_database_mode_t type DatabaseMode C.notmuch_database_mode_t
const ( const (
DATABASE_MODE_READ_ONLY DatabaseMode = 0 DATABASE_MODE_READ_ONLY DatabaseMode = 0
DATABASE_MODE_READ_WRITE DATABASE_MODE_READ_WRITE
@ -98,7 +100,7 @@ func NewDatabase(path string) (*Database, Status) {
return nil, STATUS_OUT_OF_MEMORY return nil, STATUS_OUT_OF_MEMORY
} }
self := &Database{db:nil} self := &Database{db: nil}
st := Status(C.notmuch_database_create(c_path, &self.db)) st := Status(C.notmuch_database_create(c_path, &self.db))
if st != STATUS_SUCCESS { if st != STATUS_SUCCESS {
return nil, st return nil, st
@ -132,7 +134,7 @@ func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) {
return nil, STATUS_OUT_OF_MEMORY return nil, STATUS_OUT_OF_MEMORY
} }
self := &Database{db:nil} self := &Database{db: nil}
st := Status(C.notmuch_database_open(c_path, C.notmuch_database_mode_t(mode), &self.db)) st := Status(C.notmuch_database_open(c_path, C.notmuch_database_mode_t(mode), &self.db))
if st != STATUS_SUCCESS { if st != STATUS_SUCCESS {
return nil, st return nil, st
@ -181,7 +183,6 @@ func (self *Database) NeedsUpgrade() bool {
// TODO: notmuch_database_upgrade // TODO: notmuch_database_upgrade
/* Retrieve a directory object from the database for 'path'. /* Retrieve a directory object from the database for 'path'.
* *
* Here, 'path' should be a path relative to the path of 'database' * Here, 'path' should be a path relative to the path of 'database'
@ -202,7 +203,7 @@ func (self *Database) GetDirectory(path string) *Directory {
if c_dir == nil { if c_dir == nil {
return nil return nil
} }
return &Directory{dir:c_dir} return &Directory{dir: c_dir}
} }
/* Add a new message to the given notmuch database. /* Add a new message to the given notmuch database.
@ -245,8 +246,7 @@ func (self *Database) GetDirectory(path string) *Directory {
* NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only
* mode so no message can be added. * mode so no message can be added.
*/ */
func func (self *Database) AddMessage(fname string) (*Message, Status) {
(self *Database) AddMessage(fname string) (*Message, Status) {
var c_fname *C.char = C.CString(fname) var c_fname *C.char = C.CString(fname)
defer C.free(unsafe.Pointer(c_fname)) defer C.free(unsafe.Pointer(c_fname))
@ -257,7 +257,7 @@ func
var c_msg *C.notmuch_message_t = new(C.notmuch_message_t) var c_msg *C.notmuch_message_t = new(C.notmuch_message_t)
st := Status(C.notmuch_database_add_message(self.db, c_fname, &c_msg)) st := Status(C.notmuch_database_add_message(self.db, c_fname, &c_msg))
return &Message{message:c_msg}, st return &Message{message: c_msg}, st
} }
/* Remove a message from the given notmuch database. /* Remove a message from the given notmuch database.
@ -318,7 +318,7 @@ func (self *Database) FindMessage(message_id string) (*Message, Status) {
return nil, STATUS_OUT_OF_MEMORY return nil, STATUS_OUT_OF_MEMORY
} }
msg := &Message{message:nil} msg := &Message{message: nil}
st := Status(C.notmuch_database_find_message(self.db, c_msg_id, &msg.message)) st := Status(C.notmuch_database_find_message(self.db, c_msg_id, &msg.message))
if st != STATUS_SUCCESS { if st != STATUS_SUCCESS {
return nil, st return nil, st
@ -338,7 +338,7 @@ func (self *Database) GetAllTags() *Tags {
if tags == nil { if tags == nil {
return nil return nil
} }
return &Tags{tags:tags} return &Tags{tags: tags}
} }
/* Create a new query for 'database'. /* Create a new query for 'database'.
@ -378,11 +378,12 @@ func (self *Database) CreateQuery(query string) *Query {
if q == nil { if q == nil {
return nil return nil
} }
return &Query{query:q} return &Query{query: q}
} }
/* Sort values for notmuch_query_set_sort */ /* Sort values for notmuch_query_set_sort */
type Sort C.notmuch_sort_t type Sort C.notmuch_sort_t
const ( const (
SORT_OLDEST_FIRST Sort = 0 SORT_OLDEST_FIRST Sort = 0
SORT_NEWEST_FIRST SORT_NEWEST_FIRST
@ -457,7 +458,7 @@ func (self *Query) SearchThreads() *Threads {
if threads == nil { if threads == nil {
return nil return nil
} }
return &Threads{threads:threads} return &Threads{threads: threads}
} }
/* Execute a query for messages, returning a notmuch_messages_t object /* Execute a query for messages, returning a notmuch_messages_t object
@ -503,7 +504,7 @@ func (self *Query) SearchMessages() *Messages {
if msgs == nil { if msgs == nil {
return nil return nil
} }
return &Messages{messages:msgs} return &Messages{messages: msgs}
} }
/* Destroy a notmuch_query_t along with any associated resources. /* Destroy a notmuch_query_t along with any associated resources.
@ -605,7 +606,7 @@ func (self *Messages) Get() *Message {
if msg == nil { if msg == nil {
return nil return nil
} }
return &Message{message:msg} return &Message{message: msg}
} }
/* Move the 'messages' iterator to the next message. /* Move the 'messages' iterator to the next message.
@ -657,7 +658,7 @@ func (self *Messages) CollectTags() *Tags {
if tags == nil { if tags == nil {
return nil return nil
} }
return &Tags{tags:tags} return &Tags{tags: tags}
} }
/* Get the message ID of 'message'. /* Get the message ID of 'message'.
@ -737,7 +738,7 @@ func (self *Message) GetReplies() *Messages {
if msgs == nil { if msgs == nil {
return nil return nil
} }
return &Messages{messages:msgs} return &Messages{messages: msgs}
} }
/* Get a filename for the email corresponding to 'message'. /* Get a filename for the email corresponding to 'message'.
@ -770,6 +771,7 @@ func (self *Message) GetFileName() string {
} }
type Flag C.notmuch_message_flag_t type Flag C.notmuch_message_flag_t
const ( const (
MESSAGE_FLAG_MATCH Flag = 0 MESSAGE_FLAG_MATCH Flag = 0
) )
@ -867,7 +869,7 @@ func (self *Message) GetTags() *Tags {
if tags == nil { if tags == nil {
return nil return nil
} }
return &Tags{tags:tags} return &Tags{tags: tags}
} }
/* The longest possible tag value. */ /* The longest possible tag value. */
@ -1124,4 +1126,5 @@ func (self *Filenames) Destroy() {
} }
C.notmuch_filenames_destroy(self.fnames) C.notmuch_filenames_destroy(self.fnames)
} }
/* EOF */ /* EOF */